Writing TIF to database

  • Thread starter Thread starter Bruce D
  • Start date Start date
B

Bruce D

I have two methods to write to the database. The first one saves the file
to disk the re-reads it and then inserts it. This way works...at least I'm
able to read the image when I retrieve it.
The second way (the way I want to do it) does not work...it saves it...but I
can't read the image when I retrieve it.
I'm guessing it has something to do with the amount of data that is stored
in the byte... any ideas??

1)
tImage.Save("c:\temp\xfile.tif",
System.Drawing.Imaging.ImageFormat.Tiff)
Dim st As New System.IO.FileStream("c:\temp\xfile.tif",
IO.FileMode.Open, IO.FileAccess.Read)
Dim abyte(st.Length) As Byte
st.Read(abyte, 0, st.Length - 1)
st.Close()

2)
Dim ms As New System.IO.MemoryStream
tImage.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff)
Dim abyte(ms.Length) As Byte
ms.Read(abyte, 0, ms.Length - 1)
ms.Close()


-bruce duncan
 
Bruce,

I miss a lot of code,

Here a (very) old sample of my

\\\It needs a form with a picturebox and 4 buttons
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
///
I hope this helps a little bit?

Cor
 
Found the problem:
I needed to point the MemoryStream back to the first byte by using:
ms.Position = 0

Though I'm not sure why or where it was at...but it works now.

-bruce
 

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

Back
Top