簡體   English   中英

如何在鼠標移動事件中實時在pictureBox1中的圖像上繪制紅色像素?

[英]How to draw pixels in color red on the image in pictureBox1 in real time on mouse move event?

我正在嘗試在移動鼠標時以紅色繪制像素,以便鼠標 cursor 在 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();
        }

這非常慢,並且不會以紅色繪制像素。

這在鼠標移動事件中起作用:

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;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM