Windows Develop Bookmark and Share   
 index > Windows Forms General > Image size wrong
 

Image size wrong

Hi there,

I am creating a customisable user interface that uses images as buttons etc. However when I just use the DrawImage function without including the image size it draws it at double the size, I thought I should draw it at its default size?

I then tried including the image size using the myImage.Width and Height, but still gets drawn double the size! I then looked at the values that were getting returned for the Width and height and it is returning the wrong size!

Does anyone know why the image might be returning double the actual size? (just so you know the actual image H&W is 52x52, but its getting drawn at 104x104)

Here is my code

Code Snippet

public Form1()

{

InitializeComponent();

butMusic = new Button(@"C:\Documents and Settings\Chris\My Documents\Visual Studio 2008\Projects\PFPC\PFPC\PFPC\interface\Music_Button.jpg", "Music", 100, 100); // 100 values are location

}

class Button

{

private float x, y; // Image location

private Image image; // Image

private String text; // Image text

public Button(string imagePath, string imageText, float x, float y)

{

this.image = Image.FromFile(imagePath);

this.x = x;

this.y = y;

this.text = imageText;

}

public void Draw(Graphics dc)

{

float imageCentre; // Get image centre - relative to position on screen

sFormat.Alignment = StringAlignment.Center; // Centre it when drawn where i place it

dc.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

//image centre

imageCentre = x + (image.Width / 2);

dc.DrawImage(image, x, y);//, image.Width, image.Height);

dc.DrawString(this.text, textFont, textBrush, imageCentre, y + image.Height + 2, sFormat);

}

}

Many thanks

Grovesy  Sunday, February 03, 2008 10:37 PM

Hi,

You didn’t understand how the Graphics.DrawImage Method (Image, Single, Single) works. Please read the following remarks in the document:

The DrawImage method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call DrawImage to draw that image on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image will be (216/72)*96 = 288.

That overload draws image in physical size (inches), you need to supply the width and height parameters in order to draw it in pixels.

Best Regards

Chunsheng Tang

Chunsheng Tang  Tuesday, February 05, 2008 4:21 AM
Ok you gave me the direction i needed to find the answer. I found this Microsoft page which describes whats going on and how to get the actual pixel size:

http://support.microsoft.com/kb/317174



Just seems a little convoluted and I'm surprised there is no method or property to get this directly.

I haven't actually tried the above yet but this should work.

Cheer
Chris
Grovesy  Tuesday, February 05, 2008 9:33 AM
Any ideas anyone? Am i doing something stupidly wrong?

Cheers
Grovesy  Monday, February 04, 2008 11:10 AM

Hi,

You didn’t understand how the Graphics.DrawImage Method (Image, Single, Single) works. Please read the following remarks in the document:

The DrawImage method draws an image using its physical size, so the image will have its correct size in inches regardless of the resolution (dots per inch) of the display device. For example, suppose an image has a pixel width of 216 and a horizontal resolution of 72 dots per inch. If you call DrawImage to draw that image on a device that has a resolution of 96 dots per inch, the pixel width of the rendered image will be (216/72)*96 = 288.

That overload draws image in physical size (inches), you need to supply the width and height parameters in order to draw it in pixels.

Best Regards

Chunsheng Tang

Chunsheng Tang  Tuesday, February 05, 2008 4:21 AM
Oh right I see. Is this also true for the Image Height and Width properties? i mean do they return the physical size, not the pixel size? As when I used these properties they also appeared incorrect to the pixel size!

How can I get an images pixel size at runtime? as i don't want to hard code these values.

Many thanks
Grovesy  Tuesday, February 05, 2008 9:06 AM
Ok you gave me the direction i needed to find the answer. I found this Microsoft page which describes whats going on and how to get the actual pixel size:

http://support.microsoft.com/kb/317174



Just seems a little convoluted and I'm surprised there is no method or property to get this directly.

I haven't actually tried the above yet but this should work.

Cheer
Chris
Grovesy  Tuesday, February 05, 2008 9:33 AM

You can use google to search for other answers

Custom Search

More Threads

• How to show Data on transparent prompt window when the mouse move on the DataGridView cell
• keycode
• ProgressBar Properties
• Removing password from excel file in c#
• Finding rowState ...?
• App takes time to load after restoring from minizied state
• Loading a resource-intensive form
• Complex properties in the PropertyGrid
• bind datagrid to object
• get pictures from server in windows form