By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Image <> String Problem

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* BluDog <ne**@nospam.bludog.net> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2

P: n/a
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
///
Nov 21 '05 #3

P: n/a
On 07 Sep 2004 13:02:48 +0200, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
* BluDog <ne**@nospam.bludog.net> scripsit:
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.


Herfried... yep, that makes sense. Cheers.
Nov 21 '05 #4

P: n/a
Probably a byte is added while saving.
Try to convert the byte array ToBase64 before saving,
and back after retrieval
Nov 21 '05 #5

P: n/a
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!!222222222222222222222222222222222222222222222 22222

"







 




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





 



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

??_^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.
Nov 21 '05 #6

P: n/a
On 7 Sep 2004 07:32:04 -0700, je***********@hotmail.com (jela) wrote:
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
Nov 21 '05 #7

P: n/a
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.BinaryForm atter
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.BinaryForm atter
Dim mem As New IO.MemoryStream(Convert.FromBase64String(imagestri ng))
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
///
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.