Problems printing a jpg file

jburke

New member
Joined
Jun 5, 2010
Messages
4
Programming Experience
5-10
Am having problems printing a stored jpg file. I can load it rotate it save it but when it comes to printing it i just get a small portion of the image. . Note opening the saved portion file "rotated.jpg" contains the entire image. Here is the code


Imports System.Drawing.Printing
Imports System.Drawing.Image

Public Class Form1

Private WithEvents DialogsPrintDocument As PrintDocument

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub DialogsPrintDocument_QueryPageSettings(ByVal sender As Object, ByVal e As _
System.Drawing.Printing.QueryPageSettingsEventArgs) _
Handles DialogsPrintDocument.QueryPageSettings

e.PageSettings.Landscape = True

End Sub

Private Sub DialogsPrintDocument_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles DialogsPrintDocument.PrintPage

Dim img As Image

img = Image.FromFile("C:\xxx.jpg")

img.RotateFlip(RotateFlipType.Rotate180FlipNone)

e.HasMorePages = False
e.Graphics.DrawImage(img, New Point(0, 0))
img.Save("C:\rotated.jpg")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

DialogsPrintDocument = New PrintDocument

With PrintDialog1

.AllowCurrentPage = False
.AllowPrintToFile = False
.AllowSelection = False
.AllowSomePages = False
.Document = DialogsPrintDocument
.PrinterSettings.DefaultPageSettings.Margins.Top = 25
.PrinterSettings.DefaultPageSettings.Margins.Bottom = 25
.PrinterSettings.DefaultPageSettings.Margins.Left = 25
.PrinterSettings.DefaultPageSettings.Margins.Right = 25
End With

If PrintDialog1.ShowDialog = DialogResult.OK Then

DialogsPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings
strPrintRecord = TextBox1.Text

DialogsPrintDocument.Print()

End If
End Sub
End Class
 
try .DrawImage(img, e.MarginBounds), adjust the rectangle if you need it scaled.
Also remember; img.Dispose()
 
Well that printed but this time its a full page. I am looking to print the actual size of the image. Thanks for the line
img.dispose() forgot about that one.
 
If the image is smaller than page specifying a Point as your first code is sufficient to draw the full image. If the image is larger than page you either have to draw it within a rectangle in the page (similar to that I suggested), or draw it over multiple pages. For multipage printout you have to work out those rectangles and use a DrawImage method where you both specify source and target rectangle, regarding PrintPage you also have to work with e.HasMorePages to control when another page needs to output.
 
Back
Top