Why does the PngBitmapEncoder class make my images grainy?

I use this code to convert a jpg image to png 8. The code works, but the image looks grainy. I exported to Photoshop as png 8, and it looks smoother and without grain.

Note : 8-bit png

Any image gurus that might help?

My code is:

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); } 
+4
source share
2 answers

I found this library which uses color quantization based on Palette and Octree.

This MSDN article explains the difference between algorithms with code.

+1
source

You are using a fixed palette with 256 colors for your converted image. Photoshop probably uses a palette created specifically for this image and contains all its colors (or most of them).

You can use dithering or somehow create a custom palette or both.

more information is also here .

+3
source

Source: https://habr.com/ru/post/1285665/


All Articles