winforms 如何在鼠标移动事件中真实的地在pictureBox1中的图像上绘制红色像素?

whhtz7ly  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(124)

我尝试在移动鼠标时绘制红色像素,以便鼠标光标位于pictureBox1内的位置在该点绘制一个像素。

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            pictureBox1.Image = Properties.Resources._image;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);

            Bitmap bmp = new Bitmap(pictureBox1.Image);

            for(int x = 0; x < bmp.Width; x++)
            {
                for(int y = 0; y < bmp.Height; y++)
                {
                    bmp.SetPixel(e.X, e.Y, Color.Red);
                }
            }

            bmp.Dispose();
        }

这是非常慢的,并且不能以红色绘制像素。

ruarlubt

ruarlubt1#

这在鼠标移动事件中起作用:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
            Bitmap bmp = new Bitmap(pictureBox1.Image);
            bmp.SetPixel(e.X, e.Y, Color.Red);
            pictureBox1.Image = bmp;
        }

相关问题