注册 登录
编程论坛 C# 论坛

GetPixel API 问题 C#编程

zhangdycool 发布于 2013-10-29 17:57, 773 次点击
我在窗体上创建了一个PictrueBox控件,并将PictrueBox的背景颜色设为红色。想在鼠标按下时,获取当前坐标的颜色,查了很多资料也不知道怎么解决,求解决,本人初次使用C#调用API。在此谢过。
以下是我的声明及使用
using System.Runtime.InteropServices;
[DllImport("gdi32.dll")]
public static extern System.UInt32 GetPixel(IntPtr hdc, int xPos, int yPos);
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Graphics gra = Graphics.FromHwnd(((PictureBox)sender).Handle);
            IntPtr hdc = gra.GetHdc();
            label5.BackColor = Color.FromArgb(Convert.ToInt32(GetPixel(hdc, e.X, e.Y)));
        }

5 回复
#2
afdoa832013-10-29 18:27
该像素点必须在当前剪辑区的边界之内。并不是所有设备都支持GetPixel函数。应用程序应调用GetDeviceCaps函数来确定指定的设备是否支持该函数。
你需要用函数指定与设备无关,使用GetPixel获取RGB值只是读内存。(不一定非得用GetDeviceCaps)
#3
zhangdycool2013-10-29 18:38
能不能提供一个具体一点的思路,
#4
zhangdycool2013-10-29 18:40
我把Graphics gra = Graphics.FromHwnd(((PictureBox)sender).Handle);//((PictureBox)sender).Handle为PictureBox的句柄
改成Graphics gra = Graphics.FromHwnd(PictureBox.Handle);效果是有了,但不正确。
#5
zhangdycool2013-10-29 19:33
我又仔细的看了一遍MSDN发现有这两句话:The pixel must be within the boundaries of the current clipping region.
Not all devices support GetPixel.
就是当前点必须在剪切区内这个剪切怎么确定呢,还有并不是所有设备都支持GetPixel
#6
zhangdycool2013-10-29 20:22
已经解决了。
1