Results 1 to 6 of 6

Thread: [RESOLVED] Scale image (memorystream)

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Scale image (memorystream)

    Hi,

    I'm loading an image into a memorystream from my server.

    Dim MS As New MemoryStream(wc.DownloadData(result)) ' wc = webclient
    Dim myimage = Image.FromStream(MS)
    mytab.image = myimage

    This works, but I want to scale the image to 16px*16px. How do I do that?

    I tried the following, but the images isn't smaller:

    vb.net Code:
    1. Dim bm As New Bitmap(myimage)
    2. Dim w As Integer = 16 'image width.
    3. Dim h As Integer = 16 'image height
    4. Dim thumb As New Bitmap(w, h)
    5. Dim g As Graphics = Graphics.FromImage(thumb)
    6. g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    7. g.DrawImage(bm, New Rectangle(0, 0, w, h), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
    8. g.Dispose()
    9. mytab.image = bm

    EDIT: The image can be .png (transparent), bmp, jp(e)g, gif

    Thanks in advance.
    Last edited by Radjesh Klauke; Sep 8th, 2012 at 06:43 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    Re: Scale image (memorystream)

    Have you tried using a PictureBox to display your image?

    VB.NET Code:
    1. Private Sub InitializePictureBox()
    2.     PictureBox1 = New PictureBox
    3.  
    4.     ' Set the location and size of the PictureBox control.
    5.     Me.PictureBox1.Location = New System.Drawing.Point(70, 120)    ' Or wherever.
    6.     Me.PictureBox1.Size = New System.Drawing.Size(16, 16)          ' Or whatever.
    7.     Me.PictureBox1.TabStop = False
    8.  
    9.     ' Set the SizeMode property to the StretchImage value.  This
    10.     ' will shrink or enlarge the image as needed to fit into
    11.     ' the PictureBox.
    12.     Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    13.  
    14.     ' Set the border style to a three-dimensional border.
    15.     Me.PictureBox1.BorderStyle = BorderStyle.Fixed3D
    16.  
    17.     ' Add the PictureBox to the form.
    18.     Me.Controls.Add(Me.PictureBox1)
    19.  
    20. End Sub

    You can set the position and size to whatever you wish, and if you've selected the 'SizeMode' property to 'StretchImage' the displayed image will fill the PictureBox, this is irrespective of it's original shape, so you need to be sure that the original image is square if you want a 16 x 16 image.

    Poppa.
    Last edited by Poppa Mintin; Sep 8th, 2012 at 08:00 AM.
    Along with the sunshine there has to be a little rain sometime.

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Scale image (memorystream)

    Thanks for thinking with me, but I can't add a picturebox to my tabcontrol. Besides that, not really the solution I was looking. Thanks again though


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Scale image (memorystream)

    Mainly I think you just got your variables mixed up in the drawimage call,
    maybe try somethin like..
    Code:
    mytab.image = ResizeImage(myimage, 16,16)
    
    ' ~~~
    
    Private Function ResizeImage(ByVal img As Image, ByVal w As Integer, ByVal h As Integer) As Image
        Dim newImage As New Bitmap(w, h)
        Using g As Graphics = Graphics.FromImage(newImage)
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            g.DrawImage(img, New Rectangle(0, 0, w, h))
        End Using
        Return newImage
    End Function

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Scale image (memorystream)

    ... or do it the easy way:

    vb.net Code:
    1. Dim bm As New Bitmap(myImage, 16, 16)

    p.s. @ radjesh: The reason your code didn't work was that you used bm instead of thumb on the last line.

    BB

  6. #6

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Scale image (memorystream)

    Thanks Ed. Works like a charm
    @boops boobs: Now why didn't I see that!? :P Thanks for notifying.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width