【问题标题】:WriteableBitmap.Pixels in Windows Phone 8.1Windows Phone 8.1 中的 WriteableBitmap.Pixels
【发布时间】:2015-02-04 11:13:18
【问题描述】:

我正在编写一个 Windows Phone 8.1 (WINRT) 应用程序。我正在使用 fileopenpicker 从图库中选择一张图片,然后将其显示在我的应用程序中。但我希望用户在显示在应用上之前可以裁剪此图像。

在 windows phone 8 中,我们使用了 Photochooser 任务,它具有自动使用的宽度属性和裁剪选项。

现在我正在尝试使用它: 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;
}

【问题讨论】:

标签: c# windows-phone-8 windows-runtime windows-phone-8.1 writablebitmap


【解决方案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
  • @AtifShabeer 你问过关于裁剪提供的图像,MouseEvent 在这里做什么?上面的方法应该裁剪图像,但您必须提供信息。
  • 我想在我的应用程序中实现同样的功能:你能看看这个 Windows Phone 8.0 教程吗:c-sharpcorner.com/uploadfile/55275a/…
  • @AtifShabeer 很抱歉,我没有时间分析这个。
猜你喜欢
  • 2023-04-03
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多