Windows Phone 8.1中的WriteableBitmap.Pixels

3
我正在编写一个Windows Phone 8.1(WINRT)应用程序。我正在使用fileopenpicker从库中选择图片,然后在我的应用程序中显示它。但是我想让用户在图片在应用程序中显示之前可以裁剪这个图片。
在Windows Phone 8中,我们使用Photochooser任务并设置width属性和裁剪选项自动出现。
现在我正在尝试使用这个: Windows Phone 8.0: Image Crop With Rectangle 但在Windows Phone 8.1中没有WriteableBitmap.Pixels。那么,在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;
}

它是.PixelBuffer,请参见:https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.media.imaging.writeablebitmap.pixelbuffer.aspx - Chubosaurus Software
我也尝试了同样的解决方案,但WB_CroppedImage.Pixels[i]不起作用。有什么解决办法吗? - Apoorv
1个回答

1

您应该查看BitmapEncoderBitmapDecoder类。

此外,您可能会使用BitmapBounds来裁剪您的图像 - 设置'X'和'Y'以及'宽度'和'高度'。

我认为代码可能是这样的(但我还没有测试过):

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();
    }
}

当然,您不需要将编辑后的图片保存到StorageFile中 - 我只是举了一个例子,您可以写入流,然后设置图像源。

首先,问题在于Windows Phone 8.1不支持MouseLeftButtonUp、MouseLeftButtonDown和MouseMove。 - Atif Shabeer
@AtifShabeer,您询问如何裁剪提供的图像,那么MouseEvent与此有何关系?上面的方法应该可以裁剪图像,但是您需要提供如何裁剪的信息。 - Romasz
我想在我的应用程序中实现相同的功能:您能否查看此Windows Phone 8.0教程:http://www.c-sharpcorner.com/uploadfile/55275a/windowsphone-image-crop-with-rectangle/ - Atif Shabeer
@AtifShabeer,抱歉,我没有时间分析这个。 - Romasz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接