Poorly scaled Jpeg image using c #
I'm trying to downsize a jpeg image from 3028x4051 to 854x1171. The result is an image close to 1M pixels and maintains the aspect ratio. The original image is here . However, the image deteriorates greatly. Below is a snippet of the image. The upper part is the image zoomed out in MS mask, the lower part is shortened programmatically in C #. I included the code I used to zoom out and save it. Does anyone know what might happen?
using (IRandomAccessStream sourceStream = await sourceFile.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder myDecoder = await GetDecoder(sourceStream);
BitmapTransform myTransform = new BitmapTransform() { ScaledHeight = h, ScaledWidth = w };
await myDecoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Premultiplied,
myTransform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
BitmapEncoder myEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream);
myEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, w, h, 96, 96, pixelData.DetachPixelData());
await myEncoder.FlushAsync();
}
+3
source to share
1 answer
If you are creating a new instance BitmapTransform
, you can specify the interpolation mode. Linear mode is the default, so you should try Cubic or Fant for best results.
using (IRandomAccessStream sourceStream = await sourceFile.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder myDecoder = await GetDecoder(sourceStream);
BitmapTransform myTransform = new BitmapTransform() { ScaledHeight = h, ScaledWidth = w, InterpolationMode = BitmapInterpolationMode.Fant };
await myDecoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Premultiplied,
myTransform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
BitmapEncoder myEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream);
myEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, w, h, 96, 96, pixelData.DetachPixelData());
await myEncoder.FlushAsync();
}
+2
source to share