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


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



    [csharp] view plain copy
    1. public partial class FormMain : Form  
    2. {  
    3.   
    4.     Bitmap bmpZoom = null;  
    5.     Bitmap bmpSrc = null;  
    6.   
    7.     public FormMain()  
    8.     {  
    9.         InitializeComponent();  
    10.     }  
    11.   
    12.     private void button1_Click(object sender, EventArgs e)  
    13.     {  
    14.         try  
    15.         {  
    16.             DialogResult isDone = openFileDialog1.ShowDialog();  
    17.             if (isDone == DialogResult.OK)  
    18.             {  
    19.                 pictureBox1.Image = null;  
    20.                 pictureBox2.Image = null;  
    21.                 bmpSrc = null;  
    22.                 bmpZoom = null;  
    23.   
    24.                 Image img = Bitmap.FromFile(openFileDialog1.FileName);  
    25.                 pictureBox1.Image = img;  
    26.                 bmpSrc = new Bitmap(img);  
    27.             }  
    28.         }  
    29.         catch (Exception ex)  
    30.         {  
    31.             pictureBox1.Image = null;  
    32.             bmpSrc = null;  
    33.             MessageBox.Show(ex.Message);  
    34.         }  
    35.   
    36.     }  
    37.   
    38.     private void pictureBox1_MouseClick(object sender, MouseEventArgs e)  
    39.     {  
    40.         const int zoomSize = 8;  
    41.         if (this.bmpSrc == null)  
    42.         {  
    43.             return;  
    44.         }  
    45.   
    46.         bmpZoom = null;  
    47.         bmpZoom = new Bitmap(pictureBox2.Width, pictureBox2.Height);  
    48.         Graphics grpDst = Graphics.FromImage(bmpZoom);  
    49.   
    50.         // zoom to 8x  
    51.         int width = pictureBox2.Width / zoomSize;  
    52.         int height = pictureBox2.Height / zoomSize;  
    53.   
    54.         int offsetX = width / 2;  
    55.         int offsetY = height / 2;  
    56.   
    57.         int x = e.X - offsetX;  
    58.         int y = e.Y - offsetY;  
    59.         if (offsetX + e.X >= bmpSrc.Width)  
    60.         {  
    61.             x = bmpSrc.Width - offsetX * 2;  
    62.         }  
    63.         else if (x < 0)  
    64.         {  
    65.             x = 0;  
    66.         }  
    67.   
    68.         if (offsetY + e.Y >= bmpSrc.Height)  
    69.         {  
    70.             y = bmpSrc.Height - offsetY * 2;  
    71.         }  
    72.         else if (y < 0)  
    73.         {   
    74.             y = 0;  
    75.         }  
    76.   
    77.         Color color;  
    78.         int oriX = x;  
    79.         for (int row = 0; row < pictureBox2.Height; row += zoomSize)  
    80.         {  
    81.             if (y >= bmpSrc.Height) break;  
    82.   
    83.             for (int col = 0; col < pictureBox2.Width; col += zoomSize)  
    84.             {  
    85.                 if (x >= bmpSrc.Width) break;  
    86.   
    87.                 // get pixel color  
    88.                 color = bmpSrc.GetPixel(x, y);  
    89.                 // draw zoom block  
    90.                 grpDst.FillRectangle(new SolidBrush(color), col, row, zoomSize, zoomSize);  
    91.                 x++;  
    92.             }  
    93.             x = oriX;  
    94.             y++;  
    95.         }  
    96.   
    97.         pictureBox2.Image = bmpZoom;  
    98.   
    99.     }  
    100.   
    101.     private void pictureBox2_MouseClick(object sender, MouseEventArgs e)  
    102.     {  
    103.         if (bmpZoom == nullreturn;  
    104.   
    105.         Color color = bmpZoom.GetPixel(e.X, e.Y);  
    106.         labelColor.BackColor = color;  
    107.         String val = color.ToArgb().ToString("X");  
    108.         textBox1.Text = "#" + val.Substring(2);  
    109.         textBox2.Text = "#" + rgb565FromColor(color).ToString("X");  
    110.         textBox3.Text = "#" + rgb565PFromColor(color).ToString("X");  
    111.     }  
    112.   
    113.     private int rgb565FromColor(Color color)  
    114.     {  
    115.         int val = color.B >> 3 << 11;  
    116.         val |= color.G >> 2 << 5;  
    117.         val |= color.R >> 3;  
    118.   
    119.         return val;  
    120.     }  
    121.   
    122.     private int rgb565PFromColor(Color color)  
    123.     {  
    124.         int val = color.R >> 3 << 11;  
    125.         val |= color.G >> 2 << 5;  
    126.         val |= color.B >> 3;  
    127.   
    128.         return val;  
    129.     }  
    130.   
    131.     private void button2_Click(object sender, EventArgs e)  
    132.     {  
    133.         MessageBox.Show(@"Image Color Picker  
    134.   
    135. by mostone@hotmail.com  
    136. http://blog.csdn.net/mostone  
    137. 2013-10-06", "About...");  
    138.     }  
    139. }  

  • 相关阅读:
    Linux基础_linux发展历史
    Python基础_迭代器、生成器、模块与包
    Python基础_如何用pip安装文件
    Selenium 实现多机器部署分布式执行
    Selenium 日期控件处理和JS定位
    Selenium 无头浏览器
    Selenium 窗口和frame定位
    Selenium 自动化测试模型
    Selenium 验证码处理
    Selenium PageObject设计模式
  • 原文地址:https://www.cnblogs.com/alan666/p/8312276.html
Copyright © 2020-2023  润新知