I have the following code:
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);
}
How can I find the intersection by color, I mean intersection with my drawn line and with my drawn image?
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++;
}
}
}
}
Bitmap have array of every pixel which is an object that contains information about pixels. You can use it for your advantage. Pixel have 3 channels which contains informations about intensity of red, green, blue as channels.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.