我正在使用此代码将 jpg 图像转换为 png 8。 代码有效,但图像看起来有颗粒感。我在 Photoshop 中导出为 png 8,它看起来更平滑且没有颗粒。
注意:8位png
有没有图像专家可以提供帮助?
我的代码:
Image ImageFile = Image.FromFile(@"C:\Documents and Settings\dvela\My Documents\Downloads\pngtest\Batman01.jpg");
Rectangle NewSize = new Rectangle();
NewSize.Width = 112;
NewSize.Height = (NewSize.Width * ImageFile.Height) / ImageFile.Width;
Bitmap image = new Bitmap(NewSize.Width, NewSize.Height);
Graphics graphics = Graphics.FromImage(image);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(ImageFile, NewSize);
FormatConvertedBitmap fcb = new FormatConvertedBitmap(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(image.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(NewSize.Width, NewSize.Height)), PixelFormats.Indexed8, BitmapPalettes.Halftone256Transparent, 0.5);
PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
pngBitmapEncoder.Interlace = PngInterlaceOption.Off;
pngBitmapEncoder.Frames.Add(BitmapFrame.Create(fcb));
using (Stream fileStream = File.Open(@"C:\Documents and Settings\dvela\My Documents\Downloads\pngtest\test.png", FileMode.Create))
{
pngBitmapEncoder.Save(fileStream);
}
最佳答案
您正在为转换后的图像使用具有 256 种颜色的固定调色板。 Photoshop 可能使用专为该图像创建的调色板,并包含图像的所有颜色(或大部分)。
你可以使用 dithering ,或者以某种方式生成 custom palette ,或两者兼而有之。
一些更多的信息也是here .
关于c# - 为什么 PngBitmapEncoder 类使我的图像看起来有颗粒感?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/944744/