Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hye all...


i m working in Windows Phone 7 and i need to convert a string to a stream.

i got a photo stream to which i have converted into string as-

StreamReader sr = new StreamReader(photoStream);
                string str = sr.ReadToEnd();



now i want to convert that string back to stream and then to image as -

byte[] data = Encoding.UTF8.GetBytes(str);
            MemoryStream memStream = new MemoryStream(data);
            memStream.Seek(data.Length, 0);
            WriteableBitmap bimg = PictureDecoder.DecodeJpeg(memStream);
            image1.Source = bimg;


but as it come to the line

WriteableBitmap bimg = PictureDecoder.DecodeJpeg(memStream);

it gives the exception - "The parameter is incorrect."

now what should i do...:confused:
please help...
thanks in advance...
Posted

If this is image data, then I wouldn't convert it to a string: the default conversion expects unicode text data rather than binary, and will change newlines and so forth. Instead, consider converting it to a byte[] using Stream.GetBuffer
 
Share this answer
 
Comments
DeepsMann 31-Jan-11 4:39am    
thanks,

but then how can i convert a byte[] back to image...
OriginalGriff 31-Jan-11 4:45am    
Since you are on WP7, I think the easiest way is to convert it to a MemoryStream and then use the Bitmap(Stream) constructor.
DeepsMann 31-Jan-11 4:50am    
thanks,

but i have done the same.
i have converted the byte[] to MemoryStream and then using

WriteableBitmap bimg = PictureDecoder.DecodeJpeg(memoryStream);

i tried to convert it to image.

now what should i do.
please provide some sample if u can.
DeepsMann 31-Jan-11 4:46am    
i have searched, but everywhere it uses

System.Drawing.Image.FromStream(stream);


but System.Drawing doesn't exists in WP7.
hye all,

I have converted a photo stream to string as in Windows Phone 7 (C#.net)

namespaces used :
using System.Windows.Controls; using System.IO; using System.Windows.Media.Imaging;
using System.Windows.Media;


Stream to String :

BitmapImage bimg = new BitmapImage();
                bimg.SetSource(photoStream); //photoStream is a stream containing data for a photo
                
                byte[] bytearray = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    WriteableBitmap wbitmp = new WriteableBitmap(bimg);
                    wbimg.SaveJpeg(ms, wbitmp.PixelWidth, wbitmp.PixelHeight, 0, 100);
                    ms.Seek(0, SeekOrigin.Begin);
                    bytearray = ms.GetBuffer();
                }
                string str = Convert.ToBase64String(bytearray);



String to Stream and then to Image :

byte[] data = Convert.FromBase64String(str);
                        
            Stream memStream = new MemoryStream(data);

            WriteableBitmap wbimg = PictureDecoder.DecodeJpeg(memStream);

            image1.Source = wbimg;



thanks
 
Share this answer
 
Comments
xbolslock 24-Apr-12 21:13pm    
hi! i tried your solution but all i get back is a black image when i convert from string to stream and then to image. how do i go around solving it?? thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900