本小程序功能:在图片上取某一点的颜色

主要代码如下:

using System;

using System.Diagnostics;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using bitmapTest.Properties;


namespace bitmapTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        Bitmap myBitmap;

        int cmd = 0;


        private void Form1_Load(object sender, EventArgs e)

        {

            pictureBox1.Image = Resources.bfq0002;

            myBitmap = (Bitmap)pictureBox1.Image;

        }

        /// <summary>

        /// 鼠标取色(移动)

        /// </summary>

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

        {

            int mouseX = e.X;

            int mouseY = e.Y;

            txtPoint.Text = "(" + mouseX + "," + mouseY + ")";

            if (rbtmutil.Checked)

            {

                //取色

                try

                {

                    Color pixelColor = myBitmap.GetPixel(mouseX, mouseY);

                    colorbox.BackColor = pixelColor;

                    txtColorValue.Text = System.Drawing.ColorTranslator.ToHtml(pixelColor);

                }

                catch

                { }

            }


            //命令

            switch (cmd)

            {

                case 1:  //画线

                    SolidBrush pixelBrush = new SolidBrush(Color.Red);

                    Graphics g = Graphics.FromImage(myBitmap);

                    g.FillRectangle(pixelBrush, mouseX, mouseY, 2, 2);

                    pictureBox1.Image = myBitmap;

                    break;

                case 2:

                    break;

                default:

                    break;

            }

        }

        /// <summary>

        /// 画笔

        /// </summary>

        private void btnpen1_Click(object sender, EventArgs e)

        {

            if (cmd == 1)

            {

                cmd = 0;

                btnpen1.Image = Resources.png_05252;

                this.toolTip1.SetToolTip(this.btnpen1, "单击使用画笔");

            }

            else

            {

                cmd = 1;

                btnpen1.Image = Resources.png_05253;

                this.toolTip1.SetToolTip(this.btnpen1, "单击禁用画笔");

            }

        }

        /// <summary>

        /// 恢复

        /// </summary>

        private void button1_Click(object sender, EventArgs e)

        {

            pictureBox1.Image = Resources.bfq0002;

            myBitmap = (Bitmap)pictureBox1.Image;

        }

        /// <summary>

        /// 鼠标取色(单击)

        /// </summary>

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

        {

            //取色

            try

            {

                Color pixelColor = myBitmap.GetPixel(e.X, e.Y);

                colorbox.BackColor = pixelColor;

                txtColorValue.Text = System.Drawing.ColorTranslator.ToHtml(pixelColor);

            }

            catch

            { }

        }


    }

}


程序界面如下:

图片取色小程序_小程序