Windows Phone practical development skills (32): Photo angle Processing

Source: Internet
Author: User

In the actual project, you may need to select an image from the album and then process it accordingly. However, I don't know if you have found such a situation, that is, the mobile phone is vertical, but it is indeed horizontal when uploading to Weibo or where. In one case, if you take a picture on your phone, the picture is horizontal, although it is vertical on your phone. (It may be a bit abstract. In this case, you should be deeply touched)

So what should we do in the client? One idea is to obtain the image angle. If it is 90 °, flip the image and perform the corresponding operations. In this way, two problems are involved.

1. How to obtain the photo angle in the album

2. How to flip an existing photo (stream, Bitmap, or WriteableBitmap)

I checked the system API and didn't provide support for photos, but we can use the ExifLib open-source library.

The following method is used to obtain the angle of the selected image.

/// <summary>/// get angle of photo/// </summary>/// <param name="stream">photo stream</param>/// <param name="filename">photo name</param>/// <returns>angle of the photo</returns>public static int GetAngle(Stream stream, string filename){    ExifLib.ExifOrientation _orientation;    int _angle = 0;    stream.Position = 0;    JpegInfo info = ExifReader.ReadJpeg(stream, filename);    if (info!=null)    {        _orientation = info.Orientation;        switch (info.Orientation)        {            case ExifOrientation.TopLeft:            case ExifOrientation.Undefined:                _angle = 0;                break;            case ExifOrientation.TopRight:                _angle = 90;                break;            case ExifOrientation.BottomRight:                _angle = 180;                break;            case ExifOrientation.BottomLeft:                _angle = 270;                break;        }    }    return _angle;}

After obtaining the angle, if the angle is 90 °, that is, inverse, we need to correct it. You can use the following method:

private Stream RotateStream(Stream stream, int angle){    stream.Position = 0;    if (angle % 90 != 0 || angle < 0) throw new ArgumentException();    if (angle % 360 == 0) return stream;    BitmapImage bitmap = new BitmapImage();    bitmap.SetSource(stream);    WriteableBitmap wbSource = new WriteableBitmap(bitmap);    WriteableBitmap wbTarget = null;    if (angle % 180 == 0)    {        wbTarget = new WriteableBitmap(wbSource.PixelWidth, wbSource.PixelHeight);    }    else    {        wbTarget = new WriteableBitmap(wbSource.PixelHeight, wbSource.PixelWidth);    }    for (int x = 0; x < wbSource.PixelWidth; x++)    {        for (int y = 0; y < wbSource.PixelHeight; y++)        {            switch (angle % 360)            {                case 90:                    wbTarget.Pixels[(wbSource.PixelHeight - y - 1) + x * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];                    break;                case 180:                    wbTarget.Pixels[(wbSource.PixelWidth - x - 1) + (wbSource.PixelHeight - y - 1) * wbSource.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];                    break;                case 270:                    wbTarget.Pixels[y + (wbSource.PixelWidth - x - 1) * wbTarget.PixelWidth] = wbSource.Pixels[x + y * wbSource.PixelWidth];                    break;            }        }    }    MemoryStream targetStream = new MemoryStream();    wbTarget.SaveJpeg(targetStream, wbTarget.PixelWidth, wbTarget.PixelHeight, 0, 100);    return targetStream;}

Download Demo source code

Original article Handling picture orientation in CameraCaptureTask in Windows Phone 7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.