日期:2014-05-18  浏览次数:20752 次

刚学C#,想取出屏幕上(200,300)那个点的颜色信息,请帮忙好吗?越详细越好!我还不知怎样用API
想取出屏幕上(200,300)那个点的颜色信息,请帮忙好吗?越详细越好!我还不知怎样用API

------解决方案--------------------
c#类库
------解决方案--------------------
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bitmap img=new Bitmap(1027,768);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(1024, 768));
Color color = img.GetPixel(200, 300);
}
}
------解决方案--------------------
可以使用win32 API GetPixel直接取点的颜色信息
------解决方案--------------------
public class Win32APICall
{
[DllImport( "gdi32.dll ",EntryPoint= "DeleteDC ")]
public static extern IntPtr DeleteDC(IntPtr hdc);

[DllImport( "gdi32.dll ",EntryPoint= "DeleteObject ")]
public static extern IntPtr DeleteObject(IntPtr hObject);

[DllImport( "gdi32.dll ",EntryPoint= "BitBlt ")]
public static extern bool BitBlt(IntPtr hdcDest,int nXDest,
int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,
int nXSrc,int nYSrc,int dwRop);

[DllImport ( "gdi32.dll ",EntryPoint= "CreateCompatibleBitmap ")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
int nWidth, int nHeight);

[DllImport ( "gdi32.dll ",EntryPoint= "CreateCompatibleDC ")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport ( "gdi32.dll ",EntryPoint= "SelectObject ")]
public static extern IntPtr SelectObject(IntPtr hdc,IntPtr hgdiobjBmp);

[DllImport( "user32.dll ", EntryPoint= "GetDesktopWindow ")]
public static extern IntPtr GetDesktopWindow();

[DllImport( "user32.dll ",EntryPoint= "GetDC ")]
public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport( "user32.dll ",EntryPoint= "GetSystemMetrics ")]
public static extern int GetSystemMetrics(int nIndex);

[DllImport( "user32.dll ",EntryPoint= "ReleaseDC ")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);

public static Bitmap GetDesktop()
{
int screenX;
int screenY;
IntPtr hBmp;
IntPtr hdcScreen = GetDC(GetDesktopWindow());
IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);

screenX = GetSystemMetrics(0);
screenY = GetSystemMetrics(1);
hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);

if (hBmp!=IntPtr.Zero)
{
IntPtr hOldBmp = (IntPtr) SelectObject(hdcCompatible, hBmp);
BitBlt(hdcCompatible, 0, 0,screenX,screenY, hdcScreen, 0, 0,13369376);

SelectObject(hdcCompatible, hOldBmp);
DeleteDC(hdcCompatible);
ReleaseDC(GetDesktopWindow(), hdcScreen);

Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);

DeleteObject(hBmp);
GC.Collect();

return bmp;
}

return null;
}
}


得到 Screen 的 BitMap 后.
再用 getpixel 就可以得到点的颜色了....



------解决方案--------------------
不用像zhqs1000(子鱼) 兄那样 先拷贝位图块