There are numerous tutorial on the web on how to capture and save a screenshot using C#. For example, I used this website to obtain my solution:
using (var screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb))
using (var g = Graphics.FromImage(screenshot))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);
screenshot.Save("screenshot.png", ImageFormat.Png);
}
It works fine in most programs, but the program I want to capture uses an 8 bits indexed color table. Screenshots of that program taken with this code are strange. My question is if someone can point me in the right direction for capturing screenshots of programs with indexed color tables in C#?
To help you help me, I will describe my findings and attempted solutions below.
The screenshots captured with this code of a full screen program using an 8 bits indexed color table is mostly black(for ~88 %) and there are only 17 other colors in it. I don't see a pattern in those 17 colors. In the program itself almost all 256 colors are used. I was hoping to find 256 colors in the black screenshots as well, which could indicate a simple one-on-one relationship, but that is not the case.
I would also like to note that screenshots taken manually are perfect(when I paste them in MS Paint for example). For that reason I tried fetching the image from System.Windows.Forms.Clipboard.GetImage() after taking a screenshot manually, but that returns a COMobject and I wouldn't know what to do with that. Surely that must contain the information of a valid screenshot, since MS Paint knows how to extract it from that COM object. How can I extract that myself, preferably in C#?
But my main question: Can someone point me in the right direction for capturing screenshots of programs with indexed color tables with C#?
The suggested solutions did not work and I only recently solved this problem. I took a screenshot by sending a PrintScreen key press and release to windows, and acquired the data from the clipboard in the form of a
MemoryStream
and figured out how the screenshot was decoded in that stream and converted that to a bitmap. Not very appealing, but at least it works...