By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

Bitmap and MemoryStream

P: n/a
I'm having a problem saving my bitmap in my MySQL database. Apparently it's
too big...but it shouldn't be. Here's what I got:

Dim bmpDocument As Bitmap = Bitmap.FromHbitmap(hDibCopy) ' get image from
scanner
Dim bmpResize As New Bitmap(bmpDocument, 680, 880)
mySQLManager.InsertDBImage(elmImageIndex, intNextDocument, bmpResize)

Public Function InsertDBImage(ByVal ImageElement As XmlElement, _
ByVal
DocumentID As Integer, _
ByVal tImage
As System.Drawing.Bitmap)
Dim st As New System.IO.MemoryStream
tImage.Save(st, System.Drawing.Imaging.ImageFormat.Bmp)
Dim bytBLOBData(st.Length - 1) As Byte
'st.Position = 0
st.Seek(0, IO.SeekOrigin.Begin)
st.Read(bytBLOBData, 0, st.Length)
MsgBox(bytBLOBData.Length) ' this displays
2393654...way too big

Now if I save the image to file and then use that file (as opposed to the
hDibCopy) the lenght is 87467...which is small enough to insert into my
database.
Am I doing something wrong? Why is it so big?

Any help is greatly appreciated.

-bruce duncan

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

You have to store the image in binary format. Storeimage converts
image to binary format. ConvertToBitmap converts it back. Note the offset
for the northwind database is 78 my storeimage function has a offset of 0.

Private Function StoreImage(ByVal bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFormat.Jpeg)

Return ms.GetBuffer

Catch

Return Convert.DBNull

End Try

End Function

Private Function ConvertToBitmap(ByVal data() As Byte, ByVal offset As
Integer) As Bitmap

Dim ms As New System.IO.MemoryStream

Dim bm As Bitmap

ms = New MemoryStream

ms.Write(data, offset, data.Length - offset)

bm = New Bitmap(ms)

Return bm

End Function

Ken

----------------------
"Bruce D" <br*************@hotmail.com> wrote in message
news:11*************@corp.supernews.com...
I'm having a problem saving my bitmap in my MySQL database. Apparently it's
too big...but it shouldn't be. Here's what I got:

Dim bmpDocument As Bitmap = Bitmap.FromHbitmap(hDibCopy) ' get image from
scanner
Dim bmpResize As New Bitmap(bmpDocument, 680, 880)
mySQLManager.InsertDBImage(elmImageIndex, intNextDocument, bmpResize)

Public Function InsertDBImage(ByVal ImageElement As XmlElement, _
ByVal
DocumentID As Integer, _
ByVal tImage
As System.Drawing.Bitmap)
Dim st As New System.IO.MemoryStream
tImage.Save(st, System.Drawing.Imaging.ImageFormat.Bmp)
Dim bytBLOBData(st.Length - 1) As Byte
'st.Position = 0
st.Seek(0, IO.SeekOrigin.Begin)
st.Read(bytBLOBData, 0, st.Length)
MsgBox(bytBLOBData.Length) ' this displays
2393654...way too big

Now if I save the image to file and then use that file (as opposed to the
hDibCopy) the lenght is 87467...which is small enough to insert into my
database.
Am I doing something wrong? Why is it so big?

Any help is greatly appreciated.

-bruce duncan


Nov 21 '05 #2

P: n/a
Thanks Ken...your code works but I have a question:
The size of object that is returned is ~2MB in size. If I first save the
file to disk then use FileStream...the size is 84K. I guess I don't fully
understand why the memorystream is so large. Is it holding extra data? If
it's got extra data, can I just extract what I need?

-bruce
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uv**************@TK2MSFTNGP14.phx.gbl...
Hi,

You have to store the image in binary format. Storeimage converts
image to binary format. ConvertToBitmap converts it back. Note the offset for the northwind database is 78 my storeimage function has a offset of 0.

Private Function StoreImage(ByVal bm As Bitmap) As Object

Dim ms As New MemoryStream

Try

bm.Save(ms, Imaging.ImageFormat.Jpeg)

Return ms.GetBuffer

Catch

Return Convert.DBNull

End Try

End Function

Private Function ConvertToBitmap(ByVal data() As Byte, ByVal offset As
Integer) As Bitmap

Dim ms As New System.IO.MemoryStream

Dim bm As Bitmap

ms = New MemoryStream

ms.Write(data, offset, data.Length - offset)

bm = New Bitmap(ms)

Return bm

End Function

Ken

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.