Monday, August 22, 2011

A more durable screen capture class!

Here's a more durable (not exactly performance-tuned) screen capture class that I think someone will be pleased to find someday, it also does quality reduction (and thus file-size) for the Jpeg file format.  Pretty neat.

GetScreen.cs
[code]
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.IO;

namespace ScreenCapture
{
    
    public class GetScreen
    {
        private Graphics gfx;
        private Bitmap bmp;
        private Point zero;
        private Size resolution;
        private Size imgSz;

        public GetScreen()
        {
            zero = new Point(0, 0);
            resolution = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                Screen.PrimaryScreen.Bounds.Height,
                PixelFormat.Format32bppArgb);
            gfx = Graphics.FromImage(bmp);
            imgSz = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        }
        ~GetScreen()
        {
            bmp.Dispose();
            gfx.Dispose();
        }
        public Bitmap GetWholeScreen()
        {
            gfx.CopyFromScreen(zero, zero, resolution);
            return new Bitmap(bmp, imgSz);
        }
        public Bitmap GetImageCopy(Point topLeft, Size widthHeight)
        {
            gfx.CopyFromScreen(zero, zero, resolution);
            return bmp.Clone(new Rectangle(topLeft, widthHeight), PixelFormat.DontCare);
        }
        public void SaveBitmapAsJpeg(Bitmap someBitmap, long qualityPercentage, string fileName)
        {
            if (someBitmap == null || fileName == null)
            {
                throw new ArgumentNullException();
            }
            if (qualityPercentage > 100 || qualityPercentage < 0)
            {
                throw new ArgumentException("Argument qualityPercentage was out of bounds.  0-100 is supported.");
            }
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType == "image/jpeg")
                {
                    ici = codec;
                    break;
                }
            }
            EncoderParameters ep = new EncoderParameters();
            //0 = lowest quality, 100 = highest.
            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)qualityPercentage);
            someBitmap.Save(fileName, ici, ep);
        }
        public void SaveBitmapAsJpeg(Bitmap someBitmap, long qualityPercentage, Stream st)
        {
            if (someBitmap == null || st == null)
            {
                throw new ArgumentNullException();
            }
            if (qualityPercentage > 100 || qualityPercentage < 0)
            {
                throw new ArgumentException("Argument qualityPercentage was out of bounds.  0-100 is supported.");
            }
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType == "image/jpeg")
                {
                    ici = codec;
                    break;
                }
            }
            EncoderParameters ep = new EncoderParameters();
            //0 = lowest quality, 100 = highest.
            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)qualityPercentage);
            someBitmap.Save(st, ici, ep);
        }

    }
}
[/code]


I also had time to make a demo using the class, it wasn't specifically for the blog but it may be interesting for a spare minute or so.

http://www.mediafire.com/?v0lhcfznb1oz0wt