Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 17th, 2006, 09:49 PM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default how to print image at center of paper EXACTLY

I want to print image file at the center of paper exactly.
How to do this? I am confused with the world, page and device coordinate manipulation. Please give me the simplest example in C# to accomplish my goal.

Thanks

GunTank


 
Old September 24th, 2006, 04:43 AM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default

After struggling in a couple of days, I find the best solution for it without having to call P/Invoke (to obtain hard margin) as other people did. PageSettings exposes HardMargin to ease our life.

Since e.MarginBounds is measured relative from upper left corner of printable area (rather than the edge of actual page), we must move e.MarginBounds using HardMargin point as an offset.

Note that: both MarginBounds and HardMargin are measured in hundredths of an inch.

private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    g.PageUnit = GraphicsUnit.Inch;

    Image img = new Bitmap(@"c:\Sunset.jpg");
    Graphics gg=Graphics.FromImage(img);
        
        
    RectangleF marginBounds = e.MarginBounds;
    if(!pd.PrintController.IsPreview)
        marginBounds.Offset(-e.PageSettings.HardMarginX,
                        -e.PageSettings.HardMarginY);

    float x = marginBounds.X / 100f +
                    (marginBounds.Width / 100f -
                        (float)img.Width / gg.DpiX) / 2f;
    float y = marginBounds.Y/ 100f +
                    (marginBounds.Height / 100f -
                        (float)img.Height / gg.DpiY) / 2f;
        


    g.DrawImage(img,x,y);

    //Don't call g.Dispose(). Operating System will do this job.
    gg.Dispose();//You should call it to release graphics object immediately.
}


Andi Setiawan
visual_cpp@programmer.net




Similar Threads
Thread Thread Starter Forum Replies Last Post
How to center text/image/any object into a div beetle_jaipur BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 3 January 24th, 2013 01:25 PM
How to center the header image kherrerab BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 May 29th, 2010 11:51 PM
No print button on the paper with NETSCAPE elisabeth Javascript How-To 11 October 14th, 2004 02:59 AM
print out on a paper hosefo81 PHP How-To 4 February 11th, 2004 04:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.