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. How to convert an image to a Base64 string and back?

How to convert an image to a Base64 string and back?

Scheduled Pinned Locked Moved C#
graphicshelptutorialquestion
5 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.
  • M Offline
    M Offline
    Megidolaon
    wrote on last edited by
    #1

    I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:

    ImageConverter converter = new ImageConverter();
    byte[] raw = new byte[1];
    Bitmap test = new Bitmap(OFD_Open.FileName);

    raw = (byte[])converter.ConvertTo(test, typeof(byte[]));

    string output = Convert.ToBase64String(raw);
    File.WriteAllText("image.txt", output);

    And this code to convert it from a string back to an image

    byte[] raw = new byte[1];
    Bitmap pic = new Bitmap(1, 1);
    raw = Convert.FromBase64String(OFD_Open.FileName);
    PictureBox box = new PictureBox();

    TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
    pic = (Bitmap)tc.ConvertFrom(raw);

    But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.

    S S L M 4 Replies Last reply
    0
    • M Megidolaon

      I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:

      ImageConverter converter = new ImageConverter();
      byte[] raw = new byte[1];
      Bitmap test = new Bitmap(OFD_Open.FileName);

      raw = (byte[])converter.ConvertTo(test, typeof(byte[]));

      string output = Convert.ToBase64String(raw);
      File.WriteAllText("image.txt", output);

      And this code to convert it from a string back to an image

      byte[] raw = new byte[1];
      Bitmap pic = new Bitmap(1, 1);
      raw = Convert.FromBase64String(OFD_Open.FileName);
      PictureBox box = new PictureBox();

      TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
      pic = (Bitmap)tc.ConvertFrom(raw);

      But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Hi, the Convert.FromBase64String expect a base64-encoded string, which is in your case stored within a file. But you pass the name of the file. You have to pass the content of the file!

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      1 Reply Last reply
      0
      • M Megidolaon

        I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:

        ImageConverter converter = new ImageConverter();
        byte[] raw = new byte[1];
        Bitmap test = new Bitmap(OFD_Open.FileName);

        raw = (byte[])converter.ConvertTo(test, typeof(byte[]));

        string output = Convert.ToBase64String(raw);
        File.WriteAllText("image.txt", output);

        And this code to convert it from a string back to an image

        byte[] raw = new byte[1];
        Bitmap pic = new Bitmap(1, 1);
        raw = Convert.FromBase64String(OFD_Open.FileName);
        PictureBox box = new PictureBox();

        TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
        pic = (Bitmap)tc.ConvertFrom(raw);

        But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #3

        Megidolaon wrote:

        raw = Convert.FromBase64String(OFD_Open.FileName);

        I think converting the filename won't help much... If I were you I would convert the content! From the top of my mind perhaps this will work: Convert.FromBase64String(File.ReallAllText(OFD_Open.FileName));

        A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

        1 Reply Last reply
        0
        • M Megidolaon

          I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:

          ImageConverter converter = new ImageConverter();
          byte[] raw = new byte[1];
          Bitmap test = new Bitmap(OFD_Open.FileName);

          raw = (byte[])converter.ConvertTo(test, typeof(byte[]));

          string output = Convert.ToBase64String(raw);
          File.WriteAllText("image.txt", output);

          And this code to convert it from a string back to an image

          byte[] raw = new byte[1];
          Bitmap pic = new Bitmap(1, 1);
          raw = Convert.FromBase64String(OFD_Open.FileName);
          PictureBox box = new PictureBox();

          TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
          pic = (Bitmap)tc.ConvertFrom(raw);

          But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.

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

          Hi, I have no experience with ImageConverter and have never seen any code using it. What most people do is save the image to a MemoryStream, get its byte array, call Convert.ToBase64String; then do the opposite to get it back. The advantage is the code really is very symmetrical, which is great as you want it to behave symmetrically too. BTW: your initializations with dummy data/sizes don't make sense, just leave them out. I mean the new byte[1] and new Bitmap(1,1) :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          1 Reply Last reply
          0
          • M Megidolaon

            I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:

            ImageConverter converter = new ImageConverter();
            byte[] raw = new byte[1];
            Bitmap test = new Bitmap(OFD_Open.FileName);

            raw = (byte[])converter.ConvertTo(test, typeof(byte[]));

            string output = Convert.ToBase64String(raw);
            File.WriteAllText("image.txt", output);

            And this code to convert it from a string back to an image

            byte[] raw = new byte[1];
            Bitmap pic = new Bitmap(1, 1);
            raw = Convert.FromBase64String(OFD_Open.FileName);
            PictureBox box = new PictureBox();

            TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
            pic = (Bitmap)tc.ConvertFrom(raw);

            But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.

            M Offline
            M Offline
            Megidolaon
            wrote on last edited by
            #5

            :doh: Wow, that was really stupid. I totally forgot to read the file content. Thanks. It really helps to have someone else look at your code, you sometimes overlook the most basic things.

            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