简体   繁体   English

C#打印像素值

[英]C# print pixel value

I have a 8 bit bitmap color image. 我有一个8位位图彩色图像。 when i do a 当我做的时候

Color pixelcolor = b.GetPixel(j,i);    
Console.Write(pixelcolor.ToString() + " " );

I get 我明白了

 Color [A=255, R=255, G=255, B=255]

I need to get only the 8 bit value. 我只需要获得8位值。 not 24 bit seperate values for R,G,B ,A. 不是R,G,B,A的24位单独值。

There is no way to do this using the Bitmap class directly. 直接使用Bitmap类无法做到这一点。 However, you can use the LockBits method to access the pixels directly. 但是,您可以使用LockBits方法直接访问像素。

Using unsafe code: (remember to enable unsafe code in your project first) 使用不安全的代码:(记得先在项目中启用不安全的代码

public static unsafe Byte GetIndexedPixel(Bitmap b, Int32 x, Int32 y)
{
    if (b.PixelFormat != PixelFormat.Format8bppIndexed) throw new ArgumentException("Image is not in 8 bit per pixel indexed format!");
    if (x < 0 || x >= b.Width) throw new ArgumentOutOfRangeException("x", string.Format("x should be in 0-{0}", b.Width));
    if (y < 0 || y >= b.Height) throw new ArgumentOutOfRangeException("y", string.Format("y should be in 0-{0}", b.Height));
    BitmapData data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
    try
    {
        Byte* scan0 = (Byte*)data.Scan0;
        return scan0[x + y * data.Stride];
    }
    finally
    {
        if (data != null) b.UnlockBits(data);
    }
}

The safe alternative, using Marshal.Copy : 使用Marshal.Copy的安全替代方案:

public static Byte GetIndexedPixel(Bitmap b, Int32 x, Int32 y)
{
    if (b.PixelFormat != PixelFormat.Format8bppIndexed) throw new ArgumentException("Image is not in 8 bit per pixel indexed format!");
    if (x < 0 || x >= b.Width) throw new ArgumentOutOfRangeException("x", string.Format("x should be in 0-{0}", b.Width));
    if (y < 0 || y >= b.Height) throw new ArgumentOutOfRangeException("y", string.Format("y should be in 0-{0}", b.Height));
    BitmapData data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, b.PixelFormat);
    try
    {
        Byte[] pixel = new Byte[1];
        Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + x + y * data.Stride), pixel, 0, 1);
        return pixel[0];
    }
    finally
    {
        if (data != null) b.UnlockBits(data);
    }
}

The methods in the Bitmap class doesn't let you get the palette index directly. Bitmap类中的方法不允许直接获取调色板索引。

You can get the palette for the image using the Palette property, and look for the color there, but that's a bit of a workaround. 您可以使用Palette属性获取图像的Palette ,并在那里查找颜色,但这是一种解决方法。

To get the palette index directly, you would use the LockBits method to get access to the image data directly. 要直接获取调色板索引,可以使用LockBits方法直接访问图像数据。 You would either have to use marshalling to copy the data into an array, or use pointers in unsafe mode to access it. 您可能必须使用编组将数据复制到数组中,或者使用不安全模式下的指针来访问它。


The A property in a Color value is the Alpha component. Color值中的A属性是Alpha组件。 It can have the value 0 to 255, where 0 is fully transparent and 255 is fully solid. 它的值可以是0到255,其中0表示完全透明,255表示完全透明。

If you don't want to use LockBits, you can do this: 如果您不想使用LockBits,可以执行以下操作:

Warning : This method only works if the palette does not have duplicated values and if it is not changed by another thread after pixelRGB is set. 警告 :此方法仅在调色板没有重复值且在设置pixelRGB后未被另一个线程更改时才有效。

/// <summary>
/// Gets the pixel value in bytes. Uses Bitmap GetPixel method.
/// </summary>
/// <param name="bmp">Bitmap</param>
/// <param name="location">Pixel location</param>
/// <returns>Pixel value</returns>
public static byte Get8bppImagePixel(Bitmap bmp, Point location)
{
    Color pixelRGB = bmp.GetPixel(location.X, location.Y);
    int pixel8bpp = Array.IndexOf(bmp.Palette.Entries, pixelRGB);
    return (byte)pixel8bpp;
}

The values you want are actually R , G and B , which are 8bit bitmap values of corresponding Red , Green and Blue components of the color. 您想要的值实际上是RGB ,它们是相应的颜色的RedGreenBlue分量的8位位图值。

A is a Alfa coponent, the transparency value of the color. AAlfa缔合物,颜色的透明度值。 If you don't care about it, just don't show it in string output. 如果您不关心它,只是不要在字符串输出中显示它。

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

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