Visual Basic .NET Forums  

Go Back   Visual Basic .NET Forums > Components & Controls > Reporting / Printing

Reporting / Printing Discussion on output-creation components such as reporting, pdf, etc.

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-14-2009, 4:00 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jan 2009
Posts: 7
Reputation: 0
Mijul Thomas is on a distinguished programming path ahead
Default VB.Net - Printing, Graphics, Centering, and Resolutions

Microsoft Visual Studio 2008 Professional
VB.Net
Windows XP SP2


Hi,

I am coding an application to print graphics centered both vertically and horizontally on the paper.
Sounds pretty simple, right? Yes. Well, unfortunately, I have run into a problem concerning the resolution/DPI of the images. and printing

- All images must be 150 DPI.
- The graphics MUST not be resized to fit the paper.
- Graphics are not all the same dimensions. Some are 1200x1200, some 600x400, some 30x1200, and so on.
- Paper size reports to me as 850x1100 px, but the paper size will not be limited to just that.

So my question:
What algorithm or calculations should I perform to print 150 DPI graphics centered on a paper?


Here is what I currently have which would logically seem to work, but does not:

--------------------------------------------------------------------------
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim img As Image = Image.FromFile(glbImgGetFile)
Dim px As Integer = (e.PageBounds.Width - img.Width) / 2
Dim py As Integer = (e.PageBounds.Height - img.Height) / 2
e.Graphics.DrawImage(img, px, py)
End Sub

--------------------------------------------------------------------------

The above code works great for graphics with a DPI of 72 or 96, but does not work for graphics with a DPI of 150, which I need them to be in. They are shifted to the left for some odd reason...

Any help or ideas will be appreciated.


Thanks,
M. Thomas
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-14-2009, 8:54 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 36
Posts: 9,163
Reputation: 1079
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

The printing GraphicsUnit is Display, which is 1/100th inch, while the image unit is Pixel. Convert the image measurements to Display units, then you can do the logic math with comparable values. Example:
Code:
Dim img As Image = Image.FromFile("img.bmp")

'image size in 1/100 inch measurement
Dim sz As New SizeF(100 * img.Width / img.HorizontalResolution, _
                    100 * img.Height / img.VerticalResolution)

Dim p As New PointF((e.PageBounds.Width - sz.Width) / 2, _
                    (e.PageBounds.Height - sz.Height) / 2)

e.Graphics.DrawImage(img, p)

img.Dispose()
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-15-2009, 8:51 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jan 2009
Posts: 7
Reputation: 0
Mijul Thomas is on a distinguished programming path ahead
Default

Man! This works, thank you very much
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-16-2009, 8:50 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 36
Posts: 9,163
Reputation: 1079
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Good! Here's a short explanation of the measurement systems and the conversion that was done. As mentioned image unit is Pixel, the resolution (HorizontalResolution, VerticalResolution) says how many pixels there is per inch, for display devices also called PPI (pixels per inch), so the relation between these give the inch measurement. Since the numbers in PageBounds/MarginBounds is in scale 1:100 we have to multiply the image sizes by 100 to get values in same scale.

DPI relates to PPI by 1:1 for display devices, a printer can have higher resolution and use multiple ink dots per pixel.

For example page width can be 827, which is 8.27in. An image of 400x400 at 150dpi/ppi is 2.67in. wide. The values that is compared in PrintPage handler here would be 827 and 267, and the X value of the point would be (827-267)/2=280.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
center, graphics, printing, resolution, vb.net


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





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

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0


For advertising opportunities click here.