Solved

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

Posted on 2011-02-24
1
426 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now