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

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
129
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
Ah! So the image not only contains the arrow, but the box that looks like window chrome around it.

Now that's really messed up if the green rectangle is that far off from the image.

Do you change any of the graphics context's translate or origin values? What happens when you add g.Transform.Reset() before drawing the image and the green rectangle?
Same result:

1613322045520.png
 
Can you upload the png file? Something is really weird here.
 
Here it is
Can you upload the png file? Something is really weird here.
Here it is... Actually any image will do. I was just checking my code to see if I could draw the Background Image centered, tiled, etc. The only ones I've been able to get to draw correctly is None and Stretch.
 

Attachments

  • CollapsePanel128.png
    CollapsePanel128.png
    16.3 KB · Views: 17
Hold on. Something is strange here. How can rPanel.Left be 0, but there is that black to blue gradient to the left of the panel? Is that rPanel inside some other control?
 
Hold on. Something is strange here. How can rPanel.Left be 0, but there is that black to blue gradient to the left of the panel? Is that rPanel inside some other control?
That's the main for, not the panel. The Panel is located at (21,22) on the form,
 
Last edited:
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
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!
 
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.
 
Back
Top Bottom