Avoiding "dithering" when using DrawImage
Posted on 2004-08-17
I'm using the GDI to slice an image into bits. The code I use is show below. The problem is that even if I start with an image filled with a single websafe colour, when I slice an area off, the resulting image is no longer a solid colour, it has little flecks in it. Consequently, when I display the slices, they don't match the single-colour background.
I'm not well up on graphics, so I'd appreciate any ideas or comments as to what's going on. I've tried it with GIF, JPEG and PNG images and get the same results. I tried changing "PixelFormat.Format24bppRgb" to an 8-bit setting, but it complained that it was "indexed". I've commented out the "'grPhoto.InterpolationMode" stuff, but that made no difference.
Private Function Slice(ByVal imgPhoto As Image, ByVal FromX As Integer, ByVal FromY As Integer, _
ByVal nWidth As Integer, ByVal nHeight As Integer) As Image
Dim bmPhoto As Bitmap = New Bitmap(nWidth, nHeight, PixelFormat.Format24bppRgb)
Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
'grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim rectSlice As Rectangle = New Rectangle
rectSlice.X = FromX
rectSlice.Y = FromY
rectSlice.Height = nHeight
rectSlice.Width = nWidth
grPhoto.DrawImage(imgPhoto, 0, 0, rectSlice, GraphicsUnit.Pixel)