Link to home
Start Free TrialLog in
Avatar of DrDamnit
DrDamnitFlag for United States of America

asked on

How to do get system.drawing.graphics content into a picture box?

I have created a functin that scales an image.

The scaling appears to go ok, but the problem is that I have no idea how to get the scaled graphic (gt_dest) back into the picturebox (picBannerBox) that is on Form1.

How do I do that?
Private Function ScaleImage(ByVal imgTemp As Image, ByVal box As PictureBox) As Graphics
        Dim fSize As String
        Dim iNewx As Integer = 0
        Dim iNewy As Integer = 0

        Dim oldBMP As New Bitmap(CInt(imgTemp.Width), CInt(imgTemp.Height))

        'Determine which side is larger than the canvas, 

        If ImageTooWide(imgTemp) And ImageTooTall(imgTemp) Then
            '...and if both are larger, pick the larger side.
            If imgTemp.Height > imgTemp.Width Then
                fSize = "height"
            Else
                fSize = "width"
            End If
        Else
            '... Other wise pick the one that is larger.
            If ImageTooWide(imgTemp) Then fSize = "width"
            If ImageTooTall(imgTemp) Then fSize = "height"
        End If

        Select Case fSize
            Case "width"
                iNewx = picBannerBox.Width
                iNewy = iNewx / imgTemp.Width * imgTemp.Height
            Case "height"
                iNewy = picBannerBox.Height
                iNewx = iNewy / imgTemp.Height * imgTemp.Width
        End Select
        Dim newBMP As New Bitmap(iNewx, iNewy)
        'Create a blank, new graphic that is the correct *new* size
        Dim gt_dest As Graphics = Graphics.FromImage(newBMP)
        'Draw our picture onto it, scaling it...
        gt_dest.DrawImage(imgTemp, 0, 0, iNewx + 1, iNewy + 1)
        'Draw new image to picBannerBox
        '????????????
    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DrDamnit
DrDamnit
Flag of United States of America image

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