Resolved Help with DrawImage(Image image, int x, int y)

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
81
Programming Experience
10+
I have a Rectangle which is defined as (0,40,475,244) into which I want to draw a 128x128 Image. I am calling DrawImage as follows:

C#:
Point pImage = new Point();
pImage.X = (rPanel.Right - rPanel.Left) / 2 - this.BackgroundImage.Size.Width / 2;
pImage.Y = (rPanel.Bottom - rPanel.Top) / 2 - this.BackgroundImage.Size.Height / 2;
g.DrawImage(this.BackgroundImage, pImage.X, pImage.Y);

The resulting Image ends up drawing in the wrong place.

1613196276693.png


Should have been more like this:

1613196338589.png


I know it must be something obvious, but I can't see what's wrong with the calculation of the Image location...
 
Solution
I managed to reproduce your problem.

Cracking open the .png file, it looks like it is formatted for 72 dpi, while Windows defaults to 96 dpi. So you'll need to let Windows scale the image into the appropriate area. Something like:
C#:
var ptImage = new Rectangle()
{
    X = rect.Width / 2 - _img.Width / 2 + rect.Left,
    Y = rect.Height / 2 - _img.Height / 2 + rect.Top,
};

g.DrawImage(_img, new Rectangle(ptImage, _img.Size));

In the image below, notice the bigger image drawn by DrawImage(Image, Point), and then painted over by the DrawImage(Image, Rectangle) which is right in the green rectangle where we expected the image to be drawn into.

Screenshot_1.png

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,583
Location
Chesapeake, VA
Programming Experience
10+
I managed to reproduce your problem.

Cracking open the .png file, it looks like it is formatted for 72 dpi, while Windows defaults to 96 dpi. So you'll need to let Windows scale the image into the appropriate area. Something like:
C#:
var ptImage = new Rectangle()
{
    X = rect.Width / 2 - _img.Width / 2 + rect.Left,
    Y = rect.Height / 2 - _img.Height / 2 + rect.Top,
};

g.DrawImage(_img, new Rectangle(ptImage, _img.Size));

In the image below, notice the bigger image drawn by DrawImage(Image, Point), and then painted over by the DrawImage(Image, Rectangle) which is right in the green rectangle where we expected the image to be drawn into.

Screenshot_1.png
 
Last edited:
Solution

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
81
Programming Experience
10+
I managed to reproduce your problem.

Cracking open the .png file, it looks like it is formatted for 72 dpi, while Windows defaults to 96 dpi. So you'll need to let Windows scale the image into the appropriate area. Something like:
C#:
var ptImage = new Rectangle()
{
    X = rect.Width / 2 - _img.Width / 2 + rect.Left,
    Y = rect.Height / 2 - _img.Height / 2 + rect.Top,
};

g.DrawImage(_img, new Rectangle(ptImage, _img.Size));

In the image below, notice the bigger image drawn by DrawImage(Image, Point), and then painted over by the DrawImage(Image, Rectangle) which is right in the green rectangle where we expected the image to be drawn into.

View attachment 1380
Thanks. That works. I just need to apply that to the other Layouts and I should be good to go. Thanks for the help as always!
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,971
Location
UK
Programming Experience
10+
Please, if you can remember to ask a question, you can remember to mark your question resolved and mark the post as a solution when you've gotten the answer you want from whoever provides it.
 
Top Bottom