Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. image to string and backwords..

image to string and backwords..

Scheduled Pinned Locked Moved C#
graphicsdata-structureshelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seishin
    wrote on last edited by
    #1

    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!!!

    L S A 3 Replies Last reply
    0
    • S Seishin

      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!!!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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]

      S 1 Reply Last reply
      0
      • S Seishin

        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!!!

        S Offline
        S Offline
        Stefan Prodan
        wrote on last edited by
        #3

        Look at the BitConverter class, BitConverter.ToString Method (Byte[]).

        http://stefanprodan.wordpress.com

        S 1 Reply Last reply
        0
        • L Luc Pattyn

          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]

          S Offline
          S Offline
          Seishin
          wrote on last edited by
          #4

          That's it!! Thanks a lot!!!

          life is study!!!

          1 Reply Last reply
          0
          • S Stefan Prodan

            Look at the BitConverter class, BitConverter.ToString Method (Byte[]).

            http://stefanprodan.wordpress.com

            S Offline
            S Offline
            Seishin
            wrote on last edited by
            #5

            thanks, but there is no overload for GetBytes(string s) in BitConverter class..

            life is study!!!

            S 1 Reply Last reply
            0
            • S Seishin

              thanks, but there is no overload for GetBytes(string s) in BitConverter class..

              life is study!!!

              S Offline
              S Offline
              Stefan Prodan
              wrote on last edited by
              #6

              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");

              http://stefanprodan.wordpress.com

              1 Reply Last reply
              0
              • S Seishin

                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!!!

                A Offline
                A Offline
                AFSEKI
                wrote on last edited by
                #7

                if you use "using keyword" then the MemoryStream, which is in this case your raw image data, disposed. Copy the ms.ToArray() to a new byte[] by Array.Copy(...) method before you exit "using(...) block". Hope this helps...

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups