How to copy and resize images in Windows 10 UWP

I used the code http://www.codeproject.com/Tips/552141/Csharp-Image-resize-convert-and-save to programmatically resize images. However, this project uses the System.Drawing libraries, which are not available for Windows 10 applications.

I tried using the BitmapImage class from Windows.UI.Xaml.Media.Imaging instead, but it does not seem to offer the functionality found in System.Drawing .

Has anyone had the ability to delete (reduce) images in Windows 10? My application will process images from several sources, in different formats / sizes, and I'm trying to resize the actual images to save space, and not just let the size of the application match the Image in which it is displayed.

EDIT

I changed the code from the above link and have a hack that works for my specific need. Here he is:

 public static BitmapImage ResizedImage(BitmapImage sourceImage, int maxWidth, int maxHeight) { var origHeight = sourceImage.PixelHeight; var origWidth = sourceImage.PixelWidth; var ratioX = maxWidth/(float) origWidth; var ratioY = maxHeight/(float) origHeight; var ratio = Math.Min(ratioX, ratioY); var newHeight = (int) (origHeight * ratio); var newWidth = (int) (origWidth * ratio); sourceImage.DecodePixelWidth = newWidth; sourceImage.DecodePixelHeight = newHeight; return sourceImage; } 

This method seems to work, but ideally, instead of changing the original BitmapImage I would like to create a new / copy of it to change and return.

Here is a shot at action: resized screenshot

+6
source share
1 answer

I might want to return a copy of the original BitmapImage instead of modifying the original.

There is no good method for directly copying BitmapImage , but we can reuse StorageFile several times.

If you just want to select an image, show it and show the image with the original StorageFile , you can pass the StorageFile parameter as follows:

 public static async Task<BitmapImage> ResizedImage(StorageFile ImageFile, int maxWidth, int maxHeight) { IRandomAccessStream inputstream = await ImageFile.OpenReadAsync(); BitmapImage sourceImage = new BitmapImage(); sourceImage.SetSource(inputstream); var origHeight = sourceImage.PixelHeight; var origWidth = sourceImage.PixelWidth; var ratioX = maxWidth / (float)origWidth; var ratioY = maxHeight / (float)origHeight; var ratio = Math.Min(ratioX, ratioY); var newHeight = (int)(origHeight * ratio); var newWidth = (int)(origWidth * ratio); sourceImage.DecodePixelWidth = newWidth; sourceImage.DecodePixelHeight = newHeight; return sourceImage; } 

In this scenario, you just need to call this task and show the image with the dimensions of the following size:

 smallImage.Source = await ResizedImage(file, 250, 250); 

If you want to save the BitmapImage parameter for some reason (for example, sourceImage may be a modified bitmap, but not directly loaded from a file), and you want to resize the new image to another, you will first need to save the image with dimensions as a file and then open this file and resize it again.

+8
source

All Articles