none
Bitmap scaling issues RRS feed

  • Question

  • I'm trying to re-size and rotate a bitmap using the following c# code

    Point[] Corners = new Point[3];
    Size NewSize = new Size();
    ...
    Bitmap OrgBitmap = new Bitmap(ImageFilePath);
    Bitmap ScaledBmp = new Bitmap(NewSize.Width, NewSize.Height);
    Graphics DestSurface = Graphics.FromImage(ScaledBmp);
    DestSurface.DrawImage(OrgBitmap, Corners);

    Or alternatively simply scale using the following code:

    Bitmap OrgBitmap = new Bitmap(ImageFilePath);
    Bitmap ScaledBmp = new Bitmap(OrgBitmap, NewSize.Width, NewSize.Height);

    The original image has a white background. The white in the scaled image is then replaced with red. The scaling/rotating works fine except there's a non-white contour around the scaled image. This contour did not occur when I was using c++ StretchBlt(). 

    Below is a sample image of the output

    1- The original bitmap

    2- The original bitmap with background filled with red

    3- The scaled bitmap using above code with filled background. Contour can be seen.

    4- The scaled bitmap using StretchBlt() with background filled red. No contour.

    Any ideas? I'm running windows 8. It's remarkable that MS paint produce the same non-white contour when the image is scaled using the resize and skew dialog box, but produce no contour when the image is scaled by selecing the image and dragging the selection box to resize.

    Any help is appreciated.


    • Edited by MagsB Thursday, October 3, 2013 11:38 AM
    Thursday, October 3, 2013 4:26 AM

Answers

  • Here's how I clear it

    DestSurface.FillRectangle(new SolidBrush(BkgColor), 0, 0, NewSize.Width, NewSize.Height);

    Thanks for your help. I found how to produce sharp edges:

    DestSurface.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

    • Marked as answer by MagsB Thursday, October 3, 2013 2:09 PM
    Thursday, October 3, 2013 12:23 PM

All replies

  • Clear the ScaledBmp before drawing the OrgBitmap.  Modify the Graphics properties to achieve your desired result and then use DrawImage.
    • Edited by JohnWein Thursday, October 3, 2013 7:48 AM
    Thursday, October 3, 2013 7:48 AM
  • I do clear the scaled bitmap first. What graphic properties you mean?

    I inserted a sample image of the output in the original post above


    • Edited by MagsB Thursday, October 3, 2013 11:47 AM
    Thursday, October 3, 2013 11:33 AM
  • "I do clear the scaled bitmap first."

    Post complete code.

    Thursday, October 3, 2013 12:05 PM
  • Here's how I clear it

    DestSurface.FillRectangle(new SolidBrush(BkgColor), 0, 0, NewSize.Width, NewSize.Height);

    Thanks for your help. I found how to produce sharp edges:

    DestSurface.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

    • Marked as answer by MagsB Thursday, October 3, 2013 2:09 PM
    Thursday, October 3, 2013 12:23 PM