Home All Groups Group Topic Archive Search About

Bitmap and MemoryStream

Author
5 Apr 2005 5:15 PM
Bruce D
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

Author
5 Apr 2005 5:38 PM
Ken Tucker [MVP]
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" <brucexwxdunc***@hotmail.com> wrote in message
news:1155hu3t54cru3e@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
Author
5 Apr 2005 8:39 PM
Bruce D
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


Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uvrXJZgOFHA.2568@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
>