0

I m trying to convert base64 string into image using below code

 byte[] imageBytes = Convert.FromBase64String(base64String);
 MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

 // Convert byte[] to Image
 ms.Write(imageBytes, 0, imageBytes.Length);
 Image image = Image.FromStream(ms, true);

but it is always giving me an error "parameter is not valid."

And also I want to store image in folder after conversion.

2
  • On what line do you get the error? Also, use using to prevent memory leaking. Commented Aug 11, 2014 at 7:31
  • Do you need it as an Image object in memory or is your only goal to store it on disk? Commented Aug 11, 2014 at 7:44

1 Answer 1

1

You do not Need

ms.Write(imageBytes, 0, imageBytes.Length);

and for your stream you can use:

MemoryStream ms = new MemoryStream(imageBytes)

thats all i think.

Remember to use using blocks for your MemoryStream

Image image;
using (var ms= new MemoryStream(Convert.FromBase64String(base64String)))
{
  image = Image.FromStream(ms, true);
}

To store your Image in FileSytem just use:

image.Save("c:\\myimage.bmp");
Sign up to request clarification or add additional context in comments.

5 Comments

Maybe your Base64 string is not a valid base64 string ? Do you get the Exception on Convert.FromBase64String() ?
No. I m not getting any exception. but tell me can it happen when i will try to convert only half string?
@DipaliWagh you have to convert the full base64 string
Thanx @S.L. Its working now. but the image is blured
@DipaliWagh if you mean the saved Image ... try to find the right Format and use the Parameters for Image.Save() method. msdn.microsoft.com/en-US/library/… ... have a look @ the overloads, but thats another question :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.