How to know the dimensions of an image that is in byte array or base64?

2

I have the following code:

var binario = _documentoWord.Content.EnhMetaFileBits;
string img64Bytes = "";

var base64 = Convert.ToBase64String(binario, 0, binario.Length);
byte[] imageBytesBase64 = Convert.FromBase64String(base64);

MemoryStream ms64 = new MemoryStream(imageBytesBase64);
Image img = Image.FromStream(ms64);

using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, ImageFormat.Png);

    byte[] imageBytes = ms.ToArray();

    img64Bytes = Convert.ToBase64String(imageBytes);
}

var url = "<img src='data:image/png;base64," + img64Bytes + "' />";

In this code I transform a word file into an image (byte array, and then base64). But I need to know what dimensions the image created. (And as plus, if possible, resize it.)

    
asked by anonymous 24.07.2015 / 13:35

1 answer

2

I found out. I insert these two lines inside the using, it can be later also:

var largura = img.Width.ToString();
var altura = img.Height.ToString();

It looks like this:

var binario = _documentoWord.Content.EnhMetaFileBits;
string img64Bytes = "";

var base64 = Convert.ToBase64String(binario, 0, binario.Length);
byte[] imageBytesBase64 = Convert.FromBase64String(base64);

MemoryStream ms64 = new MemoryStream(imageBytesBase64);
Image img = Image.FromStream(ms64);

using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, ImageFormat.Png);

    var largura = img.Width.ToString();
    var altura = img.Height.ToString();

    byte[] imageBytes = ms.ToArray();

    img64Bytes = Convert.ToBase64String(imageBytes);
}

var url = "<img src='data:image/png;base64," + img64Bytes + "' />";
    
24.07.2015 / 14:05