שתף באמצעות


vb 2012 print an image file

Question

Tuesday, April 8, 2014 12:56 PM

  • I am using VB 2012 on a windows 8 computer.  I want to print a .jpg file to the default printer.  I have the complete file name of the image.  It should be a simple process to simply send it to print, but I cannot seem to find a mechanism.  I see some examples using printpage, but I don't think they apply to a single image file.

    What am I missing?


RONATMOODYLAKE

14 hours 35 minutes ago

Reply

|

Quote

|

RONATMOODYLAKE

10 Points

All replies

  • 0

    I made some progress by using a picturebox containing the image with the following code (PictureBoxIdle is the complete image file path):

    PD.print()

        Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage
            e.Graphics.DrawImage(PictureBoxIdle.Image, e.MarginBounds.Left, e.MarginBounds.Top)
        End Sub

    Now I would like to take advantage of the image file characteristics and stretch the print image to fit an 8 1/2 x 11 page without losing image quality.


RONATMOODYLAKE

RONATMOODYLAKE

All replies (10)

Wednesday, April 9, 2014 12:23 AM ✅Answered

Found it myself!

I happened to see a note (this note should be in bold!) indicating that thePrintDocumentImageDisplaySettings donot take effect while the document is printing!!

Therefore the following code works:

    Private Sub PrintThisPictureToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintThisPictureToolStripMenuItem.Click
        If PictureBoxImage.Image.Height > PictureBoxImage.Image.Width Then
            PrintDocumentImageDisplay.DefaultPageSettings.Landscape = False
        Else
            PrintDocumentImageDisplay.DefaultPageSettings.Landscape = True
        End If
        PrintDocumentImageDisplay.Print()
    End Sub

    Private Sub PrintDocumentImageDisplay_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocumentImageDisplay.PrintPage
        Dim scale_width As Integer, scale_height As Integer
        If PictureBoxImage.Image.Height > PictureBoxImage.Image.Width Then
            scale_height = e.PageBounds.Height
            scale_width = (e.PageBounds.Height * PictureBoxImage.Image.Width) / PictureBoxImage.Image.Height
        Else
            scale_width = e.PageBounds.Width
            scale_height = (e.PageBounds.Width * PictureBoxImage.Image.Height) / PictureBoxImage.Image.Width
        End If
        Dim g As Graphics = e.Graphics
        Dim img As Image = PictureBoxImage.Image
        e.Graphics.DrawImage(img, 0, 0, scale_width, scale_height)
    End Sub

This example should be of significant value to anyone who wants to print the image in a picturebox!

RONATMOODYLAKE


Wednesday, April 9, 2014 1:06 AM ✅Answered | 1 vote

Good. You found it before I could post.

Here is another example. This prints the picture box image fitted to the full page maintaining the ratio.

Note I think you should use e.Graphics.VisibleClipBounds.Width etc as PageBounds includes the non-printable area of the page. VisibleClipBounds is the printable area.

Public Class Form5
    Dim PrintPicture As Bitmap    Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Image = Image.FromFile("c:\bitmaps\rusty.jpg")
        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        RadioButton1.Checked = True
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            PrintPicture = PictureBox1.Image

            Dim pd As New System.Drawing.Printing.PrintDocument
            AddHandler pd.PrintPage, AddressOf OnPrintPage

            pd.DefaultPageSettings.Landscape = RadioButton2.Checked
            pd.Print()

        Catch ex As Exception
            MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub
    Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim l, t, w, h As Integer
        Dim ratio As Single = PrintPicture.Width / PrintPicture.Height
        Dim ratiop As Single = e.Graphics.VisibleClipBounds.Width / e.Graphics.VisibleClipBounds.Height

        'size to fit printer page
        If ratio > ratiop Then
            w = e.Graphics.VisibleClipBounds.Width
            h = w / ratio
            t = (e.Graphics.VisibleClipBounds.Height / 2) - (h / 2)
            Dim scale_height As Integer = e.PageBounds.Height
        Else
            h = e.Graphics.VisibleClipBounds.Height
            w = h * ratio
            l = (e.Graphics.VisibleClipBounds.Width / 2) - (w / 2)
        End If

        'now print the image
        e.Graphics.DrawImage(PrintPicture, l, t, w, h)

    End Sub
End Class


Monday, April 7, 2014 10:19 PM

I am using VB 2012 on a windows 8 computer.  I want to print a .jpg file to the default printer.  I have the complete file name of the image.  It should be a simple process to simply send it to print, but I cannot seem to find a mechanism.  I see some examples using printpage, but I don't think they apply to a single image file.

What am I missing?

RONATMOODYLAKE


Monday, April 7, 2014 10:59 PM

I made some progress by using a picturebox containing the image with the following code (PictureBoxIdle is the complete image file path):

PD.print()

    Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage
        e.Graphics.DrawImage(PictureBoxIdle.Image, e.MarginBounds.Left, e.MarginBounds.Top)
    End Sub

Now I would like to take advantage of the image file characteristics and stretch the print image to fit an 8 1/2 x 11 page without losing image quality.

RONATMOODYLAKE


Tuesday, April 8, 2014 10:58 AM

I suggest you to post in VB.NET forum, because this forum is for development of Office, but your issue is not related to Excel.


Tuesday, April 8, 2014 9:40 PM | 1 vote

It seems to me the standard e.graphics.DrawImage works about as good as it's going to get.

However I tested the code below. In the "PrintDocument1.PrintPage" event to print normally then print Bmp1. Or comment out that line and comment in the line for printing Bmp2. And prior to printing Bmp2 use Button3 (Use Resize) to resize Bmp1 into Bmp2 for Bmp2s size in order to print Bmp2. Then use Button1 to print it. And see if you see a difference in PrintPreview between the two images.

In the image below I didn't see a difference between the two pics. The top pic was Bmp1 in PrintPreview at 100% and the bottom pic was Bmp2 in PrintPreview at 100%. The size of 770 x 1020 printing at start point 40, 40 seemed to provide even space around the image to be printed. The original image in My.Resources is 225 x 225 pixels.

Option Strict On

Imports System.Drawing.Drawing2D

Public Class Form1

    Dim Bmp1 As Bitmap
    Dim Bmp2 As Bitmap

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
        Bmp1 = New Bitmap(My.Resources.Crossbones_BMP)
        Label1.Text = "Waiting"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = "My.Resources.Crossbones_BMP" & vbCrLf & "          original size = width " & My.Resources.Crossbones_BMP.Width.ToString & _
            ", height " & My.Resources.Crossbones_BMP.Height.ToString & vbCrLf & "          new size =      width 700, height 700."
        PrintDocument1.DefaultPageSettings.PaperSize = New Printing.PaperSize("Letter", 850, 1100)
        PrintDocument1.Print()
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
        e.Graphics.PixelOffsetMode = PixelOffsetMode.Half
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected
        e.Graphics.CompositingMode = CompositingMode.SourceCopy
        e.Graphics.DrawImage(Bmp1, 40, 40, 770, 1020) ' Use this for printing Bmp1
        'e.Graphics.DrawImage(Bmp2, 40, 40, 770, 1020) ' Use this for printing Bmp2
        PrintPreviewDialog1.Document = PrintDocument1
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Bmp2 = New Bitmap(770, 1020)
        Using graphicsHandle As Graphics = Graphics.FromImage(Bmp2)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(Bmp1, 0, 0, 770, 1020)
        End Using
    End Sub

End Class

Hello. I'm old and retired. I like to program if you could call what I do programming. However I'd like to code for you! If you've got the dime then I've got the time. Call me, landline, @ BR-549.


Tuesday, April 8, 2014 11:52 PM

I am old and retired, too!

I got tired of waiting for a response so tried a different method.  I added a printDocument control to my form (called PrintDocumentImageDisplay) and used the following code - the user invokes The first process through a tool strip and the second process prints the jpg file of interest:

    Private Sub PrintThisPictureToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PrintThisPictureToolStripMenuItem.Click
        PrintDocumentImageDisplay.Print()
    End Sub

    Private Sub PrintDocumentImageDisplay_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocumentImageDisplay.PrintPage
        Dim scale_width As Integer, scale_height As Integer
        If PictureBoxImage.Image.Height > PictureBoxImage.Image.Width Then
            PrintDocumentImageDisplay.DefaultPageSettings.Landscape = False
            scale_height = e.PageBounds.Height
            scale_width = (e.PageBounds.Height * PictureBoxImage.Image.Width) / PictureBoxImage.Image.Height
        Else
            PrintDocumentImageDisplay.DefaultPageSettings.Landscape = True
            scale_width = e.PageBounds.Height
            scale_height = (e.PageBounds.Height * PictureBoxImage.Image.Height) / PictureBoxImage.Image.Width
        End If
        Dim g As Graphics = e.Graphics
        Dim img As Image = PictureBoxImage.Image
        e.Graphics.DrawImage(img, 0, 0, scale_width, scale_height)
    End Sub

The process works fine if the image is not landscape.  If it is, the PrintDocumentImageDisplay.DefaultPageSettings.Landscape = False statement seems to be ignored and the image is printed portrait anyway.

Maybe this is a bug.  I tried changing the e.Graphics.DrawImage(img, 0, 0, scale_width, scale_height) statement  to e.Graphics.DrawImage(img, 0, 0, scale_height, scale_width) and still no luck!

This code almost works but not quite.

Thanks for your response.  Can you still help?

RONATMOODYLAKE


Wednesday, April 9, 2014 2:17 AM | 1 vote

Hi,

I think you could use PrintPageEventArgs.PageBounds property to implement this:

e.Graphics.DrawImage(img, 0, 0, e.PageBounds.Width, e.PageBounds.Height)

Screenshot:

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Wednesday, April 9, 2014 4:49 PM

Thanks a bunch!  This implementation took almost 2 days of research (Google search) and I have adopted it.  Your solution is much more elegant than mine.  I am surprised that someone hasn't posted a solution.  Lots of people probably want to print pictures from their files.

RONATMOODYLAKE


Monday, April 14, 2014 1:02 AM

*** I am surprised that someone hasn't posted a solution.  Lots of people probably want to print pictures from their files.***

What does this mean? 

Best Regards,
Please remember to mark the replies as answers if they help