I'm writing a code for a certain WinForms app that will both work on Windows and ArchLinux. Everything on the form works on both platforms, except for a button that takes the screenshot of the form. It works on Windows, but on ArchLinux the resulting image is a plain gray rectangle. I'm suspecting the culprit could be pixel formats. So, can I change the pixel format before the bitmap is passed onto memory stream and saved to the disk? As one can imagine, changing the pixel format after the image is saved doesn't work if the source image is already faulty.
private void button13_Click(object sender, EventArgs e)
{
string currentDir = System.IO.Directory.GetCurrentDirectory();
var form1 = Form.ActiveForm;
using var bmp = new Bitmap(form1.Width, form1.Height);
form1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
string outputFileName = currentDir + @"\arc.png";
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
bmp.Save(memory, ImageFormat.Png);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
Bitmap original = new Bitmap(outputFileName);
Bitmap clone = new Bitmap(original.Width, original.Height,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
using (Graphics gr = Graphics.FromImage(clone))
{
gr.DrawImage(original, new Rectangle(0, 0, clone.Width, clone.Height));
}
}
Until now, tried different image formats, including png, jpeg, tiff, gif, bmp and others. Each one results in the same gray image on ArchLinux with different levels of contrast/saturation.