Beispiel #1
0
        public static void MakeScreenshot(this amulware.Graphics.Program program)
        {
            using (var bitmap = program.GrabScreenshot())
            {
                if (!Directory.Exists(Settings.ScreenShot.Path))
                {
                    Directory.CreateDirectory(Settings.ScreenShot.Path);
                }

                string filename = Settings.ScreenShot.Path + "rf_" + DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss.ff");

                if (Settings.ScreenShot.SaveAsPng)
                {
                    bitmap.Save(filename + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                {
                    var encoder = ImageCodecInfo.GetImageDecoders()
                                  .FirstOrDefault(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
                    var parameters = new EncoderParameters(1);
                    parameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);

                    bitmap.Save(filename + ".jpg", encoder, parameters);
                }

                Console.WriteLine("Screenshot saved to {0}", filename);
            }
        }
Beispiel #2
0
        // Returns a System.Drawing.Bitmap with the contents of the current framebuffer
        public static Bitmap GrabScreenshot(this amulware.Graphics.Program program)
        {
            int width  = program.ClientSize.Width;
            int height = program.ClientSize.Height;

            float ratio = (float)width / height;

            int x = 0, y = 0, w = width, h = height;

            if (ratio >= 16.0 / 9.0)
            {
                w = (int)(height * 16.0 / 9.0);
                x = (width - w) / 2;
            }
            else
            {
                h = (int)(width * 9.0 / 16.0);
                y = (height - h) / 2;
            }

            var bmp = new Bitmap(w, h);

            System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h),
                                                                  System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                                                  System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            GL.ReadPixels(x, y, w, h, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
            bmp.UnlockBits(data);

            bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return(bmp);
        }