日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

如何在C#控件中画点并获得指定点的像素颜色

發(fā)布時間:2023/12/18 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在C#控件中画点并获得指定点的像素颜色 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

畫點的方法:
方法一:
???? 用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構(gòu)成的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

總結(jié)

以上是生活随笔為你收集整理的如何在C#控件中画点并获得指定点的像素颜色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。