Advertisement Jump to content
Sign in to follow this  

[.net] C# - GDI Points to Pixels

This topic is 3817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on an editor for our game and I noticed that when trying to draw images based on the mouse coordinates and the image width/height, that it is off by a small amount. I figure this is because the cursor position is in points and the image width/height is in pixels. Is there any easy way to convert? I haven't found any built-in functions, but I can't imagine there would be none. This has got to be a common issue.

Share this post

Link to post
Share on other sites
GDI uses pixel coordinate system by default (System.Drawing.GraphicsUnit.Pixel), so I'm not sure if this the problem here unless your changing it in code.

If your drawing to a PictureBox, then use the MouseMove or MouseDown events for the PictureBox not the Form as they are relative to the top left of the control.

Perhaps if you paste some code or give more info it might help me understand what is going wrong.

Share this post

Link to post
Share on other sites
Basically I have it set up so that you drag items from the left (the images in the image library) and drag them onto the drawing area. Here is the code for my drag function:

Image img = Image.FromFile(itemDragged.ToolTipText);

Point point = this.PointToClient(Cursor.Position);
int x = point.X;
int y = point.Y;

x = x - (img.Width / 2);
y = y - (img.Height / 2);

objectList.Add(new PlaceableObject()
FilePath = itemDragged.ToolTipText,
PosX = x,
PosY = y,

I want x and y to be /2 so that when you paste and image the mouse cursor is directly in the middle of it, but it's always off by a little. Then I call the paint method

Graphics g = e.Graphics;

foreach (var item in objectList)
Image img = Image.FromFile(item.FilePath);
g.DrawImage(img, item.PosX, item.PosY);

****Sorry I don't remember how to put code in tags :/

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!