Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the color data of a specific pixel in an image using ImageSharp?

Tags:

c#

imagesharp

Posted before, but it got closed because I wrote it wrong. I'm trying to get the color of individual pixels in an image. I'm also trying to load the image from an external source - in this case, let's say I'm trying get a color from a 'abcd.jpg' (or png it doesn't really matter here) of dimensions 400x400px. The color I'm trying to get is of a pixel at 200x200. What I tried based on the previous answers was:

var ipath = @"abcd.jpg";
var color;
using (Image image = Image.Load(ipath))
            {
                color = image<TPixel>[200,200];
            }

This doesn't work though, and any time I try to use TPixel or Rgba32 it just tells me that "the variable image cannot be used with type arguments" and "the type or namespace TPixel could not be found". Someone please help, I honestly don't care what format I get the color in I just need to get the color data somehow. Thanks in advance.

like image 903
y3i Avatar asked Oct 29 '25 12:10

y3i


1 Answers

You needed to the load the image with a specific pixel type not use the pixel agnostic version. So you need to call the Image.Load<Rgba32>(..) overload(i.e. one with a pixel type specified, in this case Rgba32)

var ipath = @"abcd.jpg";
Rgba32 color;
using (var image = Image.Load<Rgba32>(ipath))
{
    color = image[200,200];
}
like image 65
tocsoft Avatar answered Nov 01 '25 02:11

tocsoft