GDI+ DrawImage(Bitmap, ...) stretch problem
I am rendering a bitmap where each pixel may be stretched to occupy many
screen pixels. I need the solid color of each pixel to be preserved, not
dithered or interpolated in any way.
My sample code is this:
g.ResetTransform();
Bitmap bmDemo = new Bitmap(2, 2, PixelFormat.Format32bppArgb);
int Alpha = 255;
bmDemo.SetPixel(0, 0, Color.FromArgb(Alpha, Color.White));
bmDemo.SetPixel(1, 0, Color.FromArgb(Alpha, Color.Red ));
bmDemo.SetPixel(0, 1, Color.FromArgb(Alpha, Color.Red ));
bmDemo.SetPixel(1, 1, Color.FromArgb(Alpha, Color.White));
g.DrawImage(bmDemo, new Rectangle( 5, 5, 2, 2)); // OK, no stretch
g.DrawImage(bmDemo, new Rectangle( 8, 8, 4, 4)); // not OK
g.DrawImage(bmDemo, new Rectangle(14, 14, 10, 10)); // not OK
The first DrawImage() does not stretch and the 4 rendered pixels are as they should be.
The second and third DrawImage() stretch the 2x2 bitmap into 4x4 and 10x10 on the screen. Both generate some pixels that are white, some that are red and many others that are a combination of red/white, red/black (black is my background color), white/black or red/white/black.
In each case, I want all 4 bitmap pixels to be rendered as either red or
white, no matter how many screen pixels they occupy.
I have tried all the Graphics, Bitmap, and InterpolationMode options that I
can find but have not found the right combination.
I have a working implmentation using WIN32 GDI calls, principally StretchDIBits(). I need the same behavior in GDI+.
Thanks ...
Don Walker
Re: GDI+ DrawImage(Bitmap, ...) stretch problem
Hi,
Try nearestneighbor interpolation mode if you're using .net 2.0
Code:
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
Re: GDI+ DrawImage(Bitmap, ...) stretch problem
Luthv,
Great idea. I am using .Net1.1 but tried it anyway. Each pixel came out as a solid color (no dithering) but was shifted as follows for the third DrawImage(), the one that stretches the 2x2 bitmap to 10x10:
Pixel 0,0 (white) 3x3 in the upper left corner.
Pixel 1,0 (red) 5x3 to the right of the 0,0 pixel
Pixel 0,1 (red) 3x5 beneath 0,0 pixel
Pixel 1,1 (white) 5x5 under and to the right of the two red pixels
The remaining 2 screen pixels to the left and below the 8x8 bitmap are black, the color of my background.
This is much closer to what I am looking for but not technically accurate. I need (for the third DrawImage()) all 4 pixels to be 5x5 and positioned accurately on the screen at 14,14 through 23,23.
Do you think .Net 2.0 will behave more accurately? In the mean time, I will try to find a .Net 2.0 (or even Vista) machine around here and try it myself.
Thanks again.
Don
Re: GDI+ DrawImage(Bitmap, ...) stretch problem
Nope, .Net 2.0 have the same problem, so I guess you better use StrectchDIBits with some P/Invoke rather than relying on the .NET interpolation
Re: GDI+ DrawImage(Bitmap, ...) stretch problem
Luthv,
On another Forum, I found a solution which appears to work for me - check it out. This is a known bug but apparently Microsoft has chosen to put it on the back burner. I tried my code on .Net 2.0 under Vista yesterday - same problem and the solution does work there too.
Again, thanks for the reply.
Don
http://codemonkeyjas.blogspot.com/20...-with-gdi.html
Re: GDI+ DrawImage(Bitmap, ...) stretch problem
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half