Drawing on printer

J

Jack Russell

I want to draw a picture on the right hand side of the printer

so in printdocument I have
Dim newImage As Image = Image.FromFile(sLogoFile)
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(e.MarginBounds.Left +
e.MarginBounds.Width - newImage.Width, CInt(sglPrintyPos))
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)
Which I thought would align the right side of the image on the right
side of the printer, but it falls off the edge of the page.

Any ideas

Thanks

Jack Russell
 
P

Peter Proost

Hi, I think you should use the margin values of your
printdocuments.defaultpagesettings

For example get the printarea height, printarea width, x and y coordinate:

Dim intPrintAreaHeight, intPrintAreaWidth, xPrint, yPrint As Int32
With youPrintDocument.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top -
..Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left -
..Margins.Right

'X and Y coordinate of the printing rectangle
xPrint = .Margins.Left ' X coordinate
yPrint = .Margins.Top ' Y coordinate
End With

' Landscape mode => switch the values
If youPrintDocument.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If

you would need something like this:

Dim ulCorner As New Point(xPrint + intPrintAreaWidth - newImage.Width,
CInt(sglPrintyPos))
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)

Hth Greetz Peter
 
J

Jack Russell

Peter,

Thanks, it does exactly the same thing!
I assume youprintdocument is the name of the control?
Any other ideas?
Jack Russell
 
P

Peter Proost

Hi, try this, with me it works, the only thing it doesn't do is pay
attention to the images resolution.

Greetz Peter

Private WithEvents pdoc As New PrintDocument

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(34, 32)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Print"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(34, 62)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 1
Me.Button2.Text = "Page Setup"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(584, 353)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim psd As New PrintDialog
psd.Document = pdoc
If psd.ShowDialog = DialogResult.OK Then
pdoc.PrinterSettings = psd.PrinterSettings
End If
Dim ppd As New PrintPreviewDialog
Try
ppd.Document = pdoc
ppd.WindowState = FormWindowState.Maximized
ppd.ShowInTaskbar = False
ppd.ShowDialog()
Catch exp As Exception
MessageBox.Show("An error occurred while trying to load the " &
_
"document for Print Preview. Make sure you currently have "
& _
"access to a printer. A printer must be connected and " & _
"accessible for Print Preview to work.", Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim psd As New PageSetupDialog
With psd
.Document = pdoc
.PageSettings = pdoc.DefaultPageSettings
End With

If psd.ShowDialog = DialogResult.OK Then
pdoc.DefaultPageSettings = psd.PageSettings
End If
End Sub

Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage


Dim intPrintAreaHeight, intPrintAreaWidth, xPrint, yPrint As Int32
With pdoc.DefaultPageSettings

intPrintAreaHeight = .PaperSize.Height - .Margins.Top -
..Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left -
..Margins.Right


xPrint = .Margins.Left ' X coordinate
yPrint = .Margins.Top ' Y coordinate
End With

' If the user selected Landscape mode, swap the printing area height
' and width.
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If

Dim newImage As Image = Image.FromFile("c:\1.jpg")
Dim ulCorner As New Point((xPrint + intPrintAreaWidth) -
newImage.Width, yPrint)
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)


e.HasMorePages = False


End Sub
 
P

Peter Proost

Hi Jack,

can you try this code, it calculates the page and image width in centimeters
keeping track of the image resolution. Then it calculates where the image
should be drawn so that the right sides are aligned. You can try it with
with to images of the same pixel size but different resolutions to see if it
works.

PS: you can ofcourse change the code so it works in inches but for me it was
easier to do it in centimeters :)

Hth Greetz Peter

Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Try
Dim intPrintAreaHeight, intPrintAreaWidth As Int32
Dim xPrint, yPrint As Double

With pdoc.DefaultPageSettings

intPrintAreaHeight = ((.PaperSize.Height / 100) * 2.54) -
((.Margins.Top / 100) * 2.54) - ((.Margins.Bottom / _ 100) * 2.54)
intPrintAreaWidth = ((.PaperSize.Width / 100) * 2.54) -
((.Margins.Left / 100) * 2.54) - ((.Margins.Right / 100) _ * 2.54)

xPrint = ((.Margins.Left / 100) * 2.54) ' X coordinate
yPrint = .Margins.Top ' Y coordinate doesn't need to be
recalculated in this scenario
End With

' If the user selected Landscape mode, swap the printing area
height
' and width.
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If

Dim newImage As Image = Image.FromFile("c:\1.jpg")
Dim realImageWidth As Double
realImageWidth = (newImage.Width /
newImage.HorizontalResolution) * 2.54
realImageWidth = (xPrint + intPrintAreaWidth) - realImageWidth
realImageWidth = (realImageWidth * 100) / 2.54

Dim ulCorner As New Point(realImageWidth, yPrint)
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)

yPrint += newImage.Height + 10
newImage.Dispose()
newImage = Image.FromFile("c:\2.jpg")

realImageWidth = (newImage.Width /
newImage.HorizontalResolution) * 2.54
realImageWidth = (xPrint + intPrintAreaWidth) - realImageWidth
realImageWidth = (realImageWidth * 100) / 2.54

ulCorner = New Point(realImageWidth, yPrint)
e.Graphics.DrawImage(newImage, ulCorner)

e.HasMorePages = False
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub
 
J

Jack Russell

Peter,

I did reply but it seems to have got lost

so

I said

Thanks Peter that seems to work perfectly apart from why did you print
the image twice??

Jack
 
P

Peter Proost

I didn't print it twice, it where two the same images equally big in pixels
but with different resolutions so they differed in centimeters/inch size. I
used it to test if my code worked ok.

Greetz Peter
 
J

Jack Russell

Thanks anyway, it seems to work well

Peter said:
I didn't print it twice, it where two the same images equally big in pixels
but with different resolutions so they differed in centimeters/inch size. I
used it to test if my code worked ok.

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.



if it


2.54) -
 
P

Peter Proost

You're welcome,

you only have to be carefull when for example you want to print an image
that is bigger than your page. You could resize you're image or warn the
user the image is to large or print the image over multiple pages.

Greetz Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top