C# 获取图片某像素点RGB565值

Project Source Download: http://download.csdn.net/detail/mostone/6360007



    public partial class FormMain : Form
    {

        Bitmap bmpZoom = null;
        Bitmap bmpSrc = null;

        public FormMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult isDone = openFileDialog1.ShowDialog();
                if (isDone == DialogResult.OK)
                {
                    pictureBox1.Image = null;
                    pictureBox2.Image = null;
                    bmpSrc = null;
                    bmpZoom = null;

                    Image img = Bitmap.FromFile(openFileDialog1.FileName);
                    pictureBox1.Image = img;
                    bmpSrc = new Bitmap(img);
                }
            }
            catch (Exception ex)
            {
                pictureBox1.Image = null;
                bmpSrc = null;
                MessageBox.Show(ex.Message);
            }

        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            const int zoomSize = 8;
            if (this.bmpSrc == null)
            {
                return;
            }

            bmpZoom = null;
            bmpZoom = new Bitmap(pictureBox2.Width, pictureBox2.Height);
            Graphics grpDst = Graphics.FromImage(bmpZoom);

            // zoom to 8x
            int width = pictureBox2.Width / zoomSize;
            int height = pictureBox2.Height / zoomSize;

            int offsetX = width / 2;
            int offsetY = height / 2;

            int x = e.X - offsetX;
            int y = e.Y - offsetY;
            if (offsetX + e.X >= bmpSrc.Width)
            {
                x = bmpSrc.Width - offsetX * 2;
            }
            else if (x < 0)
            {
                x = 0;
            }

            if (offsetY + e.Y >= bmpSrc.Height)
            {
                y = bmpSrc.Height - offsetY * 2;
            }
            else if (y < 0)
            { 
                y = 0;
            }

            Color color;
            int oriX = x;
            for (int row = 0; row < pictureBox2.Height; row += zoomSize)
            {
                if (y >= bmpSrc.Height) break;

                for (int col = 0; col < pictureBox2.Width; col += zoomSize)
                {
                    if (x >= bmpSrc.Width) break;

                    // get pixel color
                    color = bmpSrc.GetPixel(x, y);
                    // draw zoom block
                    grpDst.FillRectangle(new SolidBrush(color), col, row, zoomSize, zoomSize);
                    x++;
                }
                x = oriX;
                y++;
            }

            pictureBox2.Image = bmpZoom;

        }

        private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
        {
            if (bmpZoom == null) return;

            Color color = bmpZoom.GetPixel(e.X, e.Y);
            labelColor.BackColor = color;
            String val = color.ToArgb().ToString("X");
            textBox1.Text = "#" + val.Substring(2);
            textBox2.Text = "#" + rgb565FromColor(color).ToString("X");
            textBox3.Text = "#" + rgb565PFromColor(color).ToString("X");
        }

        private int rgb565FromColor(Color color)
        {
            int val = color.B >> 3 << 11;
            val |= color.G >> 2 << 5;
            val |= color.R >> 3;

            return val;
        }

        private int rgb565PFromColor(Color color)
        {
            int val = color.R >> 3 << 11;
            val |= color.G >> 2 << 5;
            val |= color.B >> 3;

            return val;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(@"Image Color Picker

    by mostone@hotmail.com
    http://blog.csdn.net/mostone
    2013-10-06", "About...");
        }
    }
}


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值