image to string and backwords..
-
Hi! i'm trying to convert an image object to string and later bring it back from the string.. My code:
Image image = Image.FromFile(@"C:\08.jpg"); // load some image byte[] obj; using(MemoryStream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //*#* obj = ms.ToArray(); // read it into byte array } string str = System.Text.Encoding.ASCII.GetString(obj); // convert byte[] to string byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);// convert string to byte[] Image newImage; using(MemoryStream ms = new MemoryStream(bytes)) newImage = Image.FromStream(ms); // get image from byte[] through stream.. ***
in the *** line an exception ArgumentException "Parameter is not valid." is thrown.. if in the *#* line i change from Jpeg to Bmp the exception is not thrown but the image is scrambled.. thanks for any help!!life is study!!!
-
Hi! i'm trying to convert an image object to string and later bring it back from the string.. My code:
Image image = Image.FromFile(@"C:\08.jpg"); // load some image byte[] obj; using(MemoryStream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //*#* obj = ms.ToArray(); // read it into byte array } string str = System.Text.Encoding.ASCII.GetString(obj); // convert byte[] to string byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);// convert string to byte[] Image newImage; using(MemoryStream ms = new MemoryStream(bytes)) newImage = Image.FromStream(ms); // get image from byte[] through stream.. ***
in the *** line an exception ArgumentException "Parameter is not valid." is thrown.. if in the *#* line i change from Jpeg to Bmp the exception is not thrown but the image is scrambled.. thanks for any help!!life is study!!!
Hi, it is OK to use a byte[] to hold an image. it is not OK to try and put that byte[] into a string, unless some special conversion is applied that ensures a normal string results. Possible encodings are HEX (which expands each byte to 2 chars, and base64 (see Convert.To/FromBase64String() methods). :)
Luc Pattyn [My Articles]
-
Hi! i'm trying to convert an image object to string and later bring it back from the string.. My code:
Image image = Image.FromFile(@"C:\08.jpg"); // load some image byte[] obj; using(MemoryStream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //*#* obj = ms.ToArray(); // read it into byte array } string str = System.Text.Encoding.ASCII.GetString(obj); // convert byte[] to string byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);// convert string to byte[] Image newImage; using(MemoryStream ms = new MemoryStream(bytes)) newImage = Image.FromStream(ms); // get image from byte[] through stream.. ***
in the *** line an exception ArgumentException "Parameter is not valid." is thrown.. if in the *#* line i change from Jpeg to Bmp the exception is not thrown but the image is scrambled.. thanks for any help!!life is study!!!
Look at the BitConverter class, BitConverter.ToString Method (Byte[]).
-
Hi, it is OK to use a byte[] to hold an image. it is not OK to try and put that byte[] into a string, unless some special conversion is applied that ensures a normal string results. Possible encodings are HEX (which expands each byte to 2 chars, and base64 (see Convert.To/FromBase64String() methods). :)
Luc Pattyn [My Articles]
-
Look at the BitConverter class, BitConverter.ToString Method (Byte[]).
-
thanks, but there is no overload for GetBytes(string s) in BitConverter class..
life is study!!!
Here is my code, I've test it with BMP but I think it will work with any image format.
using System.IO; using System.Drawing; using System.Runtime.Serialization.Formatters.Soap;
Image image = Image.FromFile(file); // load some image SoapFormatter formater = new SoapFormatter(); byte[] obj; using (MemoryStream ms = new MemoryStream()) { formater.Serialize(ms, image); obj = ms.ToArray(); } string str = Convert.ToBase64String(obj); // convert byte[] to string byte[] bytes = Convert.FromBase64String(str);// convert string to byte[] Image newImage; using (MemoryStream ms = new MemoryStream(bytes)) { newImage = (Image)formater.Deserialize(ms); } newImage.Save(file + ".bmp");
-
Hi! i'm trying to convert an image object to string and later bring it back from the string.. My code:
Image image = Image.FromFile(@"C:\08.jpg"); // load some image byte[] obj; using(MemoryStream ms = new MemoryStream()) { image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //*#* obj = ms.ToArray(); // read it into byte array } string str = System.Text.Encoding.ASCII.GetString(obj); // convert byte[] to string byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);// convert string to byte[] Image newImage; using(MemoryStream ms = new MemoryStream(bytes)) newImage = Image.FromStream(ms); // get image from byte[] through stream.. ***
in the *** line an exception ArgumentException "Parameter is not valid." is thrown.. if in the *#* line i change from Jpeg to Bmp the exception is not thrown but the image is scrambled.. thanks for any help!!life is study!!!