Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:11:05+00:00 2026-06-10T19:11:05+00:00

I am getting an exception when trying to convert a base64 string to a

  • 0

I am getting an exception when trying to convert a base64 string to a byte array. I am converting an Image to a byte array then to a base64 string, then encrypting it and storing it in a file. Then I am attempting to convert the base64 string back to a byte array in a MemoryStream, and recreating the image. I am getting a FormatException here:

byte[] imgBytes = Convert.FromBase64String(str);

Here is the full code for the two main functions:

public string ImageToString(Image img)
{
     using (MemoryStream ms = new MemoryStream())
     {
          img.Save(ms, ImageFormat.Jpeg);

          return Convert.ToBase64String(ms.ToArray());
     }
}

public Image StringToImage(String str)
{            
     int lent = str.Length;
     byte[] imgBytes = Convert.FromBase64String(str);
     MemoryStream ms = new MemoryStream(imgBytes, 0, imgBytes.Length);

     ms.Write(imgBytes, 0, imgBytes.Length);
     return Image.FromStream(ms, true);
}

Here is the beginning and end of the base64 string I am trying to convert….
G>/9j/4AAQSkZJRgABAQEAYABgAAD .... Uh+8fxpT/B9KAP/2Q==

Any ideas are greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:11:07+00:00Added an answer on June 10, 2026 at 7:11 pm

    The problem is that your string got corrupted somewhere along the line. That’s not a base64 string, as you can see by the second charcter >, which does not occur in a base64 string.


    Side note: Your function creates a memory stream containing the data, then writes the data to the memory stream again. Then you try to read from the memory stream without resetting the position to the beginning of the stream.

    Just create the memory stream and read from it:

    public Image StringToImage(String str) {            
      byte[] imgBytes = Convert.FromBase64String(str);
      return Image.FromStream(new MemoryStream(imgBytes), true);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert string to double. But i am getting the number
I am getting a String.FormatException trying to convert/parse a string when the culture is
I am getting this exception when trying to run my PHP unit tests: Fatal
Hey guys I am getting this exception when trying to run the query Column
Iam trying to authenticate to https url , but iam getting exception . Below
I'm getting a very weird exception when trying to deploy my app on JBOSS
I'm trying out an implementation of QuickSort but getting an Exception in thread main
I'm getting a Message signature was incorrect exception when trying to authenticate with MyOpenID
trying to use hibernate with my web app and getting following exception: Initial SessionFactory
I'm trying to parse and load an XML document, however I'm getting this exception

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.