from bitmap to stream?

C

creepwood

I'm trying to load and image into a DB and alongside the image also a
thumbnailed version of the image, but somewhere in my code, the stream
doesn't take the thumbnail data. When I just change toe bitmap.save()
to a file instead of a stream it works just fine.


Dim bm As Bitmap = System.Drawing.Image.FromStream(fs)
Dim newHeight As Integer = 40
Dim newWidth As Integer = (newHeight / bm.Height) * bm.Width

Dim resized As Bitmap = New Bitmap(newWidth, newHeight)
Dim g As Graphics = Graphics.FromImage(resized)
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(bm, New Rectangle(0, 0, resized.Width,
resized.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel)
g.Dispose()

'Problems starts here

Dim imgStream As New IO.MemoryStream
resized.Save(imgStream, ImageFormat.Jpeg)
Dim c(imgStream.Length() - 1) As Byte
imgStream.Read(c, 0, c.Length)
imgStream.Close()

'problems end here

somehere in between there something goes bad. and I don't know what.
the workaround is to save the img in a tempfile and read a stream from
that but that's absurd.

Please don't direct me to a guide, or suggest to me to rework my whole
code, I only need to get a filled stream.

/Bennie
 
J

Jody Gelowitz

Try this:
'Problems starts here

Dim imgStream As New IO.MemoryStream
resized.Save(imgStream, ImageFormat.Jpeg)
Dim c(imgStream.Length() - 1) As Byte

'Reposition the MemoryStream pointer to the start of the stream after the
save
imgStream.Position = 0
imgStream.Read(c, 0, c.Length)
imgStream.Close()

'problems end here

HTH,
Jody
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top