question

ToshifumiMaruki-9226 avatar image
0 Votes"
ToshifumiMaruki-9226 asked MohammadSadeghian-1513 answered

Graphics.Drawimage does not work correctly

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-api
0無題.png (23.6 KiB)
1無題.png (24.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ToshifumiMaruki-9226 avatar image
1 Vote"
ToshifumiMaruki-9226 answered

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);

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered ToshifumiMaruki-9226 commented

You must use the original size, like :

 int xoffset = 0, yoffset = 0;
 g.DrawImage(defaultBmp, xoffset, yoffset, defaultBmp.Width, defaultBmp.Height);
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

It has the same result.

This issue is affected by the size of the defaultBmp.
If the size is 160x82, it will not be reproduced.
If the size is 160x62, it will be reproduced.

0 Votes 0 ·

Where Does your 160x62 picture come from? Perhaps your 160x62 picture has problem. Could you please show a minimal, reproducible sample without private information?

0 Votes 0 ·

Hi,

It can be reproduced with the attached code without a bitmap.
116097-testcs.txt


0 Votes 0 ·
testcs.txt (1.8 KiB)
XiaopoYang-MSFT avatar image
0 Votes"
XiaopoYang-MSFT answered XiaopoYang-MSFT edited

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please cut an image of size (160, 62) from Point (0,0) of memBmp (200, 200), there are thin lines on the right and bottom side.
defaultBmp does not have this thin lines.
I don't understand why memBmp has thin lines.

0 Votes 0 ·

After we researching, we found that the strange cutting behavior is caused by scaling the orgBmp bitmap.

0 Votes 0 ·
ToshifumiMaruki-9226 avatar image
0 Votes"
ToshifumiMaruki-9226 answered

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



aftertile.jpg (3.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MohammadSadeghian-1513 avatar image
0 Votes"
MohammadSadeghian-1513 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.