2

How to get image size from base64 in C#?

Is it possible to do at all?

Rob
  • 26,483
  • 15
  • 80
  • 92
NoWar
  • 34,755
  • 78
  • 302
  • 475
  • Depends entirely on what image format you're using. Please be more specific – Rob Jan 18 '17 at 23:02
  • @Rob It is PNG and JPG – NoWar Jan 18 '17 at 23:02
  • @Gigabyte You can guess the answer, sure. But it'll just be a guess, which is a problem. From the original post, the following is unknown: Why does base64 matter? (The question may be how to read a base64 into an image format). What image format are you using? What do you mean by size? Dimensions? File size? – Rob Jan 18 '17 at 23:09

1 Answers1

9

Not directly as it depends on what data/format is encoded in the Base64 string. A rough approach would be (code not tested):

byte[] image = Convert.FromBase64String("Your Base64 string here");
using (var ms = new MemoryStream(image))
{
    Image img = Image.FromStream(ms);
    return new Tuple<int, int>(img.Width, img.Height); // or some other data container
}

Note: Image class comes from System.Drawing. See also the MSDN System.Drawing.Image class' documentation documentation.

Gigabyte
  • 522
  • 5
  • 12