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.
source share