Reputation: 39
I need to copy a little Bitmap that I generated to a larger object (say PictureBox) so I'm using DrawImage to do that but the result duplicate is blurry as if GDI+ is trying to scale the image except that both PageUnits are identical, the DPI resolution is identical, the source and destination rectangles are the same width and height.
If the source and destination X,Y points are 0,0 then DrawImage works perfectly, but if I change the destination X.Y position to anything else (even though width and height are the same) then the result is blurry.
I've tried every overload of DrawImage and the results are always the same. Unless the origin and destination X,Y positions are identical, the image will be blurry.
private void DrawStringVerticalStackingV4(string text, Graphics g, SizeF sizeMaxArea, PointF startPoint, Font font, Brush brush, StringFormat sf)
{
Bitmap bmp = new Bitmap(100, 10, g);
Graphics gBmp = Graphics.FromImage(bmp);
gBmp.PageUnit = g.PageUnit;
// Some stuff being drawn...
// Draw the bitmap on the original canvas
RectangleF destRect = new RectangleF(startPoint.X, startPoint.Y, gBmp.VisibleClipBounds.Width, gBmp.VisibleClipBounds.Height);
RectangleF srcRect = new RectangleF(0, 0, gBmp.VisibleClipBounds.Width, gBmp.VisibleClipBounds.Height);
g.DrawImage(bmp, destRect, srcRect, g.PageUnit);
// Housekeeping
}
Unless destRect and srcRect have the same X and Y, the result is ever so slightly distorted as if GDI+ is trying to scale the image.
Upvotes: 1
Views: 310
Reputation: 39
After playing around with this further, Hans Passant was correct. Setting Graphics.InterpolationMode to NearestNeighbour worked perfectly.
This issue only occurs if your PageUnit is anything else but Pixels. My little brain considers this a logical bug because both source and destination rectangles had the exact same width and height. The only thing that was different was the destination X and Y positions. No distortion should have occurred.
Thank you very much for the help!
Upvotes: 2