dcsimg
Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    45

    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: 310
Size:  51.2 KB
    Name:  image2.jpg
Views: 300
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
    40,001

    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
    Member
    Join Date
    Jul 2017
    Posts
    45

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,409

    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.
    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

  5. #5
    New Member
    Join Date
    Jun 2018
    Posts
    7

    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
    New Member
    Join Date
    Jun 2018
    Posts
    7

    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.

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