我正在尝试将 jpeg 图像从 3028x4051 缩小到 854x1171。这导致图像接近 100 万像素并保持纵横比。原图为here .虽然图像按比例缩小很差。下面是图像的一部分。顶部是在 MS paint 中按比例缩小的图像,底部是在 C# 中以编程方式按比例缩小的图像。我已经包含了我用来缩小和保存它的代码。有人知道会发生什么吗?
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();
}
最佳答案
在创建 BitmapTransform
的新实例的地方,您可以指定插值模式。 Linear 是默认模式,因此要获得更好的结果,您需要尝试使用 Cubic 或 Fant。
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();
}
关于c# - 使用 C# 缩小 Jpeg 图像的效果不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30596634/