如何确定绘制的线条和绘制的图像的颜色?

时间:2019-10-23 17:07:37

标签: c#

我有以下代码:

private void DrawImage(PaintEventArgs e)
{          
    newImage = Image.FromFile(bmpPath);
    e.Graphics.DrawImage(newImage, new Rectangle(0, 0, 200, 200));
    e.Graphics.DrawLine(new Pen(Color.Yellow, 20), 20, 20, 200, 200);

}

我该如何按颜色找到交点,我的意思是与我的交点 画线和我的画图?

private void Button2_Click(object sender, EventArgs e)
{
    Bitmap b = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.Height);
    pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);

    int[,] pixels = new int[pictureBox1.Width, pictureBox1.Height];
    for (int i = 0; i < pictureBox1.Width; i++)
    {
        for (int j = 0; j < pictureBox1.Height; j++)
        {
            if(Color.Black == b.GetPixel(i,j) && Color.Red == b.GetPixel(i,j))
            {
                count++;
            }
        }
    }         
}

1 个答案:

答案 0 :(得分:0)

位图具有每个像素的数组,这是一个包含有关像素信息的对象。您可以利用它来发挥自己的优势。 像素具有3个通道,其中包含有关红色,绿色,蓝色强度的信息。

public Color GetPixel(int x, int y)
{
    Color clr = Color.Empty;

    // Get color components count
    int cCount = Depth / 8;

    // Get start index of the specified pixel
    int i = ((y * Width) + x) * cCount;

    if (i > Pixels.Length - cCount)
        throw new IndexOutOfRangeException();

    if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
    {
        byte b = Pixels[i];
        byte g = Pixels[i + 1];
        byte r = Pixels[i + 2];
        byte a = Pixels[i + 3]; // a
        clr = Color.FromArgb(a, r, g, b);
    }
    if (Depth == 24) // For 24 bpp get Red, Green and Blue
    {
        byte b = Pixels[i];
        byte g = Pixels[i + 1];
        byte r = Pixels[i + 2];
        clr = Color.FromArgb(r, g, b);
    }
    if (Depth == 8)
    // For 8 bpp get color value (Red, Green and Blue values are the same)
    {
        byte c = Pixels[i];
        clr = Color.FromArgb(c, c, c);
    }
    return clr;
}