1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
| public class FastGetPixel
{
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
private static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
UInt32 dwRop // raster operation code
);
[DllImport("gdi32.dll")]
public static extern uint GetPixel(IntPtr hDC, int x, int y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
private const Int32 SRCCOPY = 13369376;
private IntPtr hWnd;
private Bitmap innerbitmap;
private bool screenmade = false;
private BitmapData bmpData;
private int byteLen = 4;
private byte[] bitmapBuffer;
private Color col = Color.Empty;
public FastGetPixel()
{
hWnd = GetDesktopWindow();
}
public FastGetPixel(IntPtr hWndsource)
{
hWnd = hWndsource;
}
public IntPtr Handle{
get{ return hWnd;}
set { try { innerbitmap.UnlockBits(bmpData); } catch { } try { innerbitmap.Dispose(); } catch { } bitmapBuffer = null; screenmade = false; hWnd = value; }
}
public Color GetLockedPixel(int Xpos, int Ypos)
{
try
{
int index = bmpData.Stride * Ypos + Xpos * byteLen;
col = Color.FromArgb(bitmapBuffer[index + 2], bitmapBuffer[index + 1], bitmapBuffer[index + 0]);
}
catch { col = Color.Empty; }
return col;
}
public bool LockWindowImage(PixelFormat PF)
{
try
{
if (!screenmade)
{
RECT rectal = new RECT();
GetWindowRect(new HandleRef(null, hWnd), out rectal);
innerbitmap = new Bitmap(rectal.Right, rectal.Bottom);
Graphics loGraphics = Graphics.FromImage(innerbitmap);
IntPtr lnDst = loGraphics.GetHdc();
IntPtr hDC = GetWindowDC(hWnd);
BitBlt(lnDst, 0, 0, rectal.Right - rectal.Left, rectal.Bottom - rectal.Top, hDC, 0, 0, SRCCOPY);
loGraphics.ReleaseHdc(lnDst);
loGraphics.Flush();
loGraphics.Dispose();
ReleaseDC(hWnd, hDC);
bmpData = innerbitmap.LockBits(new Rectangle(0, 0, innerbitmap.Width, innerbitmap.Height), ImageLockMode.ReadOnly, PF);
bitmapBuffer = new byte[bmpData.Stride * bmpData.Height];
Marshal.Copy(bmpData.Scan0, bitmapBuffer, 0, bitmapBuffer.Length);
byteLen = bmpData.Stride / bmpData.Width;
screenmade = true;
}
else { return false; }
}
catch { return false; }
return true;
}
public bool LockWindowImage()
{
try
{
if (!screenmade)
{
RECT rectal = new RECT();
GetWindowRect(new HandleRef(null, hWnd), out rectal);
innerbitmap = new Bitmap(rectal.Right, rectal.Bottom);
Graphics loGraphics = Graphics.FromImage(innerbitmap);
IntPtr lnDst = loGraphics.GetHdc();
IntPtr hDC = GetWindowDC(hWnd);
BitBlt(lnDst, 0, 0, rectal.Right - rectal.Left, rectal.Bottom - rectal.Top, hDC, 0, 0, SRCCOPY);
loGraphics.ReleaseHdc(lnDst);
loGraphics.Flush();
loGraphics.Dispose();
ReleaseDC(hWnd, hDC);
bmpData = innerbitmap.LockBits(new Rectangle(0, 0, innerbitmap.Width, innerbitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
bitmapBuffer = new byte[bmpData.Stride * bmpData.Height];
Marshal.Copy(bmpData.Scan0, bitmapBuffer, 0, bitmapBuffer.Length);
byteLen = 4;
screenmade = true;
}
else { return false; }
}
catch { return false; }
return true;
}
public bool LockWindowImage(int Left, int Top, int Width, int Height, PixelFormat PF)
{
try
{
if (!screenmade)
{
innerbitmap = new Bitmap(Width, Height);
Graphics loGraphics = Graphics.FromImage(innerbitmap);
IntPtr lnDst = loGraphics.GetHdc();
IntPtr hDC = GetWindowDC(hWnd);
BitBlt(lnDst, 0, 0, Width, Height, hDC, Left, Top, SRCCOPY);
loGraphics.ReleaseHdc(lnDst);
loGraphics.Flush();
loGraphics.Dispose();
ReleaseDC(hWnd, hDC);
bmpData = innerbitmap.LockBits(new Rectangle(0, 0, innerbitmap.Width, innerbitmap.Height), ImageLockMode.ReadOnly, PF);
bitmapBuffer = new byte[bmpData.Stride * bmpData.Height];
Marshal.Copy(bmpData.Scan0, bitmapBuffer, 0, bitmapBuffer.Length);
byteLen = bmpData.Stride / bmpData.Width;
screenmade = true;
}
else { return false; }
}
catch { return false; }
return true;
}
public void Clear() { try { innerbitmap.UnlockBits(bmpData); } catch { } try { innerbitmap.Dispose(); } catch { }bitmapBuffer = null; screenmade = false; }
}
} |