base64 string to image problem

Could someone please help me on this, im about to tear my head off....


I am SIMPLY trying to convert a base64 string from an xml file to an image ....

I have declared a DataSet object, called the LoadXml.. checked the size of the byte array which is the same as the origional....


 ' Extract the image
            Dim b() As Byte
            b = Convert.FromBase64String(ds.Tables("ScreenShots").Rows(0).Item(2))

            Dim ms As New MemoryStream
            ms.Read(b, 0, b.Length)
            ' reset the stream
            ms.Seek(0, SeekOrigin.Begin)

            Me.PictureBox1.Image = Image.FromStream(ms)

but i get ... System.ArgumentException: "Invalid Parameter used."

I know this is simple, what am i doing wrong?  I have copied and pasted the base64 text on a form online that does conversions (http://www.toastedspam.com/decode64) and the image shows up fine, so i know the text is good.

?

* Never trust a circle *
0
aikeith
6/24/2005 4:11:59 PM
asp.net.drawinggdi 1803 articles. 0 followers. Follow

5 Replies
2325 Views

Similar Articles

[PageSpeed] 0
Get it on Google Play
Get it on Apple App Store

I keep looking at your post, and finally got a chance to code something out.  I think, after going through the exercise myself, that your problem is that you never got the bytes into the memory stream (that would be a write, not a read).  In my approach, I got the byte array first, then used it in the MemoryStream ctor, so I never needed to write.

   // Setup: Create a base64 string containing the image bytes

   System.Drawing.Image img = System.Drawing.Image.FromFile(@"c:\pict1.jpg");
   System.IO.MemoryStream ms = new System.IO.MemoryStream();

   img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

   string b64 = Convert.ToBase64String(ms.ToArray());

   // Housecleaning: Get rid of the local instances
   img.Dispose();
   ms.Close();




   // Here's where aikeith's relevance starts  
       
   byte[] b;
   b = Convert.FromBase64String(b64);

   ms = new System.IO.MemoryStream(b);

   img = System.Drawing.Image.FromStream(ms);




   // Since this is a console app, save file so I can see if it works.

   img.Save(@"c:\pict1_from_b64.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

   // Housecleaning: Get rid of the local instances
   img.Dispose();
   ms.Close();


JasonF
A View Inside My Head
0
JasonFollas
6/28/2005 7:06:30 PM
Just wanted to tell you thank you Jason, it was a great help with what you provided! Thank you!

VBCode

1    Sub Base64Convert(ByVal Base64MSG As String)
2    	'Setup image and get data stream together
3    	Dim img As System.Drawing.Image
4    	Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream
5    
6    	Dim b64 As String = Base64MSG
7    	Dim b() As Byte
8    
9    	'Converts the base64 encoded msg to image data
10   	b = Convert.FromBase64String(b64)
11   	MS = New System.IO.MemoryStream(b)
12   
13   	'creates image
14   	img = System.Drawing.Image.FromStream(MS)
15   
16   	'writes image for displaying
17   	img.Save(Request.ServerVariables("APPL_PHYSICAL_PATH") & "LabelInfo.tiff", System.Drawing.Imaging.ImageFormat.Tiff)
18   	'cleaning up house
19   	img.Dispose()
20   	MS.Close()
21   End Sub
0
n0k
5/19/2006 5:52:16 PM

i am trying following code but facing same problem not sure where the problem is :

Dim filepath As String = "E:\9.jpg"

Dim sr As New System.IO.StreamReader(filepath)

Dim bytedata() As Byte = System.Text.Encoding.Default.GetBytes(sr.ReadToEnd())

Dim sBase464 As String = Convert.ToBase64String(bytedata)
sr.Close()

Dim sBackByte() As Byte

sBackByte = Convert.FromBase64String(sBase464)

Dim ms As New MemoryStream(sBackByte)

Dim image As System.Drawing.Image

image = System.Drawing.Image.FromStream(ms)   ' [ on this line i am getting the exception "Parameter is not valid." ]

ms.Close()

Return

 

please help


Khurram Shahzad
K2.Net Workflows
Dot Net Focus
(Reply == Helpful ? "Mark as Answer" : "-")
0
khurramatk2
12/25/2007 2:20:11 PM

My Vb.Net is quite rusty, but;

 Dim b As Byte() ' you want a byte ARRAY here
 string s= DirectCast( ds.Tables("ScreenShots").Rows(0).Item(2), String)
            b = Convert.FromBase64String( s)
            Dim ms As New MemoryStream(b)
         Me.PictureBox1.Image = Image.FromStream(ms)

            


[C# MVP]
Unblog|WebSite|IttyUrl
0
pbromberg
12/25/2007 2:50:28 PM

i tryied as u wrote but still getting same exception.....

 


Khurram Shahzad
K2.Net Workflows
Dot Net Focus
(Reply == Helpful ? "Mark as Answer" : "-")
0
khurramatk2
12/26/2007 11:23:29 PM
Reply: