C# 画点

    技术2022-05-20  30

    画点的方法:方法一:     用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

          希望对大家有所帮助!


    最新回复(0)