LearnOpenTK icon indicating copy to clipboard operation
LearnOpenTK copied to clipboard

("Textures" page) ImageSharp's GetPixelSpan() appears to have been removed/replaced

Open DoctorLogiq opened this issue 4 years ago • 1 comments

Hi all, Some of the information on the "Textures" page of the Learn OpenTK page may have fallen out of date, as unless my IDE (Visual Studio) is just being the usual bag of uselessness that it often is, one of the ImageSharp functions has changed.

It appears you can no longer create a byte array simply by using image.GetPixelSpan().ToArray().

Possibly related to: https://github.com/SixLabors/ImageSharp/issues/1164.

DoctorLogiq avatar Jun 24 '20 08:06 DoctorLogiq

Hey, I'm also learning now using the documentation and noticed this. I've checked the current API of ImageSharp and updated the code in my project, so until they update it, you can use it:

Image<Rgba32> image = Image.Load<Rgba32>(imagePath);
image.Mutate(x => x.Flip(FlipMode.Vertical));
			
var pixels = new List<byte>(4 * image.Width * image.Height);

for (int y = 0; y < image.Height; y++) {
	var row = image.GetPixelRowSpan(y);

	for (int x = 0; x < image.Width; x++) {
		pixels.Add(row[x].R);
		pixels.Add(row[x].G);
		pixels.Add(row[x].B);
		pixels.Add(row[x].A);
	}
}

I also gave the list constructor a capacity value because I know how big the list will be

Sioma112233 avatar Jun 25 '20 20:06 Sioma112233