BLOBs and MemoryStream ?? VB.NET SQL2000

B

Bob

I am writing an Image to SQL and can use the code below to accomplish
the streaming. However, I need to write an Image that exists in
memory. I don't want to save it to disk just to re-load and stream
it. Suggestions? Use MemoryStream() somehow?

Dim fs As FileStream = New FileStream(fileName, FileMode.Open,
FileAccess.Read)
etc.


Thanks
Bob
 
F

Frans Bouma [C# MVP]

(e-mail address removed) (Bob) wrote in @posting.google.com:
I am writing an Image to SQL and can use the code below to accomplish
the streaming. However, I need to write an Image that exists in
memory. I don't want to save it to disk just to re-load and stream
it. Suggestions? Use MemoryStream() somehow?

Dim fs As FileStream = New FileStream(fileName, FileMode.Open,
FileAccess.Read)
etc.

It's best to write byte[] arrays to the database if you want to store
images. How's your image stored in memory, in a picture box control?

FB
 
F

Frans Bouma [C# MVP]

Bob said:
My image is stored in a Image variable.

e.g. Dim Im as Image

Ok. To save the image to the DB, first create a memorystream
object, then specify that memory stream object with the Im.Save() method
and eventually a format specification (you can get one from the image
itself).

You can then get the image in a byte array from the memory stream,
by calling the ToArray() method of memory stream.

Once you have a byte array, you can set that as value of a
SqlParameter object and save it with a normal insert/update query.

From byte array to image is the same: create a memory stream from
the byte array, then create an image from the memory stream using the
shared FromStream() method of Image.

FB
 
B

Bob

Your reply looks great, thanks. I am having trouble with
MemoryStream....

Dim Im As Image
Im = Im.FromFile("C:\smallphoto.Bmp")

Dim ms As MemoryStream = New MemoryStream

Im.Save(ms, Imaging.ImageFormat.Bmp)

... I get "Value cannot be null." error on the last line. ms shows Null
when I hover over it. Suggestions?

Thanks again!
Bob
 
C

Cor

Hi Bob,

I made a while ago a complete sample. The dataset is an XML file, but that
is only for the sample.
(Have a look that it is not always necessary to use a memorystream)

I hope this helps?

Cor

\\\
Private abyt() As Byte
Private fo As New OpenFileDialog
Private sf As New SaveFileDialog

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Reading a picture and put it in a bytearray
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
abyt = br.ReadBytes(CInt(fs.Length))
br.Close()
'just to show the sample without a fileread error
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button2.Click
'writing a picture from a bytearray
If sf.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(sf.FileName, _
IO.FileMode.CreateNew)
Dim bw As New IO.BinaryWriter(fs)
bw.Write(abyt)
bw.Close()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles Button3.Click
'writing a bytearray to a dataset
Dim ds As New DataSet
ds.Tables.Add(New DataTable("Photo"))
ds.Tables(0).Columns.Add(New DataColumn("Sample"))
ds.Tables(0).Columns(0).DataType =
System.Type.GetType("System.Byte[]")
ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)
ds.Tables(0).Rows(0)(0) = abyt
Dim sf As New SaveFileDialog
If sf.ShowDialog = DialogResult.OK Then
ds.WriteXml(sf.FileName, XmlWriteMode.WriteSchema)
End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'reading a picture from a dataset
Dim ds As New DataSet
If fo.ShowDialog = DialogResult.OK Then
ds.ReadXml(fo.FileName)
End If
abyt = CType(ds.Tables(0).Rows(0)(0), Byte())
Dim ms As New IO.MemoryStream(abyt)
Me.PictureBox1.Image = Image.FromStream(ms)
End Sub
///
 

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

Top