A small class to take screen shots in C# NET

This is very simple but I wanted to test the Syntax highlighter I just installed on the blog.

using System;
using System.IO;
using System.IO.Compression;
using System.Drawing;
using System.Drawing.Imaging
using System.Collections.Generic;
using System.Windows.Forms;

public class Screenshot {
List _screenImages = new List();
Graphics _gfx; ///

/// Creates a new Screenshot object for taking, saving and compressing screenshots
public Screenshot() { }
/// Get a stream for the selected image zero-based index of the image use compression or not
public Stream this[int index, bool compress]
{
get {
if (index >= 0 && index <= _screenImages.Count)
{
MemoryStream[] ms = new MemoryStream[] { new MemoryStream(), new MemoryStream() };
_screenImages[index].Save(ms[0], ImageFormat.Bmp);
if (compress)
{
compressOutput(ms[0], ms[1]); ms[0].Close();
ms[0] = null;
ms[1].Seek(0, SeekOrigin.Begin); return ms[1];
}
else
{
ms[0].Seek(0, SeekOrigin.Begin);
return ms[0];
}
}
return null;
}
}
/// Get a stream for the selected image zero-based index of the image use compression or not type of image to return
public Stream this[int index, bool compress, ImageFormat format]
{
get
{
if (index >= 0 && _screenImages.Count < index)
{
MemoryStream[] ms = new MemoryStream[1];
_screenImages[index].Save(ms[0], format);
if (compress)
{
compressOutput(ms[0], ms[1]);
ms[0].Close();
ms[0] = null;
ms[1].Seek(0, SeekOrigin.Begin);
return ms[1];
}
else
{
ms[0].Seek(0, SeekOrigin.Begin);
return ms[0];
}
}
return null;
}
}
public int Count { get { return _screenImages.Count; } }

/// Capture all screens to memory
public void CaptureAllScreens()
{
_screenImages.Clear();
foreach (Screen x in Screen.AllScreens)
{
Bitmap tmp = new Bitmap(x.Bounds.Width, x.Bounds.Height, PixelFormat.Format32bppArgb);
_gfx = Graphics.FromImage(tmp);
_gfx.CopyFromScreen(0, 0, 0, 0, x.Bounds.Size, CopyPixelOperation.SourceCopy);
_screenImages.Add(tmp);
}
}
/// Capture only the primary screen
public void CapturePrimary()
{
_screenImages.Clear();
Bitmap tmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
_gfx = Graphics.FromImage(tmp);
_gfx.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
_screenImages.Add(tmp);
}
/// Capture a specific screen from the Screen collection Screen object
public void CapureScreen(Screen screen)
{
_screenImages.Clear();
Bitmap tmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb);
_gfx = Graphics.FromImage(tmp);
_gfx.CopyFromScreen(0, 0, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);
_screenImages.Add(tmp);
}

/// Compress the input stream to the output stream using DeflateStream
///source stream destination stream
void compressOutput(Stream source, Stream destination)
{
DeflateStream ds = new DeflateStream(destination, CompressionMode.Compress);
byte[] buffer = new byte[2048];
int bytesRead = 0;
source.Seek(0, SeekOrigin.Begin);
bytesRead = source.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
ds.Write(buffer, 0, bytesRead);
bytesRead = source.Read(buffer, 0, buffer.Length);
}
ds.Flush();
}
}

 

Leave a Reply

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑

%d bloggers like this: