print a .jpg image from vb .net

B

Brad Pears

I have the following code which I was using to print .pdf's. It worked fine
and actually printed the pdf file.

Now I want to use the same code to print a .jpg file. When I run the code,
it launches the 'Windows Picture and Fax viewer application' and DOES NOT
print the actual image.

Is there a tweak to this code or is there some other code I can use to print
a specified .jpg file directly from within my .net application???

PS.. I hardcoded the image name in here for testing purposes only and I am
simply using teh default printer.

Here is the code..
--------------------------------------------------------
Dim iCopies As Integer

Dim x As Integer

' Ask user for number of copies they want printed

iCopies = CInt(InputBox("Number of Copies: ", "Print Steel Plate Drawing",
"3"))

Dim psi As New ProcessStartInfo()

For x = 1 To iCopies

With psi

..Verb = "print"

..WindowStyle = ProcessWindowStyle.Hidden

..FileName = "C:\image.jpg"

..UseShellExecute = True

End With

Process.Start(psi)

Next x





Thanks, Brad
 
N

Newbie Coder

Brad,

Why not use the PrintDocument control..? You can then pass the name etc.
 
B

Brad Pears

Hmmm, every time I see sample code on this control, it has never ever showed
being able to simply pass a document name to the control.

I will look closer at it though... Do you have any source code that
specifically shows how to go about printing an existing document using that
control?? If so, I would love to see it!!

THanks, Brad
 
W

William LaMartin

This code should do it. However, the printed image may be distorted if you
don't first resize it to fit the target page size.

Private Sub PrintImage_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PrintImage.Click
If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK
Then
Try
AddHandler Me.PrintDocument1.PrintPage, AddressOf
Me.GraphicPrint
Me.PrintDocument1.Print()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub

Private Sub GraphicPrint(ByVal sender As Object, ByVal e As
System.Drawing.Printing.PrintPageEventArgs)
Dim imagePrint As Image = Image.FromFile(Path to the File)
'Here you really need to put in some code to resize image to fit
paper and use either landscape of portriat.
e.Graphics.DrawImage(imagePrint, 50, 50, imagePrint.Width,
imagePrint.Height)
e.HasMorePages = False
End Sub
 
B

Brad Pears

That does work...

However, I am so new to this, any chance you could either give me or point
me to some sample code to actually set a printer to landscape/portrait (or
better yet - use the pagesetupdialog control to do all this) and to resize
an image to fit???

Thanks, Brad
 

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