简体   繁体   中英

Image from Base64 to Image

I am asking the user to upload an image, which can be any image format.

I pass the base64 data to my .Net controller, as well as the filename (From which, I can get the extension).

I am then converting that base64 string into an Image.

    public static Image Base64ToImage(string base64String)
    {

        // Convert base 64 string to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        // Convert byte[] to Image
        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            Image image = Image.FromStream(ms, true);
            return image;
        }
    }

I get an error:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters

I think it's because the base54 string has:

"data:image/jpeg;base64,/9j/4AAQSkZJRgABA ...." at the start.

So I need to trim off the "data:image/jpeg;base64,", and then save it? As I am removing data about the file type, do I need to specify it somewhere else?

All the example I find don't seem to trim anything off the front. They just save the string. I'm guessing, something on my side is adding that header?

Hi Please find a version of working converters i had been using in the past, Do tell if you need more explanation.

Seems like your conversion to base64 is the culprit!

       /// <summary>
    ///1 Convert String to Image
    /// </summary>
    /// <param name="commands"></param>
    /// <returns></returns>

    public Image ConvertStringtoImage(string commands)
    {

        byte[] photoarray = Convert.FromBase64String(commands);
        MemoryStream ms = new MemoryStream(photoarray, 0, photoarray.Length);
        ms.Write(photoarray, 0, photoarray.Length);
        Image image = System.Drawing.Image.FromStream(ms);
        return image;

    }


    /// <summary>
    ///2. Read picture from Database and return as image
    /// just change the mysql to sql server type.
    /// </summary>
    /// <param name="commands"></param>
    /// <returns></returns>

    public Image Readphotofromdb(string commands)
    {
        Image image = null;
        using (MySqlConnection dbConn = new MySqlConnection(connector))
        {
            using (MySqlCommand cmd = new MySqlCommand(commands, dbConn))
            {
                dbConn.Open();
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        byte[] photoarray = Convert.FromBase64String(reader.GetString(0));
                        MemoryStream ms = new MemoryStream(photoarray, 0, photoarray.Length);
                        ms.Write(photoarray, 0, photoarray.Length);
                        image = new Bitmap(ms);

                    }
                }
            }
        }
        MySqlConnection.ClearAllPools();
        return image;

    }

    /// <summary>
    /// 3. Convert Image to base64 string
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>


    public string ConvertImageToString(Image image)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Close();
            byteArray = stream.ToArray();
        }
        string base64String = Convert.ToBase64String(byteArray);
        return base64String;
    }

use image data like /9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQ......9= instead of data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQ.. ....9=

 private void GetImage(){
  string imagedata="/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQ.. 
   ....9=";
  Image image=ImageFromBase64(imagedata);
}
public static Image ImageFromBase64(string base64String)
    {
                
        byte[] imageBytes = Convert.FromBase64String(base64String);
         
        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            Image image = Image.FromStream(ms, true);
            return image;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM