473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Image <> String Problem

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
7 2266
* 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
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
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
Probably a byte is added while saving.
Try to convert the byte array ToBase64 before saving,
and back after retrieval
Nov 21 '05 #5
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bob | last post by:
My understanding of the image string function is this, that it allows the display of text in Graphics As per the parameters it receives: ImageString($image,6,$xpos,$ypos,$out,$col) these...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
4
by: Csaba2000 | last post by:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would...
3
by: Frank Wein | last post by:
Hi, today i found out some browsers (at least Mozilla and IE) support the <image> tag, its usage seems to be the same as for the <img> tag (IMDB.com uses it for example). I did not find anything...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
1
by: jross | last post by:
I've looked all over Google for a solution for the disappearing background image in IE. My problem code looks like this - <td colspan="3" background="../images/dots.gif"></td> It obviously...
2
by: -Karl | last post by:
Couls someone please advise me on this error. What I am trying to do is be able to convert an XML document into arrays. I read that the subs & functions have to be in <scripttags. Thanks! ...
0
by: dimitri pater | last post by:
---------- Forwarded message ---------- From: dimitri pater <dimitri.pater@gmail.com> Date: Sep 5, 2007 9:13 PM Subject: Re: StringIO MySQL data blob Image problem To: Tim Golden...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.