Link to home
Start Free TrialLog in
Avatar of sainavya1215
sainavya1215

asked on

Print an Image in a picturebox control

hi,

Below code prints image on a vb.net picturebox control. if there is a large image it fits it exactly inside the picturebox control. But the print lies during printing instead of printing the image size on the picturebox control it prints actual image size.

How could we print the image as per the size of picturebox?

here is the code used:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pd As New System.Drawing.Printing.PrintDocument
        AddHandler pd.PrintPage, AddressOf OnPrintPage
        pd.Print()
    End Sub
    Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
    End Sub
Avatar of Svetlin_Panayotov
Svetlin_Panayotov

       Dim inp As IntPtr = New IntPtr
        Dim result As System.Drawing.Image
        result = PictureBox1.Image.GetThumbnailImage(PictureBox1.Width, PictureBox1.Height, Nothing, inp)

Then you can use "result" instead "PictureBox1.Image"
Avatar of sainavya1215

ASKER

Could u pls let me know what code has to be eliminated here  and where exactly is ur code to be added.......

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim pd As New System.Drawing.Printing.PrintDocument
        AddHandler pd.PrintPage, AddressOf OnPrintPage
        pd.Print()
    End Sub
    Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Svetlin_Panayotov
Svetlin_Panayotov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for that........
you're welcome :)