2013-03-20 47 views
0

我有裝載了一個位圖,比如一個圖片,照片後:獲取像素顏色從圖片框畫線

Picture1.Image = new Bitmap("photo.bmp");

而且在Picture1_Paint()事件,我畫照片一行:

e.Graphics.DrawLine(myPen, pointA, pointB);

現在,我想顯示一個被點擊的像素的RGB信息:

Bitmap bitmap = (Bitmap) Picture1.Image; /* Making sure I'm using the image being displayed */ 
Color color = bitmap.GetPixel(e.X, e.Y); 
lblSelectedColor.Text = color.R.ToString() + ", " + color.G.ToString() + ", " + color.B.ToString(); 

問題是:我得到的RGB值是原始照片上該像素的顏色,不包括我的線條圖。例如,如果在天空中繪製一條粗紅線,當我點擊那條紅線時,照片上仍然會呈現天藍色。

我想要得到任何在PictureBox中顯示的顏色信息,包括我繪製的線或橢圓。

回答

1

有通過Paint事件或OnPaint方法由控制所佔據的屏幕上繪製之間的差,並在繪製控制內側觀察該位圖。你做前者,但試圖從後者獲得一個像素。

而不是繪製Paint事件,您必須爲您的圖像創建一個Graphics對象並直接繪製。然後將圖像分配給圖片框的Image屬性。

例如,從我的頭頂:

Image image = /* ... */; 
using (Graphics g = Graphics.FromImage(image)) 
{ 
    g.DrawLine(myPen, pointA, pointB); 
} 
picture1.Image = image; 

然後,當你在圖片框的Image對象上做GetPixel,你會得到你剛剛提請線的像素值。

+0

我想再次複製圖像,'位圖位圖=(位圖)Picture1.Image; '會確保圖像包含所有圖形? – user1032613 2013-03-20 02:39:43

+0

@ user1032613不,不。您必須查看屏幕上的圖像與圖片框中的圖像之間的區別。任何窗口和任何控件都可以在屏幕上繪製。你已經給了你的照片箱一張圖片,並且它將拍攝該照片並在你的屏幕上繪製圖片。當您在「Paint」事件中繪製某些東西時,_you正在屏幕上繪圖。當你做'Picture1.Image'的時候,你從你的圖片框中獲得了圖片的圖片,而這不是你在屏幕上看到的圖片。 – Virtlink 2013-03-20 03:23:02

0

使用Bob Powell's Eye Dropper,這裏是代碼:

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


namespace pixelcolor 
{ 
    /// <summary> 
    /// Summary description for Form1. 
    /// </summary> 
    public class Form1 : System.Windows.Forms.Form 
    { 


    [DllImport("Gdi32.dll")] 
    public static extern int GetPixel(
    System.IntPtr hdc, // handle to DC 
    int nXPos, // x-coordinate of pixel 
    int nYPos // y-coordinate of pixel 
    ); 

    [DllImport("User32.dll")] 
    public static extern IntPtr GetDC(IntPtr wnd); 

    [DllImport("User32.dll")] 
    public static extern void ReleaseDC(IntPtr dc); 


    private System.Windows.Forms.Panel panel1; 
    private System.Timers.Timer timer1; 

    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.Container components = null; 

    public Form1() 
    { 
     // 
     // Required for Windows Form Designer support 
     // 
     InitializeComponent(); 
     this.SetStyle(ControlStyles.ResizeRedraw,true); 
    } 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    protected override void Dispose(bool disposing) 
    { 
     if(disposing) 
     { 
     if (components != null) 
     { 
      components.Dispose(); 
     } 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.panel1 = new System.Windows.Forms.Panel(); 
     this.timer1 = new System.Timers.Timer(); 
     ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit(); 
     this.SuspendLayout(); 
     // 
     // panel1 
     // 
     this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
     this.panel1.Location = new System.Drawing.Point(216, 8); 
     this.panel1.Name = "panel1"; 
     this.panel1.Size = new System.Drawing.Size(64, 56); 
     this.panel1.TabIndex = 0; 
     // 
     // timer1 
     // 
     this.timer1.Enabled = true; 
     this.timer1.SynchronizingObject = this; 
     this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed); 
     // 
     // Form1 
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
     this.BackColor = System.Drawing.Color.White; 
     this.ClientSize = new System.Drawing.Size(292, 273); 
     this.Controls.Add(this.panel1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); 
     ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit(); 
     this.ResumeLayout(false); 

    } 
    #endregion 

    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.Run(new Form1()); 
    } 

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
    { 
     Random r=new Random(1); 

     for(int x=0;x<100;x++) 
     { 
     SolidBrush b=new SolidBrush(Color.FromArgb(r.Next(255),r.Next(255),r.Next(255))); 
     e.Graphics.FillRectangle(b,r.Next(this.ClientSize.Width),r.Next(this.ClientSize.Height),r.Next(100),r.Next(100)); 
     } 
    } 

    private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     Point p=Control.MousePosition; 
     IntPtr dc=GetDC(IntPtr.Zero); 
     this.panel1.BackColor=ColorTranslator.FromWin32(GetPixel(dc,p.X,p.Y)); 
     ReleaseDC(dc); 
    } 
    } 
} 

如果你想在PictureBox或在您自己的形式來樣的顏色,那麼你只需要獲得DC該對象。這可以使用CreateGraphics,Graphics.GetHdc和Graphics.ReleaseHdc來完成。下面的清單顯示了一個MouseMove處理程序,可用於從窗體中獲取像素顏色。

protected override void OnMouseMove(MouseEventArgs e) 
{ 
    Graphics g=this.CreateGraphics(); 
    IntPtr myDC=g.GetHdc(); 
    Color c=ColorTranslator.FromWin32(GetPixel(myDC,e.X,e.Y)); 
    g.ReleaseHdc(myDC); 

    this.panel1.BackColor=c; 
}