2009-06-03 77 views
4

我使用此代码将JPG图像转换为PNG 8. 代码有效,但图像看起来有颗粒感。我做了一个导出在Photoshop中的PNG 8,它看起来更顺畅,没有粮食。为什么PngBitmapEncoder类会让我的图像看起来有颗粒感?

注意: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); 
} 
+0

你可以发布这两个图片,或其裁剪版本? – 2009-06-03 15:29:26

回答

1

我发现this library,它执行基于调色板和基于八叉树的颜色量化。

这个MSDN article解释了算法与代码之间的区别。

3

您正在使用256发色为您转换图像的固定调色板。 Photoshop可能使用专门为该图像创建的调色板,并包含其所有颜色(或大部分颜色)。

您可以使用dithering或以某种方式生成custom palette或两者。

一些更多的信息也是here

+0

我比较了Photoshop中的颜色表和右边的颜色表。有没有可以创建自定义调色板的库? – 2009-06-03 14:10:25

+0

我根本不知道C#,所以我无法帮助你。 – cube 2009-06-03 14:18:16