Results 1 to 12 of 12

Thread: How to resize picturebox image into bitmap for Printing in A4 format?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    73

    How to resize picturebox image into bitmap for Printing in A4 format?

    Hello All,

    I'am trying to resize my Picturebox images into bitmap for Printing purpose but facing an issue. The issue is shown below by images.
    First image is the actual form image, second image is the print preview image which is showing only top right part of the image. I want to display the the whole image but in smaller size so that I can fit both my graphs on the print page.

    Name:  image1.jpg
Views: 2238
Size:  51.2 KB
    Name:  image2.jpg
Views: 2202
Size:  10.1 KB

    the code for printing is shown below

    Code:
     Private Sub Print_Click(sender As Object, e As EventArgs) Handles Print.Click   ' print button click
    
            PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
            Dim panel As New Panel
            Me.Controls.Add(panel)
    
            Dim s As Size = FVC1PictureBox.Size
            memoryimage = New Bitmap(s.Width, s.Height)
            Dim memorygraphics As Graphics = Graphics.FromImage(memoryimage)
            Dim screenpos As Point = FVC1PictureBox.PointToScreen(New Point(0, 0))
            memorygraphics.CopyFromScreen(screenpos.X, screenpos.Y, 0, 0, s)
    
    
            memoryimage = ResizeBitmap(memoryimage, 200, 200)
            PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
    
    
        End Sub
        Function ResizeBitmap(ByVal bitmapToResize As Bitmap, ByVal width As Integer, ByVal height As Integer)
    
            'make a blank bitmap the correct size
    
            Dim NewBitmap As New Bitmap(width, height)
    
            'make an instance of graphics that will draw on "NewBitmap"
    
            Dim BitmpGraphics As Graphics = Graphics.FromImage(NewBitmap)
    
            'work out the scale factor
    
            Dim scaleFactorX As Integer = bitmapToResize.Width / width
    
            Dim scaleFactorY As Integer = bitmapToResize.Height / width
    
            'resize the graphics
    
            BitmpGraphics.ScaleTransform(scaleFactorX, scaleFactorY)
    
            'draw the bitmap to NewBitmap
    
            BitmpGraphics.DrawImage(bitmapToResize, 0, 0)
            BitmpGraphics.Save()
            Return NewBitmap
    
        End Function
    
    Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
            
            e.Graphics.DrawImage(memoryimage, 50, 160)
        End Sub

    Kindly suggest a solution.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    I haven't tested this, but my initial thoughts are to replace this section:
    Code:
            Dim s As Size = FVC1PictureBox.Size
            memoryimage = New Bitmap(s.Width, s.Height)
            Dim memorygraphics As Graphics = Graphics.FromImage(memoryimage)
            Dim screenpos As Point = FVC1PictureBox.PointToScreen(New Point(0, 0))
            memorygraphics.CopyFromScreen(screenpos.X, screenpos.Y, 0, 0, s)
    
    
            memoryimage = ResizeBitmap(memoryimage, 200, 200)
    ...with just this:
    Code:
            memoryimage = New Bitmap(FVC1PictureBox.Image)
            memoryimage = ResizeBitmap(memoryimage, 200, 200)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    73

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    Hello,

    Actually there is no fixed image inside the Picturebox. It is a graph plotted by serial data values on the picture box control using graphics & drawline function .

    Using your suggested changes in the code gives a null refernce object exception. I think this is because there is no image in picturebox.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    You need to draw whatever you've drawn onto the PictureBox onto a Bitmap first then. There are two ways to do that:

    1. Write a single method that takes a Grpahics object that does the drawing and then call that with two different Graphics objects; one for the control and another for a Bitmap. To see an example of that, follow the CodeBank link in my signature below and check out my Simple Drawing thread.

    2. Call the DrawToBitmap method of the PictureBox. I've not tested whether this will include drawing in the Paint event handler but I would expect that it would.

  5. #5
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    Sorry, Testing edit...
    Last edited by tommytwotrain; Aug 11th, 2018 at 08:02 AM.

  6. #6
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    434

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    Sumit,

    You can make a single drawing sub routine that accepts a graphics surface (ie e.graphics) and draws the graph on the surface.

    Then you can call that drawing routine with both the screen control (picturebox) or the printer e.graphics drawing surface.

    That way you get a high resolution vector printout instead of a jagged resized bitmap.

    You could also use the chart control to draw the graph.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    73

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    Hello ,

    How can I convert the real time serial data plotting into bitmap? The graph plotting is based on serial data. Only the Grid at the back of the graph is fixed and is being drawn in the Picturebox paint event. The serial data is being plotted in a delegate function. The plotting stops on the click of "STOP" button.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    When you draw on a PictureBox you use the e.Graphics Graphics object. You can create a bitmap...

    Code:
    Dim img as new Bitmap(width, height)
    Dim gr as Graphics = Graphics.FromImage(img)
    gr is a Graphics object, which when used in the same way you use the PictureBox Graphics object, will draw on img the Bitmap object.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    73

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    Hi Paul,

    Below is the code for my graph drawing on the picturebox

    Code:
    Function PrintFVCexp(ByVal Xvalue2 As UInt32) As Object
    
            mygraphics1 = FVC1PictureBox.CreateGraphics
            mygraphics1.PageUnit = GraphicsUnit.Pixel
            mygraphics1.PageScale = 1
            mygraphics1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            mygraphics1.ScaleTransform(1, 1)
    
    
            If DataEnter1 = True Then
                mygraphics1.DrawLine(p6, (x1 + (k2)), (y1), (x1 + (k2 + 2)), (y2 - Xvalue2))
                y1 = (y2 - Xvalue2)
                DataEnter1 = False
                Dr = True
            End If
            k2 = k2 + 2
    
            If k2 = FVC1PictureBox.Width Then
                k2 = 0
    
                mygraphics1.Clear(Color.White)
                Me.graph()
    
            End If
        End Function
    It is a function called inside the delegate function of serial data received event. This function is getting called multiple times till the time data is received at serial port.

    Where should I write the bitmap part?

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    Code:
    Function PrintFVCexp(ByVal mygraphics1 as Graphics, ByVal Xvalue2 As UInt32) As Object
    
        mygraphics1.PageUnit = GraphicsUnit.Pixel
        mygraphics1.PageScale = 1
        mygraphics1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        mygraphics1.ScaleTransform(1, 1)
    
    
        If DataEnter1 = True Then
            mygraphics1.DrawLine(p6, (x1 + (k2)), (y1), (x1 + (k2 + 2)), (y2 - Xvalue2))
            y1 = (y2 - Xvalue2)
            DataEnter1 = False
            Dr = True
        End If
        k2 = k2 + 2
    
        If k2 = FVC1PictureBox.Width Then
            k2 = 0
    
            mygraphics1.Clear(Color.White)
            Me.graph()
    
        End If
    End Function
    Then you can call it like this...

    Code:
    Dim mygraphics1 as Graphics = FVC1PictureBox.CreateGraphics
    PrintFVCexp(mygraphics1, youruintvalue)
    Or...

    Code:
    Dim img as new Bitmap(width, height)
    Dim gr as Graphics = Graphics.FromImage(img)
    PrintFVCexp(gr, youruintvalue)
    Alternatively copy the image from the picturebox...

    Code:
    Dim img as new Bitmap(width, height)
    Dim gr as Graphics = Graphics.FromImage(img)
    gr.CopyFromScreen(etc, etc,)
    I'm not at my computer at the moment. You'll have to research the coordinates for CopyFromScreen

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2017
    Posts
    73

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    Hello Paul,

    I tried your code as shown below

    Code:
    Function PrintFVCexp(ByVal mygraphics1 as Graphics, ByVal Xvalue2 As UInt32) As Object
    
        mygraphics1.PageUnit = GraphicsUnit.Pixel
        mygraphics1.PageScale = 1
        mygraphics1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        mygraphics1.ScaleTransform(1, 1)
    
    
        If DataEnter1 = True Then
            mygraphics1.DrawLine(p6, (x1 + (k2)), (y1), (x1 + (k2 + 2)), (y2 - Xvalue2))
            y1 = (y2 - Xvalue2)
            DataEnter1 = False
            Dr = True
        End If
        k2 = k2 + 2
    
        If k2 = FVC1PictureBox.Width Then
            k2 = 0
    
            mygraphics1.Clear(Color.White)
            Me.graph()
    
        End If
    End Function
    and the function is called in the delegate as

    Code:
    Dim img as new Bitmap(width, height)
    Dim gr as Graphics = Graphics.FromImage(img)
    Private Function Graph1Delegate()
    
    PrintFVCexp(gr,count1)
    
    End Function
    How ever it does not draw anything on the picture box. I think we have to assign img as Picturebox image to make it visible.

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to resize picturebox image into bitmap for Printing in A4 format?

    One of the advantages of drawing to a bitmap is you can do that from the thread you're getting the data in so you wouldn't need a delegate for the drawing.
    You should probably create the image at an outer scope so it remains available. You would then draw your graph in it first, then as you get data in draw the data "on top".
    If you want to see the graph in the picturebox, you have two options. Assign the bitmap to the Image, as you said, or draw the image into the picturebox. In the second case, that should be done in the Paint event of the picturebox, so you would trigger the Paint Event by invalidating the picturebox, rather than using the CreateGraphics method of the picturebox.

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