PC Review


Reply
Thread Tools Rate Thread

Bitmap and MemoryStream

 
 
Bruce D
Guest
Posts: n/a
 
      5th Apr 2005
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



 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      5th Apr 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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




 
Reply With Quote
 
Bruce D
Guest
Posts: n/a
 
      5th Apr 2005
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitmap from memorystream has altered pixels margec3 Microsoft Dot NET Compact Framework 1 29th Jun 2007 05:48 PM
Bitmap and MemoryStream MLM450@hotmail.com Microsoft C# .NET 1 6th Jul 2006 02:56 PM
Memorystream bitmap with tcpclient and sockets =?Utf-8?B?dmJkb3RuZXRtYW5pYQ==?= Microsoft VB .NET 1 25th Aug 2004 05:28 PM
memorystream bytes to bitmap =?Utf-8?B?dmJkb3RuZXRtYW5pYQ==?= Microsoft VB .NET 3 25th Aug 2004 12:29 PM
Memorystream bytearray bitmap =?Utf-8?B?dmJkb3RuZXRtYW5pYQ==?= Microsoft VB .NET 4 25th Aug 2004 11:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:14 AM.