How I can Crop / Clip images in WinRT. I have an image filled completely in a windows 8 window. I need to clip / crop the image from center and i need to display the two image section into two separate grids. How I can do the same through windows 8. Is it possible to implement this without using WritableBitmapEx. If no, how to do the same through WritableBitmapEx.
There are many ways to do it actually, each with some pros and cons.
WriteableBitmapEx
seems like a popular solution. I have a similar implementation in WinRT XAML Toolkit
. Both are essentially copying blocks of pixels from a full image bitmap. It might not be the fastest way, but if you'd want to get an out of the box solution - it is one that is easy to use. You need to copy the pixels, so you are not optimizing for memory use at the time of the operation and so might run out of memory on very large images quicker. You can recrop easily though end save the results to an image file if you want.BitmapDecoder
solution Jan recommended is one I often use as it is part of the platform, written in native code and possibly highly optimized and you don't copy the pixels, but if you want to recrop - you'll need to decode the image again.Clip
geometry is a quick display-only solution. You don't actually modify the bitmap in memory - you simply display a region of it on the screen. You need then to keep the entire image in memory and if you want to save it - you still need to update the bitmap to save it - by using either one of the first two solutions or maybe use RenderTargetBitmap.Render()
if screen resolution is enough for you. It should be very quick though to update the crop region displayed on the screen for quick preview.Rectangle
filled with an ImageBrush
where you can apply a Transform
and specify the Rectangle
size to control cropping. It is fairly similar to the Clip
solution only instead of clipping an image and in this case you actually have to use the Tramsform
(which you can also do on a Clip
- RectangleGeometry
). For quick updates - using a Transform
might actually be a bit faster than updating the geometry and also supports scaling and rotations.