Image <> String Problem

B

BluDog

Hi

I am trying to store an image in an xml file, i vcan store it fine but
the retreval is a bit of a problem:

To String:

Dim ms As New IO.MemoryStream
Dim arrImage() As Byte
Me.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
arrImage = ms.GetBuffer
Dim UTF8 As New System.Text.UTF8Encoding
Dim imageString as string = UTF8.GetString(arrImage)


Getting it back:

Dim UTF8 As New System.Text.UTF8Encoding
Dim arrImage() As Byte = UTF8.GetBytes(imageString)
Dim ms As New IO.MemoryStream(arrImage)
Me.Image = System.Drawing.Image.FromStream(ms)


Getting it back throws an error on the final line, "Invaid Parameter
Used". If i complete the above without saving it as the InnerText on
an XmlNode in betwen it works fine.

Any ideas would be great.

Thanks

blu
 
H

Herfried K. Wagner [MVP]

* BluDog said:
Dim ms As New IO.MemoryStream
Dim arrImage() As Byte
Me.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
arrImage = ms.GetBuffer
Dim UTF8 As New System.Text.UTF8Encoding
Dim imageString as string = UTF8.GetString(arrImage)

It won't solve your problem, but I suggest to use
'System.Text.Encoding.UTF8' instead instantiating the encoding.
 
C

Cor Ligthert

BluDog,

I have made a complete sample that special in your case fits probably
completly

I hope it helps?

Cor

\\\needs a new project with on the form 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
///
 
B

BluDog

It won't solve your problem, but I suggest to use
'System.Text.Encoding.UTF8' instead instantiating the encoding.

Herfried... yep, that makes sense. Cheers.
 
J

jela

Probably a byte is added while saving.
Try to convert the byte array ToBase64 before saving,
and back after retrieval
 
B

BluDog

Cor

Thanks, i can see this working, however in my application i want to
store the image as an XmlNode, the complete xml file looks like the
following:

- <Tags>
- <Tag id="e2c0fde4-f540-43e9-b0ac-0d7c94f2196f">
<Checked>Unchecked</Checked>
<Text>Test Tag</Text>
<Expanded>True</Expanded>
<Image type="base64Binary">JFIF


HHC    $.' ",#(7),01444'9=82<.342C
 2!!22222222222222222222222222222222222222222222222222

"













 






}


!1AQa"q2#BR$3br
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz













 





w


!1AQaq"2B #3Rbr
$4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz



??_^Nn+FQ.r8gSdG#N
Qc-tEAA_} +BI ?U</Image>
</Tag>
</Tags>

The example you have given illustratess how to store the image as a
file in it's own right and as in xml format through a dataset, my
problem is that i want it to be a tag within an existing xml document.

The problem is that somewhere and i am not sure where there is data
being lost i believe when turning the image into a string:

Dim ms As New IO.MemoryStream
Me.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim imageBytes() As Byte = ms.GetBuffer
Dim imageString As String =
System.Text.Encoding.UTF8.GetString(imageBytes)
Dim imageNode As XmlElement = CreateAppendElement(tagNode,
"Image")
imageNode.SetAttribute("type", "base64Binary")
imageNode.InnerText = imageString

I cannot find any references that create an xml representation of an
image as a part of a larger xml file.

Thanks

Blu.
 
B

BluDog

Probably a byte is added while saving.
Try to convert the byte array ToBase64 before saving,
and back after retrieval

Fantastic... thanks, for anyone else looking the solution based on the
OP:

To String:

Dim ms As New IO.MemoryStream
Me.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim imageString As String =
System.Convert.ToBase64String(ms.GetBuffer)
Dim imageNode As XmlElement = CreateAppendElement(tagNode,
"Image")
imageNode.SetAttribute("type", "base64Binary")
imageNode.InnerText = imageString


Getting it back:

Dim imageString As String = xmlNode("Image").InnerText
Dim imageBytes() As Byte =
System.Convert.FromBase64String(imageString)
Dim ms As New IO.MemoryStream(imageBytes)
Me.Image = Me.Image.FromStream(ms)

Cheers

Blu
 
C

Cor Ligthert

BluDog,

I changed my sample a little bit for you, I thought that it now fits your
needs.

Test program is included

I hope this helps?

Cor
\\\to test it needs 1 picturebox and 1 button on a form.
Private Function SerializeByte(ByVal _
imagestring As Byte()) As String
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New IO.MemoryStream
bf.Serialize(mem, imagestring)
Return Convert.ToBase64String(mem.ToArray())
End Function
Private Function DeserializeString(ByVal _
imagestring As String) As Byte()
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New IO.MemoryStream(Convert.FromBase64String(imagestring))
Return DirectCast(bf.Deserialize(mem), Byte())
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''
'Test program
Private fo As New OpenFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If fo.ShowDialog = DialogResult.OK Then
Dim fs As New IO.FileStream(fo.FileName, _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
Dim abyt As Byte() = br.ReadBytes(CInt(fs.Length))
br.Close()
Dim myresultstring As String = SerializeByte(abyt)
Dim abyt2 As Byte() = DeserializeString(myresultstring)
Dim ms As New IO.MemoryStream(abyt2)
Me.PictureBox1.Image = Image.FromStream(ms)
End If
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