oozcitak / exiflibrary

A .Net Standard library for editing Exif metadata
MIT License
131 stars 48 forks source link

After getting ImageFile object with ImageFile.FromFile("pathToImage"), how to get a byte array Extension? #113

Open dg2k opened 1 year ago

dg2k commented 1 year ago

I have var image = ImageFile.FromFile(pathToImage);

Can an Extension ToByteArray() on ImageFile be easily implemented as shown below? I can easily implement from IO Stream but since the file is already open for getting Exif metadata, I am trying to avoid opening for getting IO Stream.

An alternative question is can ImageFile be converted to MemoryStream which allows me to get to a byte array? Apologies as I am new to ImageFile.

byte[] imageData = image.ToByteArray();

public static class MyExtensions
{
    public static byte[] ToByteArray (this ImageFile imageFile)
    {
        var memoryStream = <##  Get MemoryStream from imageFile ##>

        return memoryStream.ToArray();
    }
}

UPDATE after a few hours:

I think I figured out?? but someone correct me if wrong;

public static byte[] ToByteArray (this ImageFile imageFile)
{
    MemoryStream memStream = new MemoryStream();
    imageFile.Save(memStream );
    return memStream.ToArray();
}