Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dg2k opened this issue May 10, 2023 · 0 comments

Comments

@dg2k
Copy link

dg2k commented May 10, 2023

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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant