2014-04-01 93 views
1

如何在WinRT中裁剪/裁剪图像。我有一个完全填充在Windows 8窗口中的图像。我需要从中心裁剪/裁剪图像,我需要将两个图像部分显示为两个单独的网格。我怎样才能通过Windows 8做到这一点。是否有可能实现这一点,而不使用WritableBitmapEx。如果不是,那么如何通过WritableBitmapEx完成相同的操作。WinRT中的裁剪/裁剪图像

回答

3

有很多方法可以做到这实际上,EA带有一些优点和缺点。

  • WriteableBitmapEx看起来很受欢迎的解决方案。我在WinRT XAML Toolkit中也有类似的实现。两者基本上都是从完整的图像位图复制像素块。这可能不是最快的方法,但是如果你想要开箱即用的解决方案 - 这是一个易于使用的解决方案。您需要复制像素,因此您在操作时并未针对内存使用进行优化,因此可能会更快地在超大图像上耗尽内存。如果需要,您可以轻松修复,但最终会将结果保存到图像文件。
  • Jan建议的BitmapDecoder解决方案是我经常使用的解决方案,因为它是平台的一部分,使用本地代码编写,并且可能高度优化,并且不会复制像素,但是如果您想要复制 - 您需要再次解码图像。
  • Xyroid对Clip几何的建议是一种快速显示的解决方案。您实际上并不修改内存中的位图 - 只需在屏幕上显示它的一个区域即可。然后,您需要将整个图像保存在内存中,并且如果要保存它 - 您仍然需要更新位图以保存它 - 通过使用前两种解决方案中的任意一种,或者如果屏幕分辨率足够用,可以使用RenderTargetBitmap.Render()。尽快更新屏幕上显示的裁剪区域以进行快速预览应该非常快速。
  • 另一种方法是用Rectangle填充ImageBrush,您可以在其中应用Transform并指定Rectangle大小来控制裁剪。它与Clip解决方案非常相似,而不是剪辑图像,在这种情况下,您实际上必须使用Tramsform(您也可以在Clip - RectangleGeometry上执行此操作)。对于快速更新 - 使用Transform实际上可能比更新几何体快一些,并且还支持缩放和旋转。
1

您可以使用Bitmapdecoder和BitmapTransform类。 This example非常适合种植。您还应该阅读this tutorial进行剪辑。基本上你实现这样的功能(从例如拍摄):

async public static Task<ImageSource> GetCroppedBitmapAsync(StorageFile originalImgFile, Point startPoint, Size corpSize, double scale) 
{ 


if (double.IsNaN(scale) || double.IsInfinity(scale)) 
{ 
    scale = 1; 
} 


// Convert start point and size to integer. 
uint startPointX = (uint)Math.Floor(startPoint.X * scale); 
uint startPointY = (uint)Math.Floor(startPoint.Y * scale); 
uint height = (uint)Math.Floor(corpSize.Height * scale); 
uint width = (uint)Math.Floor(corpSize.Width * scale); 


using (IRandomAccessStream stream = await originalImgFile.OpenReadAsync()) 
{ 


    // Create a decoder from the stream. With the decoder, we can get 
    // the properties of the image. 
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); 

    // The scaledSize of original image. 
    uint scaledWidth = (uint)Math.Floor(decoder.PixelWidth * scale); 
    uint scaledHeight = (uint)Math.Floor(decoder.PixelHeight * scale); 



    // Refine the start point and the size. 
    if (startPointX + width > scaledWidth) 
    { 
     startPointX = scaledWidth - width; 
    } 


    if (startPointY + height > scaledHeight) 
    { 
     startPointY = scaledHeight - height; 
    } 


    // Create cropping BitmapTransform and define the bounds. 
    BitmapTransform transform = new BitmapTransform(); 
    BitmapBounds bounds = new BitmapBounds(); 
    bounds.X = startPointX; 
    bounds.Y = startPointY; 
    bounds.Height = height; 
    bounds.Width = width; 
    transform.Bounds = bounds; 


    transform.ScaledWidth = scaledWidth; 
    transform.ScaledHeight = scaledHeight; 

    // Get the cropped pixels within the bounds of transform. 
    PixelDataProvider pix = await decoder.GetPixelDataAsync( 
     BitmapPixelFormat.Bgra8, 
     BitmapAlphaMode.Straight, 
     transform, 
     ExifOrientationMode.IgnoreExifOrientation, 
     ColorManagementMode.ColorManageToSRgb); 
    byte[] pixels = pix.DetachPixelData(); 


    // Stream the bytes into a WriteableBitmap 
    WriteableBitmap cropBmp = new WriteableBitmap((int)width, (int)height); 
    Stream pixStream = cropBmp.PixelBuffer.AsStream(); 
    pixStream.Write(pixels, 0, (int)(width * height * 4)); 


    return cropBmp; 
} 


} 
+0

嗨Jan,我有一个图像设置为统一,其水平和垂直居中,我使用WriteableBitmapEx裁剪,但我没有得到确切的裁剪区域。 – Apoorv

1

XAML静态的方式,如果我的屏幕尺寸为1366×768 &我要裁剪中心400x300的图像,然后我会做到这一点。

<Image Source="Assets/img100.png" Stretch="Fill"> 
    <Image.Clip> 
     <RectangleGeometry Rect="483,234,400,300" /> 
    </Image.Clip> 
</Image> 

动态的方式。它会使所有分辨率的中心裁剪,尽管高度&宽度是固定的。

double _Height = 300, _Width = 400; 
img.Clip = new RectangleGeometry 
{ 
    Rect = new Rect((Window.Current.Bounds.Width - _Width)/2, (Window.Current.Bounds.Height - _Height)/2, _Width, _Height) 
}; 

不要忘了签...

How to resize Image in C# WinRT/winmd?

Crop image with rectangle

Crop image with dynamic rectangle coordinate

Cropping tool after file picker (like the one after you take a picture)