24h購物| | PChome| 登入
2011-01-04 14:54:39| 人氣16,325| 回應2 | 上一篇 | 下一篇

[C#] 螢幕像素的座標與顏色

推薦 0 收藏 0 轉貼0 訂閱站台



重點:

1. using System.Drawing.Imaging

 2.加入Timer timer1. ///Interval : 10ms

      this.timer1.Enabled = true;
      this.timer1.Interval = 10;

 3. GetPixelColor( ) 函式讀取滑鼠座標及像素顏色.

 

程式碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CursorPosition
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

          private void timer1_Tick(object sender, EventArgs e)      
        {////Timer, 每10ms讀取滑鼠座標值及像素顏色並顯示於 PictureBox背景
            Color color = GetPixelColor(Cursor.Position);
            lblCoordX.Text = "X = " + Cursor.Position.X.ToString();
            lblCoordY.Text = "Y = " + Cursor.Position.Y.ToString();
            lblA.Text = " A = " + color.A;
            lblR.Text = " R = " + color.R;
            lblG.Text = " G = " + color.G;
            lblB.Text = " B = " + color.B;
            pictureBox1.BackColor = color;
        }    


    static Color GetPixelColor(Point position)
        {
            using (var bitmap = new Bitmap(1, 1))
            {
                using (var graphics = Graphics.FromImage(bitmap))
                {
                    graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
                }
                return bitmap.GetPixel(0, 0);
            }
        }    }
}

參考資料:

Color of a screen pixel

 

台長: Chris C.S Huang
人氣(16,325) | 回應(2)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: C# |
此分類上一篇:[C#] 列示目錄下的檔案

(悄悄話)
2014-12-04 20:20:07
(悄悄話)
2014-12-04 20:25:21
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文