Skip to content

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

@dg2k

Description

@dg2k

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions