Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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


  #2 (permalink)  
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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.