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

92 篇文章 6 订阅
52 篇文章 6 订阅

画点的方法:
方法一:
     用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 图形操作函数集合
        [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方法创建的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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值