Graphics.Drawimage does not work correctly

Toshifumi Maruki 101 Reputation points
2021-07-08T10:41:08.267+00:00

Hi

I have the following code.

Bitmap memBmp;  

defaultBmp.Save("default.jpg" , System.Drawing.Imaging.ImageFormat.Jpeg );  

memBmp = new Bitmap(width, height);  // witdth > defaultBmp.Width and height > defaultBmp.Height  
Graphics g = Graphics.FromImage(memBmp);  

SolidBrush brush = new SolidBrush(Color.Black);  
rect = new Rectangle(0, 0, memBmp.Width, memBmp.Height);  
g.FillRectangle(brush, rect);  
brush.Dispose();  

g.DrawImage(defaultBmp, new Point(xoffset, yoffset);  

memBmp.Save("after.jpg", System.Drawing.Imaging.ImageFormat.Jpeg );  

default.jpg
112994-0%E7%84%A1%E9%A1%8C.png

after.jpg
112975-1%E7%84%A1%E9%A1%8C.png

after.jpg has thin line at right and under side.
Why ?

Windows development Windows API - Win32
0 comments No comments
{count} votes

Accepted answer
  1. Toshifumi Maruki 101 Reputation points
    2021-08-09T22:20:12.647+00:00

    The solution to this problem was to specify the wrap mode as follows:

            // Create a default bitmap.
            Bitmap defaultBmp = new Bitmap(160,62);
            g = Graphics.FromImage(defaultBmp);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            System.Drawing.Imaging.ImageAttributes wrapMode = new System.Drawing.Imaging.ImageAttributes();
            wrapMode.SetWrapMode(System.Drawing.Drawing2D.WrapMode.Tile);
            g.DrawImage(orgBmp,
                        new Rectangle(0, 0, defaultBmp.Width, defaultBmp.Height),
                            0, 0, orgBmp.Width, orgBmp.Height,
                            GraphicsUnit.Pixel, wrapMode);
    
    1 person found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2021-07-08T11:34:30.183+00:00

    You must use the original size, like :

    int xoffset = 0, yoffset = 0;
    g.DrawImage(defaultBmp, xoffset, yoffset, defaultBmp.Width, defaultBmp.Height);
    

  2. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2021-07-21T05:46:33.52+00:00

    According to the attached code, the painted bitmap defaultBmp(160, 62) is drawn on memBmp(200, 200) at Point(0, 0) without scaled. I’m Sorry, Any questions?

    DrawImage(Image, Point):Draws the specified Image, using its original physical size, at the specified location.


  3. Toshifumi Maruki 101 Reputation points
    2021-07-26T03:28:30.32+00:00

    An easy way to understand this problem is to tile the defaultBmp into a memBmp. 117756-aftertile.jpg

    0 comments No comments

  4. Mohammad Sadeghian 1 Reputation point
    2022-04-21T20:04:57.477+00:00

    I see the same issue.
    I have a general purpose app that resizes a source bitmap to a PictureBox control that is scaled and depending on scale factor sometime the last row and sometime the last column (and some time both) are not rendered to destination bitmap. InterpolationMode does not have any effect. Debugger data indicate that all my data is correct.
    Copying pixel data manually works as expected.
    This is the call from C#:
    graphics.DrawImage(sourceBitmap, dstRect, srcRect, GraphicsUnit.Pixel);

    and this is relevant data from VS debugger:
    sourceBitmap.Width 15 int
    sourceBitmap.Height 8 int

    • srcRect {X = 0 Y = 0 Width = 15 Height = 8} System.Drawing.Rectangle
    • dstRect {X = 0 Y = 0 Width = 480 Height = 256} System.Drawing.Rectangle
    • currentView.ClientSize {Width = 480 Height = 256} System.Drawing.Size
      scale 32 float
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.