用鼠标选择图像。 [英] Selection on image by mouse.

查看:79
本文介绍了用鼠标选择图像。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在图像上绘制矩形来选择某个区域并获取像素值(在列表框中显示)并将像素设置为其他颜色。我该怎么办?

I want to select certain area in the by drawing a rectangular on the image and the get the pixel value(display in listbox) and set the pixel to other colors. What should i do?

推荐答案

我想通过在图像上绘制一个矩形来选择某个区域并获取像素值(在列表框中显示) )并将像素设置为其他颜色。我该怎么办?
I want to select certain area in the by drawing a rectangular on the image and the get the pixel value(display in listbox) and set the pixel to other colors. What should i do?

这里是你如何画一个矩形并在列表框中显示它的规格。对于(选择)矩形的上下文中的Pixel值是什么意思?

here's how you could draw a rectangle and display the specs of it in a listbox. What do you mean with Pixel value in the context of a (selection) Rectangle?

    public partial class Form1 : Form
    {
        Random _rnd = new Random();
        private int _x = 0;
        private int _y = 0;
        private int _w = 0;
        private int _h = 0;

        public Form1()
        {
            InitializeComponent();
            //usually done by designer
            Init();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //generate a bitmap with some content
            Bitmap bmp = new Bitmap(this.pictureBox1.ClientSize.Width, this.pictureBox1.ClientSize.Height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                g.Clear(Color.Green);

                for (int i = 0; i < 10; i++)
                {
                    using (SolidBrush sb = new SolidBrush(Color.FromArgb(255, _rnd.Next(256), _rnd.Next(256), _rnd.Next(256))))
                        g.FillEllipse(sb, new Rectangle(_rnd.Next(bmp.Width / 4 * 3), _rnd.Next(bmp.Height / 4 * 3), _rnd.Next(bmp.Width / 4), _rnd.Next(bmp.Height / 4)));
                }
            }

            this.pictureBox1.Image = bmp;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //cleanup
            if (this.pictureBox1.Image != null)
                this.pictureBox1.Image.Dispose();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //set x and y of the rectangle
            if (e.Button == MouseButtons.Left)
            {
                _x = e.X;
                _y = e.Y;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            //set width and height of the rectangle
            if (e.Button == MouseButtons.Left)
            {
                _w = e.X - _x;
                _h = e.Y - _y;

                //paint the rectangle
                this.pictureBox1.Invalidate();
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            //paint the rectangle
            if (_w > 0 && _h > 0)
            {
                e.Graphics.DrawRectangle(Pens.Red, new Rectangle(_x, _y, _w, _h));
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            //add to list
            if (_w > 0 && _h > 0)
                this.listBox1.Items.Add(new Rectangle(_x, _y, _w, _h).ToString());
        }

        //###############################################################
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.ListBox listBox1;

        private void Init()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.listBox1 = new System.Windows.Forms.ListBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(26, 25);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(512, 512);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(544, 25);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(251, 108);
            this.listBox1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(807, 581);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
        }
    }

问候,

  Thorsten

  Thorsten


这篇关于用鼠标选择图像。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆