none
Graphics.DrawImage Method produces incorrect size RRS feed

  • Question

  • Target .Net 3.5, Windows Forms, C#, using VS 2015 Community.

    Method: DrawImage(Image image, int x, int y, int width, int height)

    Issue:

    This method is rendering at one pixel less than the width & height specified.  I am scaling an 8x8 icon to 16x16 but the final render is 15x15.

    Graphics gfx; // Defined elsewhere
    Bitmap temp; // Defined elsewhere
    gfx.InterpolationMode = InterpolationMode.NearestNeighbor; gfx.DrawImage(temp, 0, 0, 16, 16);

    I grabbed a screenshot and confirmed that the result is one pixel off.  If I use 17, 17 for the size, it works.

    Monday, January 9, 2017 7:15 AM

All replies

  • Hi DG Osborne, 

    The following is what I referred from MSDN

    To shrink an image, groups of pixels in the original image must be mapped to single pixels in the smaller image. The effectiveness of the algorithms that perform these mappings determines the quality of a scaled image. Algorithms that produce higher-quality scaled images tend to require more processing time. In the preceding list, NearestNeighbor is the lowest-quality mode and HighQualityBicubic is the highest-quality mode.

     
    Image image = new Bitmap("GrapeBunch.bmp");
    int width = image.Width;
    int height = image.Height;
    
    // Draw the image with no shrinking or stretching.
    e.Graphics.DrawImage(
        image,
        new Rectangle(10, 10, width, height),  // destination rectangle  
        0,
        0,           // upper-left corner of source rectangle
        width,       // width of source rectangle
        height,      // height of source rectangle
        GraphicsUnit.Pixel,
        null);
    
    // Shrink the image using low-quality interpolation. 
    e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
    e.Graphics.DrawImage(
       image,
        new Rectangle(10, 250, (int)(0.6 * width), (int)(0.6 * height)),
        // destination rectangle 
        0,
        0,           // upper-left corner of source rectangle
        width,       // width of source rectangle
        height,      // height of source rectangle
        GraphicsUnit.Pixel);
    
    // Shrink the image using medium-quality interpolation.
    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
    e.Graphics.DrawImage(
        image,
        new Rectangle(150, 250, (int)(0.6 * width), (int)(0.6 * height)),
        // destination rectangle 
        0,
        0,           // upper-left corner of source rectangle
        width,       // width of source rectangle
        height,      // height of source rectangle
        GraphicsUnit.Pixel);
    
    // Shrink the image using high-quality interpolation.
    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    e.Graphics.DrawImage(
        image,
        new Rectangle(290, 250, (int)(0.6 * width), (int)(0.6 * height)),
        // destination rectangle 
        0,
        0,           // upper-left corner of source rectangle
        width,       // width of source rectangle
        height,      // height of source rectangle
        GraphicsUnit.Pixel);
    

    Please also take a look at this sample.

    >>This method is rendering at one pixel less than the width & height specified.  I am scaling an 8x8 icon to 16x16 but the final render is 15x15.

    Since you are using NearestNeighbor mode, NearestNeighbor is the lowest-quality mode. To shrink an image, groups of pixels in the original image must be mapped to single pixels in the smaller image.

    But you are just the opposite. So per my understanding, this size becomes smaller than before is expected. Please also try above sample.

    For more details, please refer to https://msdn.microsoft.com/en-us/library/k0fsyd4e(v=vs.110).aspx

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Monday, January 16, 2017 8:45 AM
    • Unproposed as answer by DG Osborne Wednesday, September 20, 2017 12:38 AM
    Tuesday, January 10, 2017 2:02 AM
  • Sorry, this answer doesn't make any sense.

    Nothing in the definition states that this method is only for shrinking the bitmap. Your example even includes the comment "// Draw the image with no shrinking or stretching." which implies that stretching (i.e. enlarging) could also be performed.

    NearestNeighbor should make stretching the image operation trivial, as I was operating on an even multiple of the original size. It works but only if you give it the wrong output by one pixel. Clearly, that is not operating as intended.

    Wednesday, September 20, 2017 12:43 AM
  • Hi, I don't know if you are still having this problem, as this post is a bit older, but I was having this exact same problem, and I found the solution is to set the PixelOffsetMode property in the Graphics object to HighQuality, like so:

    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;


    • Proposed as answer by BlueRaja Sunday, October 6, 2019 12:30 PM
    Saturday, July 21, 2018 4:46 AM