Solved

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

Posted on 2011-02-24
1
428 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:DrDamnit
1 Comment
 
LVL 32

Accepted Solution

by:
DrDamnit earned 0 total points
ID: 34974327
Figured it out:

Dim newBMP As New Bitmap(iNewx, iNewy)
        Dim newImage As Image
        newImage = CType(newBMP, Image)

        'Create a blank, new graphic that is the correct *new* size
        Dim gt_dest As Graphics = Graphics.FromImage(newImage)
        'Draw our picture onto it, scaling it...
        gt_dest.DrawImage(imgTemp, 0, 0, iNewx + 1, iNewy + 1)
        Return newImage

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question