[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:
Dim bm As New Bitmap(myimage)
Dim w As Integer = 16 'image width.
Dim h As Integer = 16 'image height
Dim thumb As New Bitmap(w, h)
Dim g As Graphics = Graphics.FromImage(thumb)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(bm, New Rectangle(0, 0, w, h), New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
g.Dispose()
mytab.image = bm
EDIT: The image can be .png (transparent), bmp, jp(e)g, gif
Thanks in advance.
Re: Scale image (memorystream)
Have you tried using a PictureBox to display your image?
VB.NET Code:
Private Sub InitializePictureBox()
PictureBox1 = New PictureBox
' Set the location and size of the PictureBox control.
Me.PictureBox1.Location = New System.Drawing.Point(70, 120) ' Or wherever.
Me.PictureBox1.Size = New System.Drawing.Size(16, 16) ' Or whatever.
Me.PictureBox1.TabStop = False
' Set the SizeMode property to the StretchImage value. This
' will shrink or enlarge the image as needed to fit into
' the PictureBox.
Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
' Set the border style to a three-dimensional border.
Me.PictureBox1.BorderStyle = BorderStyle.Fixed3D
' Add the PictureBox to the form.
Me.Controls.Add(Me.PictureBox1)
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.
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 ;)
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
Re: Scale image (memorystream)
... or do it the easy way:
vb.net Code:
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
Re: Scale image (memorystream)
Thanks Ed. Works like a charm :thumb:
@boops boobs: Now why didn't I see that!? :P Thanks for notifying.