Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have an image I need to center on the print page. It's size is in pixels and I have to convert it first to inches. How do you convert from pixels to inches?
Posted

This can be done by using the Height and VerticalResolution of the image you're trying to print.

The following article describes this as the calculation:

C#
float imageHeightPrint = imageToPrint.Height / 
imageToPrint.VerticalResolution * 100;


http://stackoverflow.com/questions/796993/transform-pixel-height-of-image-to-printing-size-used-by-graphics-object[^]
 
Share this answer
 
Comments
Al Yambo 7-Jun-12 8:33am    
Thank you Stephen. The VerticalResolution and HorizontalResolution are all I needed as it gives the conversion in pixels/inch.
Hi try this Code, I am not sure ,hope it may work for you


CSS
Image fullSizeImg = null;
        byte[] imageBytes = Convert.FromBase64String(base64String);
        MemoryStream ms = new MemoryStream(imageBytes);
        fullSizeImg = Image.FromStream(ms, true);
        System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(700, 800, dummyCallBack, IntPtr.Zero);
        thumbNailImg.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Png);
        fullSizeImg.Dispose();
        thumbNailImg.Dispose();
 
Share this answer
 
Comments
Al Yambo 7-Jun-12 8:29am    
thank you for your response.
Thank you Stephen for giving the right answer and link. This is just a clarification post.


Image newImage = Image.FromFile("c:\\Documents\\pic.gif");

newImage.HorizontalResolution and newImage.VerticalResolution gives the conversion in pixels/inch. So if your horizontal measurement is for example 238 pixels and the horizontal resolution is 96 pixels/inch then
238 pixels*(inch/96 pixels)= 2.4792 inch. However I had to multiply by 100 (2.4792 X 100) since I guess the default units for the print page are in hundred inches. Like a normal size typewritting paper would be 850 X 1100 instead of 8.5 X 11.

I can then use this measurements and the page measurements to center the image on the print page.

as in:

// Create Point for upper-left corner of image.
Point ulCorner = new Point(xpos, ypos);

// Draw image to screen.
g.Graphics.DrawImage(newImage, ulCorner);
 
Share this answer
 
Comments
LenuIndia 4-Jul-17 13:07pm    
I am Using vb.net, I need fix measurement [width= 3.375” x Height = 2.125“]

different images here
image 1 size width = 4500, Height =1393
image 2 size width = 2081, Height =1280
image 3 size width = 1081, Height =280 etc.... But
I want fix measurement [width= 3.375” x Height = 2.125“] in vb.net code Please Help

My E-Mail is lenuthomas@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900