As the title says, calling Bitmap.GetPixel results in nothing. No value being assigned to the variable, no exceptions being thrown.
Here’s my test code:
private void frmStatTracker_Load(object sender, EventArgs e)
{
ScreenCapture sc = new ScreenCapture();
Color charlie = new Color();
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle.Contains("DM -"))
{
sc.CaptureWindowToFile(p.MainWindowHandle, "C:\\test.png", System.Drawing.Imaging.ImageFormat.Png);
Image i = sc.CaptureWindow(p.MainWindowHandle);
Bitmap b = new Bitmap(i);
pictureBox1.Image = b;
charlie = b.GetPixel(65, 41);
return;
}
}
}
When I breakpoint the code, I get the following:
i has a valid image in it from the screenCapture.
b has a valid image in it.
charlie is left with no value. When I add it to the watch list, it tells me that charlie does not exist in the current context, and the variable does not exist in the Locals window. If I breakpoint either the declaration, or the line where it assigns a value, the breakpoint will be hit and appear to be run.
I’m at my wits end, and have no idea how to even start troubleshooting something like this. Help?
Thanks 🙂
You are debugging a release (or other optimized) build. That’s why
charliedoesn’t exist. The variable has only been assigned, not otherwise used. The compiler has optimized it away.EDIT: You can prevent this by using a Debug build, or, as David Heffernan said, by doing something meaningful with
charlie.