如何在C#控件中画点并获得指定点的像素颜色
畫點的方法:
方法一:
???? 用picGraphics.FillRectangle(new SolidBrush(fillColor), p.X, p.Y, 1, 1); 即用一個像素填充方法.
方法二:
??? 用gdi32.dll庫中的SetPixel方法
?????[DllImport("gdi32.dll")]
?????private static extern int SetPixel(IntPtr hdc, int x1, int y1, int color);
獲得指定點的顏色:
?? 用gdi32.dll庫中的GetPixel方法
???[DllImport("gdi32.dll")]
???private static extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
?? 下面給出一些圖形操作的集合:
???
?????? #region 圖形操作函數(shù)集合
????????[DllImport("gdi32.dll")]
??????? private static extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
????????[DllImport("gdi32.dll")]
??????? private static extern int SetPixel(IntPtr hdc, int x1, int y1, int color);
???????????????
????????
??????? static public byte GetRValue(uint color)
??????? {
??????????? return (byte)color;
??????? }
????????????
??????? static public byte GetGValue(uint color)
??????? {
??????????? return ((byte)(((short)(color)) >> 8));
??????? }
??????? static public byte GetBValue(uint color)
??????? {
??????????? return ((byte)((color) >> 16));
??????? }
??????? static public byte GetAValue(uint color)
??????? {
??????????? return ((byte)((color) >> 24));
??????? }
??????? private? uint RGB(Color color)
??????? {
????????????// 返回由RGB構成的32位uint
??????????? uint R = color.R;
??????????? uint G = color.G;
??????????? uint B = color.B;
??????????? G <<= 8;
??????????? B <<= 16;
??????????? return ((uint)(R|G|B));
??????? }
????????
??????? public Color GetColor(Point p)
??????? {
????????????// 得到指定點的顏色RGB
????????????
??????????? uint colorref = GetPixel(this.picGraphics.GetHdc(), p.X, p.Y);??// picGraphics是我用picturePox類中的CreateGraphics方法創(chuàng)建的Graphics對象
??????????? this.picGraphics.ReleaseHdc();
??????????? byte Red = GetRValue(colorref);
??????????? byte Green = GetGValue(colorref);
??????????? byte Blue = GetBValue(colorref);
??????????? return Color.FromArgb(Red, Green, Blue);
??????? }
??????? public void SetColor(Point p, Color fillColor)
??????? {
??????????? SetPixel(this.picGraphics.GetHdc(), p.X, p.Y, (int)RGB(fillColor));
??????????? this.picGraphics.ReleaseHdc();
??????? }
???????? #endregion
總結
以上是生活随笔為你收集整理的如何在C#控件中画点并获得指定点的像素颜色的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统程序员成长计划-走近专业程序员
- 下一篇: C# Socket服务器及多客户端连接应