How to know the dimensions of an image that is in array of bytes or Base64?

Asked

Viewed 704 times

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 (array of bytes, and then Base64). But I need to know which dimensions of the created image. (And as a plus, if possible, resize it).

1 answer

2


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

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

Stayed 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 + "' />";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.