I'm trying to get a pixel colour from the picturebox and painting the panel with the corresponding colour every time i click. How do i display the cursor coordinates on the form as well? Can you tell me what is wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Color colorpt;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Bitmap img = new Bitmap(pictureBox1.Image);
int x = e.X;
int y = e.Y;
textBox1.Text = string.Format("X: {0} Y: {1}", x, y);
if (e.Button == MouseButtons.Left)
{
colorpt = img.GetPixel(x, y);
}
panel1.BackColor = colorpt;
img.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("D:\\1.png");
}
}
}
This is my entire code that i am running.
This is the result: demo of result
Based on my test, I can paint a panel with the obtained color of pixel.
I think there is no need to use Paint event and I suggest that you use textbox to show the
coordinate of the cursor.
You can refer to the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Color colorpt;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Bitmap img = new Bitmap(pictureBox1.Image);
int x = e.X;
int y = e.Y;
textBox1.Text = string.Format("X: {0} Y: {1}", x, y);
if (e.Button == MouseButtons.Left)
{
colorpt = img.GetPixel(x, y);
}
panel1.BackColor = colorpt;
img.Dispose();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("D:\\1.png");
}
}
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.