|
-
Sep 8th, 2012, 06:26 AM
#1
Thread Starter
PowerPoster
[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.
Last edited by Radjesh Klauke; Sep 8th, 2012 at 06:43 AM.
-
Sep 8th, 2012, 07:57 AM
#2
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.
Last edited by Poppa Mintin; Sep 8th, 2012 at 08:00 AM.
Along with the sunshine there has to be a little rain sometime.
-
Sep 8th, 2012, 12:27 PM
#3
Thread Starter
PowerPoster
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
-
Sep 8th, 2012, 04:43 PM
#4
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
-
Sep 9th, 2012, 02:49 AM
#5
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
Last edited by boops boops; Sep 9th, 2012 at 02:54 AM.
-
Sep 9th, 2012, 04:08 AM
#6
Thread Starter
PowerPoster
Re: Scale image (memorystream)
Thanks Ed. Works like a charm 
@boops boobs: Now why didn't I see that!? :P Thanks for notifying.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|