1
Answer

Resizing a PNG in a UWP project without losing transparency?

Ali Roshanbin

Ali Roshanbin

7y
248
1
Hi there,
 
I want to resize a PNG image that is transparent in a Universal Windows Project, but I lose transparency.
Similar to this article of the Stack Overflow website.
http://stackoverflow.com/questions/753968/why-does-resizing-a-png-image-lose-transparency 
Can anyone help me to figure it out?
 
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;
 
Answers (1)