p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

Free Code from Wrox
Go Back   p2p.wrox.com Forums > C# and C > C# 2005 > C# 2005
I forgot my password
Register Now
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 computer programmers 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 programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 17th, 2006, 10: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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old September 24th, 2006, 05: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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get image to fit in print preview margin. Aves BOOK: Visual Basic 2005 Programmer's Reference 3 May 1st, 2007 12:01 PM
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 2 March 30th, 2007 03:57 AM
How to center the header image kherrerab BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 February 5th, 2007 03:48 PM
No print button on the paper with NETSCAPE elisabeth Javascript How-To 11 October 14th, 2004 03: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 02:48 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
© 2010 Wiley Publishing, Inc