11

Possible Duplicate:
converting a base 64 string to an image and saving it

I have a PNG image encoded as a Base64 string. I need to convert this string into PNG format. How can I convert this in C#?

Community
  • 1
  • 1
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322

1 Answers1

27
using Convert=System.Convert;
using MemoryStream=System.IO.MemoryStream;
using Image=System.Drawing.Image;
//...

byte[] data = Convert.FromBase64String(base64String);
using(var stream = new MemoryStream(data, 0, data.Length))
{
  Image image = Image.FromStream(stream);
  //TODO: do something with image
}
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • 1
    `using System.Drawing;` – adinas Nov 12 '19 at 11:56
  • 1
    Note that from .NET 6 and above System.Drawing will not be supported on Linux https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only – Hantick May 08 '23 at 12:44