1: void OnCameraCaptureCompleted(object sender, PhotoResult e)
2: {
3: // figure out the orientation from EXIF data
4: e.ChosenPhoto.Position = 0;
5: JpegInfo info = ExifReader.ReadJpeg(e.ChosenPhoto, e.OriginalFileName);
6:
7: _width = info.Width;
8: _height = info.Height;
9: _orientation = info.Orientation;
10:
11: PostedUri.Text = info.Orientation.ToString();
12:
13: switch (info.Orientation)
14: {
15: case ExifOrientation.TopLeft:
16: case ExifOrientation.Undefined:
17: _angle = 0;
18: break;
19: case ExifOrientation.TopRight:
20: _angle = 90;
21: break;
22: case ExifOrientation.BottomRight:
23: _angle = 180;
24: break;
25: case ExifOrientation.BottomLeft:
26: _angle = 270;
27: break;
28: }
29:
30: if (_angle > 0d)
31: {
32: capturedImage = RotateStream(e.ChosenPhoto, _angle);
33: }
34: else
35: {
36: capturedImage = e.ChosenPhoto;
37: }
38:
39: BitmapImage bmp = new BitmapImage();
40: bmp.SetSource(capturedImage);
41:
42: ChosenPicture.Source = bmp;
43: }
44:
45: private Stream RotateStream(Stream stream, int angle)
46: {
47: stream.Position = 0;
48: if (angle % 90 != 0 || angle < 0) throw new ArgumentException();
49: if (angle % 360 == 0) return stream;
50:
51: BitmapImage bitmap = new BitmapImage();
52: bitmap.SetSource(stream);
53: WriteableBitmap wbSource = new WriteableBitmap(bitmap);
54:
55: WriteableBitmap wbTarget = null;
56: if (angle % 180 == 0)
57: {
58: wbTarget = new WriteableBitmap(wbSource.PixelWidth, wbSource.PixelHeight);
59: }
60: else
61: {
62: wbTarget = new WriteableBitmap(wbSource.PixelHeight, wbSource.PixelWidth);
63: }
64:
65: for (int x = 0; x < wbSource.PixelWidth; x++)
66: {
67: for (int y = 0; y < wbSource.PixelHeight; y++)
68: {
69: switch (angle % 360)
70: {
71: case 90:
72: wbTarget.Pixels[(wbSource.PixelHeight - y - 1) + x * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];
73: break;
74: case 180:
75: wbTarget.Pixels[(wbSource.PixelWidth - x - 1) + (wbSource.PixelHeight - y - 1) * wbSource.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];
76: break;
77: case 270:
78: wbTarget.Pixels[y + (wbSource.PixelWidth - x - 1) * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];
79: break;
80: }
81: }
82: }
83: MemoryStream targetStream = new MemoryStream();
84: wbTarget.SaveJpeg(targetStream, wbTarget.PixelWidth, wbTarget.PixelHeight, 0, 100);
85: return targetStream;
86: }