3

I am writing a Windows Phone 8.1 (WINRT) App. I am using fileopenpicker to choose a picture from gallery and then i display it in my app. But i want that user can crop this image before getting displayed on app.

In windows phone 8, we used Photochooser task having width property and cropping options automatically used to come.

Now I am trying to use this : Windows Phone 8.0: Image Crop With Rectangle

But there is no WriteableBitmap.Pixels in Windows Phone 8.1. What to use instead of WriteableBitmap.Pixels?

// Create a new WriteableBitmap. The size of the bitmap is the size of the cropping rectangle
// drawn by the user, multiplied by the image size ratio.
WB_CroppedImage = new WriteableBitmap((int)(widthRatio * Math.Abs(Point2.X - Point1.X)), (int)(heightRatio * Math.Abs(Point2.Y - Point1.Y)));

// Calculate the offset of the cropped image. This is the distance, in pixels, to the top left corner
// of the cropping rectangle, multiplied by the image size ratio.
int xoffset = (int)(((Point1.X < Point2.X) ? Point1.X : Point2.X) * widthRatio);
int yoffset = (int)(((Point1.Y < Point2.Y) ? Point1.Y : Point2.X) * heightRatio);

// Copy the pixels from the targeted region of the source image into the target image, 
// using the calculated offset
if (WB_CroppedImage.Pixels.Length > 0)
{
    for (int i = 0; i < WB_CroppedImage.Pixels.Length; i++)
    {
        int x = (int)((i % WB_CroppedImage.PixelWidth) + xoffset);
        int y = (int)((i / WB_CroppedImage.PixelWidth) + yoffset);
        WB_CroppedImage.Pixels[i] = WB_CapturedImage.Pixels[y * WB_CapturedImage.PixelWidth + x];
    }

    // Set the source of the image control to the new cropped bitmap
    FinalCroppedImage.Source = WB_CroppedImage;                              
}
else
{
     FinalCroppedImage.Source = null;
}
Romasz
  • 29,662
  • 13
  • 79
  • 154
Atif Shabeer
  • 553
  • 6
  • 18

1 Answers1

1

You should take a look at BitmapEncoder and BitmapDecoder classes.

Also you probably will be able to use BitmapBounds to crop your image - set 'X' and 'Y' along with 'Width' and 'Height'.

I think the code may look like this (but I've not tested it):

StorageFile destination; // your destination file
using (var sourceStream = await sourceFile.OpenAsync(FileAccessMode.Read))
{
    BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(sourceStream);
    // here you scale your image if needed and crop by setting X, Y, Width and Height
    BitmapTransform bmpTransform = new BitmapTransform() { ScaledHeight = scaledHeight, ScaledWidth = scaledWidth, InterpolationMode = BitmapInterpolationMode.Cubic, Bounds = new BitmapBounds { X = topLeftX, Y = topLeftY Width = desiredSizeW, Height = desiredSizeH } };
    PixelDataProvider pixelData = await bmpDecoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, bmpTransform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
    using (var destFileStream = await destination.OpenAsync(FileAccessMode.ReadWrite))
    {
        BitmapEncoder bmpEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destFileStream);
        // here you need to set height and width - take from above
        bmpEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, desiredSizeW, desiredSizeH, 300, 300, pixelData.DetachPixelData());
        await bmpEncoder.FlushAsync();
    }
}

Of course you don't need to save the edited picture to StorageFile - I've used it as an example, you can write to stream and then set your image source.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • First the problem is MouseLeftButtonUp,MouseLeftButtonDown,MouseMove are not supported in Windows phone 8.1 – Atif Shabeer Feb 04 '15 at 13:00
  • @AtifShabeer You have asked about cropiing the provided image, what MouseEvent have here to do? The method above should crop the image, but you have to provide the information how. – Romasz Feb 04 '15 at 13:06
  • I want to implement the same in my app: Can you have a look at this Windows Phone 8.0 Tutorial: http://www.c-sharpcorner.com/uploadfile/55275a/windowsphone-image-crop-with-rectangle/ – Atif Shabeer Feb 04 '15 at 13:10
  • @AtifShabeer I'm sorry, but I won't have time to analyze this. – Romasz Feb 04 '15 at 13:17