Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you center an image to the print page? Thank you for your answer.
Posted
Comments
Clifford Nelson 6-Jun-12 19:38pm    
Might help if you gave information about what environment you are using. Could mean Crystal Reports, winforms, wpf.
Al Yambo 7-Jun-12 6:03am    
Its a windows application and I need to print an image with the numerical data. I have printing code and I do not use crystal reports.

1 solution

If you are using C# to print directly, then you should be using a PrintDocument[^] object to do the print.

If you look at the PrintPage event, it includes the printable area as the PrintPageEventArgs.MarginBounds, and you have the size of your image, so centring it is just a case of setting the top left hand corner of the print position to the Width of the print area minus the Width of the image, divided by two for the X, and the Height of the print area minus the Height of the image, divided by two for the Y.

Feed that into the Graphics.DrawImage method, and it will be centred.
 
Share this answer
 
Comments
Al Yambo 7-Jun-12 6:00am    
Thank you for your response. I tried what you suggested, my only problem now is that the image size is in pixels and while the print area is in inches. How do you convert from pixels to inches?
OriginalGriff 7-Jun-12 6:19am    
Check the PageUnit property - you should be able to change it to pixels.
Al Yambo 8-Jun-12 12:12pm    
Thanks. Its nice to have another alternative using PageUnit property but I already used someone elses suggestion on another post as the one below.

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

newImage.HorizontalResolution and newImage.VerticalResolution gives pixels/inch.

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