dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    Addicted Member Slabs1960's Avatar
    Join Date
    Mar 2017
    Location
    Oranjemund, Namibia
    Posts
    168

    Assistance Needed with Printing a Popup Form

    I am trying to print a Windows Form Popup. I has a GIF image on it, in a PictureBox. At first, I used the PrintDocument component. Then I tried the PrintDialog. The reason being, I want to give the user an option of printing to a printer, or if they have a PDF printer, such as Microsoft Print to PDF or Foxit Reader PDF Printer, to print to a PDF file.

    I found some code on the web which I implemented and modified slightly. It works (sort of) with the printer, but does not work with print to PDF. No sure if I should make this 2 posts.

    Problem1:
    Printing to the print works, BUT, prints the background form, under the popup, and is off center.
    Constellation VB.pdf

    This is how it should look.
    Constellation Correct.pdf

    How do I get the image to print correctly?

    Problem 2:
    How do I get it to print to a PDF, if Print to PDF is selected in the dialog box?

    Code:
    Code:
    Private WithEvents Pd As Printing.PrintDocument
    
        ' Storage for Form Image
        Dim formImage As Bitmap
    
        ' Create API prototype
        Private Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal hdcDest As IntPtr,
                                          ByVal nXDest As Integer, ByVal nYDest As _
                                            Integer, ByVal nWidth As Integer,
                                             ByVal nHeight As Integer, ByVal _
                                              hdcSrc As IntPtr, ByVal nXSrc As Integer,
                                                ByVal nYSrc As Integer,
                                                   ByVal dwRop As System.Int32) As Long
    
    
        Private Sub PopupFrmConstellation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim strFilePath As String = ""
            Dim strImagePath As String = xmlApplicationDataFolder & "\UserData\Constellations\" & Me.Tag.ToString & ".gif"
    
    
            'Display Image
            If Not (PictureBoxConstellation.Image Is Nothing) Then
                PictureBoxConstellation.Image.Dispose()
                PictureBoxConstellation.Image = Nothing
            End If
    
    
            Try
                PictureBoxConstellation.Image = Image.FromFile(strImagePath)
    
                TextBoxConstellation.Text = Me.Tag.ToString
                StatusLabelConstellationMap.Text = Me.Tag.ToString
    
            Catch ex As Exception
                strFilePath = xmlApplicationDataFolder & "\UserData\ImageFiles\MessierObjects"
                strImagePath = strFilePath & "\" & "No-image-available.jpg"
                PictureBoxConstellation.Image = Image.FromFile(strImagePath)
            Finally
                ' Nothing
            End Try
    
            Pd = New Printing.PrintDocument
            StartPosition = FormStartPosition.CenterScreen
    
        End Sub
    
    
        'Print Form
    #Region "Print Form"
        Private Sub Pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Pd.PrintPage
    
            e.Graphics.DrawImage(formImage, 100, 100)
    
        End Sub
    
    
        Private Sub MnuPrint_Click(sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
    
            Dim dlgResult As DialogResult = PrintDialogConstellation.ShowDialog()
    
            GetFormImage()
    
            PrintDialogConstellation.ShowHelp = True
            PrintDialogConstellation.AllowSomePages = True
    
            ' If the result is OK then print the document.
            If (dlgResult = DialogResult.OK) Then
                Pd.Print()
            End If
    
        End Sub
    
    
        Private Sub GetFormImage()
    
            Dim g As Graphics = Me.CreateGraphics()
            Dim s As Size = Me.Size
    
            formImage = New Bitmap(s.Width, s.Height, g)
    
            Dim mg As Graphics = Graphics.FromImage(formImage)
            Dim dc1 As IntPtr = g.GetHdc
            Dim dc2 As IntPtr = mg.GetHdc
    
            'Added code to compute And capture the form title bar And borders
            Dim widthDiff As Integer = (Me.Width - Me.ClientRectangle.Width)
            Dim heightDiff As Integer = (Me.Height - Me.ClientRectangle.Height)
            Dim borderSize As Integer = widthDiff \ 2
            Dim heightTitleBar As Integer = heightDiff - borderSize
    
            BitBlt(dc2, 0, 0, Me.ClientRectangle.Width + widthDiff, Me.ClientRectangle.Height + heightDiff, dc1, 0 - borderSize, 0 - heightTitleBar, 13369376)
    
            g.ReleaseHdc(dc1)
            mg.ReleaseHdc(dc2)
    
        End Sub
    Any assistance appreciated.
    Last edited by Slabs1960; Feb 11th, 2018 at 04:02 AM.
    I can fix what stupid does, but, I cannot fix stupid

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,954

    Re: Assistance Needed with Printing a Popup Form

    Do you really want to print the whole form, or just the image? If it's just the image then just print the Image. If you really do want to print the whole form then you might consider downloading the VB PowerPacks from Microsoft and then using the PrintForm component.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Slabs1960's Avatar
    Join Date
    Mar 2017
    Location
    Oranjemund, Namibia
    Posts
    168

    Re: Assistance Needed with Printing a Popup Form

    I would like to print the image.
    I can fix what stupid does, but, I cannot fix stupid

  4. #4

    Thread Starter
    Addicted Member Slabs1960's Avatar
    Join Date
    Mar 2017
    Location
    Oranjemund, Namibia
    Posts
    168

    Re: Assistance Needed with Printing a Popup Form

    After the comment from jmcilhinney I rewrote to code to print the image, and not the form.

    It prints, but the image is too large, and needs to be resized. I added some code to resize, but it is not working.

    Full Code
    Code:
     Private Sub PopupFrmConstellation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim strFilePath As String = xmlApplicationDataFolder & "\UserData\Constellations\"
            strImagePath = strFilePath & Me.Tag.ToString & ".gif"
    
    
            'Test for File Type
            If File.Exists(strFilePath & Me.Tag.ToString & ".gif") Then
                strImagePath = strFilePath & Me.Tag.ToString & ".gif"
    
            ElseIf File.Exists(strFilePath & Me.Tag.ToString & ".bmp") Then
                strImagePath = strFilePath & Me.Tag.ToString & ".bmp"
    
            ElseIf File.Exists(strFilePath & Me.Tag.ToString & ".png") Then
                strImagePath = strFilePath & Me.Tag.ToString & ".png"
    
            ElseIf File.Exists(strFilePath & Me.Tag.ToString & ".jpg") Then
                strImagePath = strFilePath & Me.Tag.ToString & ".jpg"
    
            Else
                MessageBox.Show("Constellation Map Image File not Found", "PrintConstellation Map", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
            End If
    
    
            'Display Image
            If Not (PictureBoxConstellation.Image Is Nothing) Then
                PictureBoxConstellation.Image.Dispose()
                PictureBoxConstellation.Image = Nothing
            End If
    
    
            Try
                PictureBoxConstellation.Image = Image.FromFile(strImagePath)
    
                TextBoxConstellation.Text = Me.Tag.ToString
                StatusLabelConstellationMap.Text = Me.Tag.ToString
    
            Catch ex As Exception
                PictureBoxConstellation.Image.Dispose()
                PictureBoxConstellation.Image = Nothing
    
                MessageBox.Show("Constellation Map Image File not Found", "PrintConstellation Map", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
            Finally
                ' Nothing
            End Try
    
    
    
        End Sub
    
    
        'Print Form
    #Region "Print Form"
        Private Sub PrintConstellation_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintConstellation.PrintPage
    
            'Fetch File
            Dim stream As New FileStream(strImagePath, FileMode.Open, FileAccess.Read)
            Dim image As Image = Image.FromStream(stream)
    
            If File.Exists(strImagePath) Then
    
    
    
                '==============================================================================================================
                Dim ScaleFac As Integer = 100
    
                While (ScaleFac * image.Width / image.HorizontalResolution > e.PageBounds.Width Or
                             ScaleFac * image.Height / image.VerticalResolution > e.PageBounds.Height) And ScaleFac > 2
                    ScaleFac -= 1
                    End While
    
                Dim sz As New SizeF(ScaleFac * image.Width / image.HorizontalResolution, ScaleFac * image.Height / image.VerticalResolution)
    
                Dim p As New PointF((e.PageBounds.Width - sz.Width) / 2, (e.PageBounds.Height - sz.Height) / 2)
    
                e.Graphics.DrawImage(image, p)
    
                '===============================================================================================================
    
            Else
                MessageBox.Show("No Image Found", "Print Constellation Map", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
    
    
    
        End Sub
    
    
        Private Sub MnuPrint_Click(sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrint.Click
    
            Dim dlgResult As DialogResult = dlgPrintDialogConstellation.ShowDialog()
    
            dlgPrintDialogConstellation.ShowHelp = True
    
            If (dlgResult = DialogResult.OK) Then
                PrintConstellation.Print()
            End If
    
        End Sub
    Code to Resize image:
    Code:
               Dim ScaleFac As Integer = 100
    
                While (ScaleFac * image.Width / image.HorizontalResolution > e.PageBounds.Width Or
                             ScaleFac * image.Height / image.VerticalResolution > e.PageBounds.Height) And ScaleFac > 2
                    ScaleFac -= 1
                    End While
    
                Dim sz As New SizeF(ScaleFac * image.Width / image.HorizontalResolution, ScaleFac * image.Height / image.VerticalResolution)
    
                Dim p As New PointF((e.PageBounds.Width - sz.Width) / 2, (e.PageBounds.Height - sz.Height) / 2)
    
                e.Graphics.DrawImage(image, p)
    Any assistance appreciated.
    I can fix what stupid does, but, I cannot fix stupid

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,706

    Re: Assistance Needed with Printing a Popup Form

    Try this...

    Code:
    Imports System.IO
    Imports System.Drawing.Printing
    
    Public Class Form1
    
        Private WithEvents pd As New PrintDocument
        Private ppd As New PrintPreviewDialog
    
        Private strImagePath As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ppd.WindowState = FormWindowState.Maximized
            ppd.Document = pd
            strImagePath = "your image path"
            ppd.ShowDialog()
        End Sub
    
        Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
    
            If File.Exists(strImagePath) Then
    
                'Fetch File
                Dim stream As New FileStream(strImagePath, FileMode.Open, FileAccess.Read)
                Dim image As Image = image.FromStream(stream)
    
                Dim s As Single = CSng(image.Width / image.Height)
                Dim w As Single = e.MarginBounds.Width
                Dim h As Single = e.MarginBounds.Height
                Dim x As Integer
                Dim y As Integer
    
                If w / h > s Then
                    w = s * h
                    x = e.MarginBounds.Left + CInt((e.MarginBounds.Width - w) / 2)
                    y = e.MarginBounds.Top
                Else
                    h = w / s
                    x = e.MarginBounds.Left
                    y = e.MarginBounds.Top + CInt((e.MarginBounds.Height - h) / 2)
                End If
    
                e.Graphics.DrawImage(image, New Rectangle(x, y, CInt(w), CInt(h)))
    
            End If
    
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.