简体   繁体   English

使用 ImageSharp 获取图像中特定像素的颜色数据?

[英]Getting the color data of a specific pixel in an image using 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.我也在尝试从外部源加载图像 - 在这种情况下,假设我正在尝试从尺寸为 400x400px 的“abcd.jpg”(或 png 在这里并不重要)中获取颜色。 The color I'm trying to get is of a pixel at 200x200.我想要获得的颜色是 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".但这不起作用,每当我尝试使用 TPixel 或 Rgba32 时,它只会告诉我“变量图像不能与类型参数一起使用”和“找不到类型或命名空间 TPixel”。 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.提前致谢。

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(ie one with a pixel type specified, in this case Rgba32 )因此,您需要调用Image.Load<Rgba32>(..)重载(即指定像素类型的重载,在本例中Rgba32

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM