c# - 使用 C# 缩小 Jpeg 图像的效果不佳

标签 c# image

我正在尝试将 jpeg 图像从 3028x4051 缩小到 854x1171。这导致图像接近 100 万像素并保持纵横比。原图为here .虽然图像按比例缩小很差。下面是图像的一部分。顶部是在 MS paint 中按比例缩小的图像,底部是在 C# 中以编程方式按比例缩小的图像。我已经包含了我用来缩小和保存它的代码。有人知道会发生什么吗?

enter image description here

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/

相关文章:

arrays - 使用PIL中的fromarray保存4 channel 的图像向量,然后重新读取它

c# - 工程转C#时ApplicationEvents.vb的函数写到哪里

c# - 如何使用.NET 远程扩展环境变量?

c# - 迭代属性和嵌套的第一级属性

JavaScript Image().width 返回 0

Java GUI - JPanel、JFrame、JButton

php - 我怎样才能在网络服务器上自动生成缩略图?

c# - 无法加载 Directory.Build.props 引用的程序集

c# - 抽象类与接口(interface)的建议

jquery - 将图像链接到 <img class> 中的 URL