CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Posts
    13

    Is there a way to shrink a form or reduce its size when it is printed???

    The code I'm working with is below. Right now, the form is too big to be printed out entirely on a sheet of paper. Is there a way to reduce the size of the form that gets sent to the printer without reducing the size of the form on the screen?


    Private Sub pd_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
    Handles pd.PrintPage
    e.Graphics.DrawImage(formImage, 0, 0)
    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

    Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    ' create an instance of the PrintDocument component
    pd = New Printing.PrintDocument
    Me.StartPosition = FormStartPosition.CenterScreen
    End Sub

    Private Sub Print_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) _
    Handles Print.Click
    ' initiate the printdocument component
    GetFormImage()
    pd.Print()
    End Sub

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Is there a way to shrink a form or reduce its size when it is printed???

    In your PrintPage event, create a new bitmap the size you want to print.

    Dim bm As Bitmap
    Dim BM2 As Bitmap
    Dim GR2 As Graphics
    bm = New Bitmap(TextBox1.Text)
    BM2 = New Bitmap(bm.Width * .5, bm.Height * .5) '<------ This will make it half the original size.
    GR2 = Graphics.FromImage(BM2)
    GR2.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    GR2.DrawImage(bm, 0, 0, BM2.Width, BM2.Height)

  3. #3
    Join Date
    Feb 2005
    Posts
    13

    Re: Is there a way to shrink a form or reduce its size when it is printed???

    I appreciate the response. However, I am having a little difficulty with the code.
    The following is showing up as an error:

    BM2 = New Bitmap(bm.Width * 0.5, bm.Height * 0.5) '<------ This will make it half the original size.

    Since I'm printing the form, I dont understand the bm/textbox1.text part of the code. Is there anyway you could help clarify this.

    This is how I've placed it in the code:

    Private Sub pd_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
    Handles pd.PrintPage
    Dim bm As Bitmap
    Dim BM2 As Bitmap
    Dim GR2 As Graphics
    bm = New Bitmap(TextBox1.Text)
    BM2 = New Bitmap(bm.Width * 0.5, bm.Height * 0.5) '<------ This will make it half the original size.
    GR2 = Graphics.FromImage(BM2)
    GR2.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    GR2.DrawImage(bm, 0, 0, BM2.Width, BM2.Height)
    e.Graphics.DrawImage(formImage, 0, 0)
    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

    Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    ' create an instance of the PrintDocument component
    pd = New Printing.PrintDocument
    Me.StartPosition = FormStartPosition.CenterScreen
    End Sub

    Private Sub Print_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) _
    Handles Print.Click
    ' initiate the printdocument component
    GetFormImage()
    pd.Print()
    End Sub

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Is there a way to shrink a form or reduce its size when it is printed???

    Sorry, bm = New Bitmap(TextBox1.Text) is just loading my original picture, you should be able to replace all references to bm with formImage.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured