Esempio n. 1
0
        private static void TakeScreenshot(Form form,
                                           string destinationFile,
                                           string destinationFileInverted)
        {
            if (File.Exists(destinationFile))
            {
                File.Delete(destinationFile);
            }

            if (File.Exists(destinationFileInverted))
            {
                File.Delete(destinationFileInverted);
            }

            using (var destinationBitmap = new Bitmap(form.Width, form.Height))
            {
                form.DrawToBitmap(destinationBitmap, new Rectangle(0, 0, destinationBitmap.Width, destinationBitmap.Height));
                destinationBitmap.Save(destinationFile);

                using (var grayScaleImage = GrayScaleImageHelper.ToGrayScale(destinationBitmap))
                {
                    var        bounds      = new Rectangle(0, 0, grayScaleImage.Width, grayScaleImage.Height);
                    BitmapData bitmapData  = grayScaleImage.LockBits(bounds, ImageLockMode.ReadOnly, grayScaleImage.PixelFormat);
                    var        grayScaleHW = new byte[grayScaleImage.Height * bitmapData.Stride];
                    Marshal.Copy(bitmapData.Scan0, grayScaleHW, 0, grayScaleImage.Height * bitmapData.Stride);
                    var stride = bitmapData.Stride;
                    grayScaleImage.UnlockBits(bitmapData);

                    GrayScaleImageHelper.Invert(grayScaleHW, stride, grayScaleImage.Width, grayScaleImage.Height);
                    var data = GrayScaleImageHelper.FromData2(grayScaleImage.Width, grayScaleImage.Height, stride, grayScaleHW);
                    File.WriteAllBytes(destinationFileInverted, data);
                }
            }
        }