by Luke
23. September 2010 22:55
This is very simple but I wanted to test the Syntax highlighter I just installed on the blog.
001using System;
002using System.IO;
003using System.IO.Compression;
004using System.Drawing;
005using System.Drawing.Imaging;
006using System.Collections.Generic;
007using System.Windows.Forms;
008
009 class Screenshot
010 {
011 List<Bitmap> _screenImages = new List<Bitmap>();
012 Graphics _gfx;
013
014 /// <summary>
015 /// Creates a new Screenshot object for taking, saving and compressing screenshots
016 /// </summary>
017 public Screenshot()
018 {
019 }
020
021 /// <summary>
022 /// Get a stream for the selected image
023 /// </summary>
024 /// <param name="index">zero-based index of the image</param>
025 /// <param name="compress">use compression or not</param>
026 /// <returns>Stream</returns>
027 public Stream this[int index, bool compress]
028 {
029 get
030 {
031 if (index >= 0 && index <= _screenImages.Count)
032 {
033 MemoryStream[] ms = new MemoryStream[] { new MemoryStream(), new MemoryStream() };
034 _screenImages[index].Save(ms[0], ImageFormat.Bmp);
035 if (compress)
036 {
037 compressOutput(ms[0], ms[1]);
038 ms[0].Close();
039 ms[0] = null;
040 ms[1].Seek(0, SeekOrigin.Begin);
041 return ms[1];
042 }
043 else
044 {
045 ms[0].Seek(0, SeekOrigin.Begin);
046 return ms[0];
047 }
048 }
049 return null;
050 }
051 }
052 /// <summary>
053 /// Get a stream for the selected image
054 /// </summary>
055 /// <param name="index">zero-based index of the image</param>
056 /// <param name="compress">use compression or not</param>
057 /// <param name="format">type of image to return</param>
058 /// <returns>Stream</returns>
059 public Stream this[int index, bool compress, ImageFormat format]
060 {
061 get
062 {
063 if (index >= 0 && _screenImages.Count < index)
064 {
065 MemoryStream[] ms = new MemoryStream[1];
066 _screenImages[index].Save(ms[0], format);
067 if (compress)
068 {
069 compressOutput(ms[0], ms[1]);
070 ms[0].Close();
071 ms[0] = null;
072 ms[1].Seek(0, SeekOrigin.Begin);
073 return ms[1];
074 }
075 else
076 {
077 ms[0].Seek(0, SeekOrigin.Begin);
078 return ms[0];
079 }
080 }
081 return null;
082 }
083 }
084
085 public int Count
086 {
087 get { return _screenImages.Count; }
088 }
089
090 /// <summary>
091 /// Capture all screens to memory
092 /// </summary>
093 public void CaptureAllScreens()
094 {
095 _screenImages.Clear();
096 foreach (Screen x in Screen.AllScreens)
097 {
098 Bitmap tmp = new Bitmap(x.Bounds.Width, x.Bounds.Height, PixelFormat.Format32bppArgb);
099 _gfx = Graphics.FromImage(tmp);
100 _gfx.CopyFromScreen(0, 0, 0, 0, x.Bounds.Size, CopyPixelOperation.SourceCopy);
101 _screenImages.Add(tmp);
102 }
103 }
104
105 /// <summary>
106 /// Capture only the primary screen
107 /// </summary>
108 public void CapturePrimary()
109 {
110 _screenImages.Clear();
111 Bitmap tmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
112 _gfx = Graphics.FromImage(tmp);
113 _gfx.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
114 _screenImages.Add(tmp);
115 }
116 /// <summary>
117 /// Capture a specific screen from the Screen collection
118 /// </summary>
119 /// <param name="screen">Screen object</param>
120 public void CapureScreen(Screen screen)
121 {
122 _screenImages.Clear();
123 Bitmap tmp = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb);
124 _gfx = Graphics.FromImage(tmp);
125 _gfx.CopyFromScreen(0, 0, 0, 0, screen.Bounds.Size, CopyPixelOperation.SourceCopy);
126 _screenImages.Add(tmp);
127 }
128
129 /// <summary>
130 /// Compress the input stream to the output stream using DeflateStream
131 /// </summary>
132 /// <param name="source">source stream</param>
133 /// <param name="destination">destination stream</param>
134 void compressOutput(Stream source, Stream destination)
135 {
136 DeflateStream ds = new DeflateStream(destination, CompressionMode.Compress);
137 byte[] buffer = new byte[2048];
138 int bytesRead = 0;
139 source.Seek(0, SeekOrigin.Begin);
140 bytesRead = source.Read(buffer, 0, buffer.Length);
141 while (bytesRead > 0)
142 {
143 ds.Write(buffer, 0, bytesRead);
144 bytesRead = source.Read(buffer, 0, buffer.Length);
145 }
146 ds.Flush();
147 }
148 }
149