I do have a working method however it is fairly resource intensive and I am looking for a better way. Running the code below was suggested on a response to a similar question here.
However running said code yields a System.OutOfMemoryException
.
The exception happens @ using (Graphics gdest = Graphics.FromImage(screenPixel)) {
Current code:
Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format16bppArgb1555);
using (Graphics gdest = Graphics.FromImage(screenPixel)) {
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero)) {
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
}
return screenPixel.GetPixel(0, 0);
EDIT:
Okay so I've fixed the issue I was having above with the following code. However now I am only getting the color black (0,0,0) returned.
this.screenPixel = new Bitmap(1, 1, PixelFormat.Format16bppRgb555);
using (Graphics gdest = Graphics.FromImage(this.screenPixel)) {
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero)) {
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, width, height, hSrcDC, x, y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
}
Color result = screenPixel.GetPixel(0, 0);
Console.WriteLine(result.R + " " + result.G + " " + result.B);
GC.SuppressFinalize(this.screenPixel);
this.screenPixel.Dispose();
return result;