How to resize Image in C# WinRT/winmd?

前端 未结 6 737
执念已碎
执念已碎 2020-11-27 18:54

I\'ve got simple question, but so far I\'ve found no answer: how to resize jpeg image in C# WinRT/WinMD project and save it as new jpeg?

I\'m developing Windows 8 Me

6条回答
  •  再見小時候
    2020-11-27 19:26

    More simpler code to re-size the image, not crop. The below code re-size the image as 80x80

    using (var sourceStream = await sourceFile.OpenAsync(FileAccessMode.Read))
    {
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream);
        BitmapTransform transform = new BitmapTransform() { ScaledHeight = 80, ScaledWidth = 80 };
        PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
            BitmapPixelFormat.Rgba8,
            BitmapAlphaMode.Straight,
            transform,
            ExifOrientationMode.RespectExifOrientation,
            ColorManagementMode.DoNotColorManage);
    
        using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream);
            encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, 80, 80, 96, 96, pixelData.DetachPixelData());                        
            await encoder.FlushAsync();
        }
    }
    

    Source

提交回复
热议问题