How to take a screenshot with Mono C#? - c#

I'm trying to use use code get a screenshot in Mono C# but I'm getting a System.NotImplementedException when I call CopyFromScreen. My code works with .NET, so is there an alternate way of getting a screenshot using Mono?
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
bitmap.Save(memoryStream, imageFormat);
bitmap.Save(#"\tmp\screenshot.png", ImageFormat.Png);
I am using Mono JIT compiler version 2.4.2.3 (Debian 2.4.2.3+dfsg-2)
UPDATE: Mono cannot take screenshots. Sad. Lame.

I guess an alternative would be using gtk# to get a screenshot. You would need to create a mono project with GTK# support, after that following code should execute:
Gdk.Window window = Gdk.Global.DefaultRootWindow;
if (window!=null)
{
Gdk.Pixbuf pixBuf = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, false, 8,
window.Screen.Width, window.Screen.Height);
pixBuf.GetFromDrawable(window, Gdk.Colormap.System, 0, 0, 0, 0,
window.Screen.Width, window.Screen.Height);
pixBuf.ScaleSimple(400, 300, Gdk.InterpType.Bilinear);
pixBuf.Save("screenshot0.jpeg", "jpeg");
}
you could also use P\Invoke and call GTK functions directly.
hope this helps, regards

We had the same problem when we needed to take screenshots with Mono on Linux and OS X in the same software.
Actually, you can use CopyFromScreen on Mono on Linux. However you need to install mono-complete package, which includes System.Drawing.
For OS X the most reliable way is to Process.Start the screencapture command line tool. It is present there by default.
For Linux, to make it reliable, one can use either import command line from ImageMagic (you'll need to make it a dependency in this case), or force dependency on mono-complete package which includes System.Drawing. Gtk# approach is unreliable according to our tests and may provide blank screens or fail with exception (and we take thousands of screenshots on various machines every day).
Another thing is that when you need to capture multiple displays in one screenshot, you have to properly align separate screenshots after taking those separately via CopyFromScreen.
So we have this solution so far:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Pranas
{
/// <summary>
/// ScreenshotCapture
/// </summary>
public static class ScreenshotCapture
{
#region Public static methods
/// <summary>
/// Capture screenshot to Image object
/// </summary>
/// <param name="onlyPrimaryScreen">Create screen only from primary screen</param>
/// <returns></returns>
public static Image TakeScreenshot(bool onlyPrimaryScreen = false)
{
try
{
return WindowsCapture(onlyPrimaryScreen);
}
catch (Exception)
{
return OsXCapture(onlyPrimaryScreen);
}
}
#endregion
#region Private static methods
//private static Image ImageMagicCapture(bool onlyPrimaryScreen)
//{
// return ExecuteCaptureProcess("import", "-window root ");
//}
private static Image OsXCapture(bool onlyPrimaryScreen)
{
var data = ExecuteCaptureProcess(
"screencapture",
string.Format("{0} -T0 -tpng -S -x", onlyPrimaryScreen ? "-m" : ""));
return data;
}
/// <summary>
/// Start execute process with parameters
/// </summary>
/// <param name="execModule">Application name</param>
/// <param name="parameters">Command line parameters</param>
/// <returns>Bytes for destination image</returns>
private static Image ExecuteCaptureProcess(string execModule, string parameters)
{
var imageFileName = Path.Combine(Path.GetTempPath(), string.Format("screenshot_{0}.jpg", Guid.NewGuid()));
var process = Process.Start(execModule, string.Format("{0} {1}", parameters, imageFileName));
if (process == null)
{
throw new InvalidOperationException(string.Format("Executable of '{0}' was not found", execModule));
}
process.WaitForExit();
if (!File.Exists(imageFileName))
{
throw new InvalidOperationException(string.Format("Failed to capture screenshot using {0}", execModule));
}
try
{
return Image.FromFile(imageFileName);
}
finally
{
File.Delete(imageFileName);
}
}
/// <summary>
/// Capture screenshot with .NET standard implementation
/// </summary>
/// <param name="onlyPrimaryScreen"></param>
/// <returns>Return bytes of screenshot image</returns>
private static Image WindowsCapture(bool onlyPrimaryScreen)
{
if (onlyPrimaryScreen) return ScreenCapture(Screen.PrimaryScreen);
var bitmaps = (Screen.AllScreens.OrderBy(s => s.Bounds.Left).Select(ScreenCapture)).ToArray();
return CombineBitmap(bitmaps);
}
/// <summary>
/// Create screenshot of single display
/// </summary>
/// <param name="screen"></param>
/// <returns></returns>
private static Bitmap ScreenCapture(Screen screen)
{
var bitmap = new Bitmap(screen.Bounds.Width, screen.Bounds.Height, PixelFormat.Format32bppArgb);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(
screen.Bounds.X,
screen.Bounds.Y,
0,
0,
screen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
return bitmap;
}
/// <summary>
/// Combine images into one bitmap
/// </summary>
/// <param name="images"></param>
/// <returns>Combined image</returns>
private static Image CombineBitmap(ICollection<Image> images)
{
Image finalImage = null;
try
{
var width = 0;
var height = 0;
foreach (var image in images)
{
width += image.Width;
height = image.Height > height ? image.Height : height;
}
finalImage = new Bitmap(width, height);
using (var g = Graphics.FromImage(finalImage))
{
g.Clear(Color.Black);
var offset = 0;
foreach (var image in images)
{
g.DrawImage(image,
new Rectangle(offset, 0, image.Width, image.Height));
offset += image.Width;
}
}
}
catch (Exception ex)
{
if (finalImage != null)
finalImage.Dispose();
throw ex;
}
finally
{
//clean up memory
foreach (var image in images)
{
image.Dispose();
}
}
return finalImage;
}
#endregion
}
}
Or install it via NuGet (disclaimer: I'm the author):
PM> Install-Package Pranas.ScreenshotCapture
We use it heavily in our product on many setups, so we periodically improve the code and put notes into blog.

With Mono 2.4.4 you can get the whole screen without GTK#:
public static class MonoScreenShooter
{
public static void TakeScreenshot(string filePath)
{
using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
bmpScreenCapture.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}

Related

How can I convert the content of a WPF-window into an image? [duplicate]

I am making a screen capturing application and everything is going fine. All I need to do is capture the active window and take a screenshot of this active window. Does anyone know how I can do this?
Rectangle bounds = Screen.GetBounds(Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using(Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
bitmap.Save("test.jpg", ImageFormat.Jpeg);
}
for capturing current window use
Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.Left,bounds.Top), Point.Empty, bounds.Size);
}
bitmap.Save("C://test.jpg", ImageFormat.Jpeg);
}
ScreenCapture sc = new ScreenCapture();
// capture entire screen, and save it to a file
Image img = sc.CaptureScreen();
// display image in a Picture control named imageDisplay
this.imageDisplay.Image = img;
// capture this window, and save it
sc.CaptureWindowToFile(this.Handle,"C:\\temp2.gif",ImageFormat.Gif);
http://www.developerfusion.com/code/4630/capture-a-screen-shot/
I suggest next solution for capturing any current active window (not only our C# application) or entire screen with cursor position determination relative to left-top corner of window or screen respectively:
public enum enmScreenCaptureMode
{
Screen,
Window
}
class ScreenCapturer
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public Bitmap Capture(enmScreenCaptureMode screenCaptureMode = enmScreenCaptureMode.Window)
{
Rectangle bounds;
if (screenCaptureMode == enmScreenCaptureMode.Screen)
{
bounds = Screen.GetBounds(Point.Empty);
CursorPosition = Cursor.Position;
}
else
{
var foregroundWindowsHandle = GetForegroundWindow();
var rect = new Rect();
GetWindowRect(foregroundWindowsHandle, ref rect);
bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
}
var result = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(result))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return result;
}
public Point CursorPosition
{
get;
protected set;
}
}
Here is a snippet to capture either the desktop or the active window.
It has no reference to Windows Forms.
public class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
public static Image CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}
public static Bitmap CaptureActiveWindow()
{
return CaptureWindow(GetForegroundWindow());
}
public static Bitmap CaptureWindow(IntPtr handle)
{
var rect = new Rect();
GetWindowRect(handle, ref rect);
var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var graphics = Graphics.FromImage(result))
{
graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return result;
}
}
How to capture the whole screen:
var image = ScreenCapture.CaptureDesktop();
image.Save(#"C:\temp\snippetsource.jpg", ImageFormat.Jpeg);
How to capture the active window:
var image = ScreenCapture.CaptureActiveWindow();
image.Save(#"C:\temp\snippetsource.jpg", ImageFormat.Jpeg);
Originally found here: http://www.snippetsource.net/Snippet/158/capture-screenshot-in-c
KvanTTT's code worked great. I extended it a bit to allow a little more flexibility on save format, as well as the ability to save by hWnd, .NET Control/Form. You can get a bitmap or save to file, with a few options.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MosaiqPerformanceMonitor {
public enum CaptureMode {
Screen, Window
}
public static class ScreenCapturer {
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
/// <summary> Capture Active Window, Desktop, Window or Control by hWnd or .NET Contro/Form and save it to a specified file. </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="mode">Optional. The default value is CaptureMode.Window.</param>
/// <param name="format">Optional file save mode. Default is PNG</param>
public static void CaptureAndSave(string filename, CaptureMode mode = CaptureMode.Window, ImageFormat format = null) {
ImageSave(filename, format, Capture(mode));
}
/// <summary> Capture a specific window (or control) and save it to a specified file. </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="handle">hWnd (handle) of the window to capture</param>
/// <param name="format">Optional file save mode. Default is PNG</param>
public static void CaptureAndSave(string filename, IntPtr handle, ImageFormat format = null) {
ImageSave(filename, format, Capture(handle));
}
/// <summary> Capture a specific window (or control) and save it to a specified file. </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="c">Object to capture</param>
/// <param name="format">Optional file save mode. Default is PNG</param>
public static void CaptureAndSave(string filename, Control c, ImageFormat format = null) {
ImageSave(filename, format, Capture(c));
}
/// <summary> Capture the active window (default) or the desktop and return it as a bitmap </summary>
/// <param name="mode">Optional. The default value is CaptureMode.Window.</param>
public static Bitmap Capture(CaptureMode mode = CaptureMode.Window) {
return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
}
/// <summary> Capture a .NET Control, Form, UserControl, etc. </summary>
/// <param name="c">Object to capture</param>
/// <returns> Bitmap of control's area </returns>
public static Bitmap Capture(Control c) {
return Capture(c.Handle);
}
/// <summary> Capture a specific window and return it as a bitmap </summary>
/// <param name="handle">hWnd (handle) of the window to capture</param>
public static Bitmap Capture(IntPtr handle) {
Rectangle bounds;
var rect = new Rect();
GetWindowRect(handle, ref rect);
bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(result))
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
return result;
}
/// <summary> Position of the cursor relative to the start of the capture </summary>
public static Point CursorPosition;
/// <summary> Save an image to a specific file </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="format">Optional file save mode. Default is PNG</param>
/// <param name="image">Image to save. Usually a BitMap, but can be any
/// Image.</param>
static void ImageSave(string filename, ImageFormat format, Image image) {
format = format ?? ImageFormat.Png;
if (!filename.Contains("."))
filename = filename.Trim() + "." + format.ToString().ToLower();
if (!filename.Contains(#"\"))
filename = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? #"C:\Temp", filename);
filename = filename.Replace("%NOW%", DateTime.Now.ToString("yyyy-MM-dd#hh.mm.ss"));
image.Save(filename, format);
}
}
}
I assume you use Graphics.CopyFromScreen to get the screenshot.
You can use P/Invoke to GetForegroundWindow (and then get its position and size) to determine which region you need to copy from.
You can use the code from this question: How can I save a screenshot directly to a file in Windows?
Just change WIN32_API.GetDesktopWindow() to the Handle property of the window you want to capture.
If you want to use managed code: This will capture any window via the ProcessId.
I used the following to make the window active.
Microsoft.VisualBasic.Interaction.AppActivate(ProcessId);
Threading.Thread.Sleep(20);
I used the print screen to capture a window.
SendKeys.SendWait("%{PRTSC}");
Threading.Thread.Sleep(40);
IDataObject objData = Clipboard.GetDataObject();
Use the following code :
// Shot size = screen size
Size shotSize = Screen.PrimaryScreen.Bounds.Size;
// the upper left point in the screen to start shot
// 0,0 to get the shot from upper left point
Point upperScreenPoint = new Point(0, 0);
// the upper left point in the image to put the shot
Point upperDestinationPoint = new Point(0, 0);
// create image to get the shot in it
Bitmap shot = new Bitmap(shotSize.Width, shotSize.Height);
// new Graphics instance
Graphics graphics = Graphics.FromImage(shot);
// get the shot by Graphics class
graphics.CopyFromScreen(upperScreenPoint, upperDestinationPoint, shotSize);
// return the image
pictureBox1.Image = shot;
Works if the Desktop scaling is set.
public class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
public static Image CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}
public static Bitmap CaptureActiveWindow()
{
return CaptureWindow(GetForegroundWindow());
}
public static Bitmap CaptureWindow(IntPtr handle)
{
var rect = new Rect();
GetWindowRect(handle, ref rect);
GetScale getScale = new GetScale();
var bounds = new Rectangle(rect.Left, rect.Top, (int)((rect.Right - rect.Left)* getScale.getScalingFactor()), (int)((rect.Bottom - rect.Top )* getScale.getScalingFactor()));
var result = new Bitmap(bounds.Width, bounds.Height);
using (var graphics = Graphics.FromImage(result))
{
graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return result;
}
}
A little tweak to method static void ImageSave() will grant you the option where to save it. Credit goes to Microsoft (http://msdn.microsoft.com/en-us/library/sfezx97z.aspx)
static void ImageSave(string filename, ImageFormat format, Image image, SaveFileDialog saveFileDialog1)
{
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Enregistrer un image";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (saveFileDialog1.FilterIndex)
{
case 1:
image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}
fs.Close();
}
}
Your button_click event should be coded something like this...
private void btnScreenShot_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
ScreenCapturer.CaptureAndSave(filename, mode, format, saveFileDialog1);
}//
Based on ArsenMkrt's reply, but this one allows you to capture a control in your form (I'm writing a tool for example that has a WebBrowser control in it and want to capture just its display). Note the use of PointToScreen method:
//Project: WebCapture
//Filename: ScreenshotUtils.cs
//Author: George Birbilis (http://zoomicon.com)
//Version: 20130820
using System.Drawing;
using System.Windows.Forms;
namespace WebCapture
{
public static class ScreenshotUtils
{
public static Rectangle Offseted(this Rectangle r, Point p)
{
r.Offset(p);
return r;
}
public static Bitmap GetScreenshot(this Control c)
{
return GetScreenshot(new Rectangle(c.PointToScreen(Point.Empty), c.Size));
}
public static Bitmap GetScreenshot(Rectangle bounds)
{
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bitmap))
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
return bitmap;
}
public const string DEFAULT_IMAGESAVEFILEDIALOG_TITLE = "Save image";
public const string DEFAULT_IMAGESAVEFILEDIALOG_FILTER = "PNG Image (*.png)|*.png|JPEG Image (*.jpg)|*.jpg|Bitmap Image (*.bmp)|*.bmp|GIF Image (*.gif)|*.gif";
public const string CUSTOMPLACES_COMPUTER = "0AC0837C-BBF8-452A-850D-79D08E667CA7";
public const string CUSTOMPLACES_DESKTOP = "B4BFCC3A-DB2C-424C-B029-7FE99A87C641";
public const string CUSTOMPLACES_DOCUMENTS = "FDD39AD0-238F-46AF-ADB4-6C85480369C7";
public const string CUSTOMPLACES_PICTURES = "33E28130-4E1E-4676-835A-98395C3BC3BB";
public const string CUSTOMPLACES_PUBLICPICTURES = "B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5";
public const string CUSTOMPLACES_RECENT = "AE50C081-EBD2-438A-8655-8A092E34987A";
public static SaveFileDialog GetImageSaveFileDialog(
string title = DEFAULT_IMAGESAVEFILEDIALOG_TITLE,
string filter = DEFAULT_IMAGESAVEFILEDIALOG_FILTER)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = title;
dialog.Filter = filter;
/* //this seems to throw error on Windows Server 2008 R2, must be for Windows Vista only
dialog.CustomPlaces.Add(CUSTOMPLACES_COMPUTER);
dialog.CustomPlaces.Add(CUSTOMPLACES_DESKTOP);
dialog.CustomPlaces.Add(CUSTOMPLACES_DOCUMENTS);
dialog.CustomPlaces.Add(CUSTOMPLACES_PICTURES);
dialog.CustomPlaces.Add(CUSTOMPLACES_PUBLICPICTURES);
dialog.CustomPlaces.Add(CUSTOMPLACES_RECENT);
*/
return dialog;
}
public static void ShowSaveFileDialog(this Image image, IWin32Window owner = null)
{
using (SaveFileDialog dlg = GetImageSaveFileDialog())
if (dlg.ShowDialog(owner) == DialogResult.OK)
image.Save(dlg.FileName);
}
}
}
Having the Bitmap object you can just call Save on it
private void btnCapture_Click(object sender, EventArgs e)
{
webBrowser.GetScreenshot().Save("C://test.jpg", ImageFormat.Jpeg);
}
The above assumes the GC will grab the bitmap, but maybe it's better to assign the result of someControl.getScreenshot() to a Bitmap variable, then dispose that variable manually when finished with each image, especially if you're doing this grabbing often (say you have a list of webpages you want to load and save screenshots of them):
private void btnCapture_Click(object sender, EventArgs e)
{
Bitmap bitmap = webBrowser.GetScreenshot();
bitmap.ShowSaveFileDialog();
bitmap.Dispose(); //release bitmap resources
}
Even better, could employ a using clause, which has the added benefit of releasing the bitmap resources even in case of an exception occuring inside the using (child) block:
private void btnCapture_Click(object sender, EventArgs e)
{
using(Bitmap bitmap = webBrowser.GetScreenshot())
bitmap.ShowSaveFileDialog();
//exit from using block will release bitmap resources even if exception occured
}
Update:
Now WebCapture tool is ClickOnce-deployed (http://gallery.clipflair.net/WebCapture) from the web (also has nice autoupdate support thanks to ClickOnce) and you can find its source code at https://github.com/Zoomicon/ClipFlair/tree/master/Server/Tools/WebCapture
public static void ScreenCapFull_Clip(string strDestinationFolder, string strDestinationFile, bool bWriteToFile, bool bWriteToClipboard, out string strError)
{
strError = String.Empty;
if (strDestinationFile.Trim() == String.Empty) // Sort of hokey
bWriteToFile = false; // strDestinationFile = "c:\\temp\\clip.jpg";
int screenLeft = SystemInformation.VirtualScreen.Left;
int screenTop = SystemInformation.VirtualScreen.Top;
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;
// Create a bitmap of the appropriate size to receive the full-screen screenshot.
using (Bitmap bitmap = new Bitmap(screenWidth, screenHeight))
{
// Draw the screenshot into our bitmap.
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size);
}
//Save the screenshot as a Jpg image
var uniqueFileName = strDestinationFile; // "C:\\temp\\a.Jpg";
try
{
if(bWriteToFile == true)
bitmap.Save(uniqueFileName, ImageFormat.Jpeg);
}
catch (Exception ex)
{
strError = "Error: Could not write to file: " + uniqueFileName + "-" + ex.Message + "...";
}
if (bWriteToClipboard == false)
return;
try
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
PictureBox pb = new PictureBox();
pb.Image = bitmap; //???
//Put the image in a memorystream. VpaResult.VpaImage is a picturebox.
//VpaResult.VpaImage.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
pb.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
DataObject m_data = new DataObject();
m_data.SetData("PNG", true, ms);
Clipboard.SetDataObject(m_data, true);
}
catch (Exception ex)
{
strError += "Error: Could not write to Clipboard: " + ex.Message + "...";
strError = ex.Message;
}
}
} // End public static void ScreenCapFull_Clip

Is there a way to extract frames from a video file using ffmpeg to memory and make some manipulation on each frame?

The goal is to extract each time a frame from the video file then make histogram from the image and then to move to the next frame. this way all the frames.
The frames extraction and the histogram manipulation is working fine when the frames have saved as images on the hard disk. but now i want to do it all in memory.
to extract the frames i'm using ffmpeg because i think it's fast enough:
ffmpeg -r 1 -i MyVid.mp4 -r 1 "$filename%03d.png
for now i'm using the ffmpeg in command prompt window.
with this command it will save on the hard disk over 65000 images(frames).
but instead saving them on the hard disk i wonder if i can make the histogram manipulation on each frame in memory instead saving all the 65000 frames to the hard disk.
then i want to find specific images using the histogram and save to the hard disk this frames.
the histogram part for now is also using files from the hard disk and not from the memory:
private void btnLoadHistogram_Click(object sender, System.EventArgs e)
{
string[] files = Directory.GetFiles(#"d:\screenshots\", "*.jpg");
for (int i = 0; i < files.Length; i++)
{
sbInfo.Text = "Loading image";
if (pbImage.Image != null)
pbImage.Image.Dispose();
pbImage.Image = Image.FromFile(files[i]);//txtFileName.Text);
Application.DoEvents();
sbInfo.Text = "Computing histogram";
long[] myValues = GetHistogram(new Bitmap(pbImage.Image));
Histogram.DrawHistogram(myValues);
sbInfo.Text = "";
}
}
public long[] GetHistogram(System.Drawing.Bitmap picture)
{
long[] myHistogram = new long[256];
for (int i=0;i<picture.Size.Width;i++)
for (int j=0;j<picture.Size.Height;j++)
{
System.Drawing.Color c = picture.GetPixel(i,j);
long Temp=0;
Temp+=c.R;
Temp+=c.G;
Temp+=c.B;
Temp = (int) Temp/3;
myHistogram[Temp]++;
}
return myHistogram;
}
and the code of the class of the constrol HistogramaDesenat :
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Histograma
{
/// <summary>
/// Summary description for HistogramaDesenat.
/// </summary>
public class HistogramaDesenat : System.Windows.Forms.UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public HistogramaDesenat()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
this.Paint += new PaintEventHandler(HistogramaDesenat_Paint);
this.Resize+=new EventHandler(HistogramaDesenat_Resize);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// HistogramaDesenat
//
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "HistogramaDesenat";
this.Size = new System.Drawing.Size(208, 176);
}
#endregion
private void HistogramaDesenat_Paint(object sender, PaintEventArgs e)
{
if (myIsDrawing)
{
Graphics g = e.Graphics;
Pen myPen = new Pen(new SolidBrush(myColor),myXUnit);
//The width of the pen is given by the XUnit for the control.
for (int i=0;i<myValues.Length;i++)
{
//We draw each line
g.DrawLine(myPen,
new PointF(myOffset + (i*myXUnit), this.Height - myOffset),
new PointF(myOffset + (i*myXUnit), this.Height - myOffset - myValues[i] * myYUnit));
//We plot the coresponding index for the maximum value.
if (myValues[i]==myMaxValue)
{
SizeF mySize = g.MeasureString(i.ToString(),myFont);
g.DrawString(i.ToString(),myFont,new SolidBrush(myColor),
new PointF(myOffset + (i*myXUnit) - (mySize.Width/2), this.Height - myFont.Height ),
System.Drawing.StringFormat.GenericDefault);
}
}
//We draw the indexes for 0 and for the length of the array beeing plotted
g.DrawString("0",myFont, new SolidBrush(myColor),new PointF(myOffset,this.Height - myFont.Height),System.Drawing.StringFormat.GenericDefault);
g.DrawString((myValues.Length-1).ToString(),myFont,
new SolidBrush(myColor),
new PointF(myOffset + (myValues.Length * myXUnit) - g.MeasureString((myValues.Length-1).ToString(),myFont).Width,
this.Height - myFont.Height),
System.Drawing.StringFormat.GenericDefault);
//We draw a rectangle surrounding the control.
g.DrawRectangle(new System.Drawing.Pen(new SolidBrush(Color.Black),1),0,0,this.Width-1,this.Height-1);
}
}
long myMaxValue;
private long[] myValues;
private bool myIsDrawing;
private float myYUnit; //this gives the vertical unit used to scale our values
private float myXUnit; //this gives the horizontal unit used to scale our values
private int myOffset = 20; //the offset, in pixels, from the control margins.
private Color myColor = Color.Black;
private Font myFont = new Font("Tahoma",10);
[Category("Histogram Options")]
[Description ("The distance from the margins for the histogram")]
public int Offset
{
set
{
if (value>0)
myOffset= value;
}
get
{
return myOffset;
}
}
[Category("Histogram Options")]
[Description ("The color used within the control")]
public Color DisplayColor
{
set
{
myColor = value;
}
get
{
return myColor;
}
}
/// <summary>
/// We draw the histogram on the control
/// </summary>
/// <param name="myValues">The values beeing draw</param>
public void DrawHistogram(long[] Values)
{
myValues = new long[Values.Length];
Values.CopyTo(myValues,0);
myIsDrawing = true;
myMaxValue = getMaxim(myValues);
ComputeXYUnitValues();
this.Refresh();
}
/// <summary>
/// We get the highest value from the array
/// </summary>
/// <param name="Vals">The array of values in which we look</param>
/// <returns>The maximum value</returns>
private long getMaxim(long[] Vals)
{
if (myIsDrawing)
{
long max = 0;
for (int i=0;i<Vals.Length;i++)
{
if (Vals[i] > max)
max = Vals[i];
}
return max;
}
return 1;
}
private void HistogramaDesenat_Resize(object sender, EventArgs e)
{
if (myIsDrawing)
{
ComputeXYUnitValues();
}
this.Refresh();
}
private void ComputeXYUnitValues()
{
myYUnit = (float) (this.Height - (2 * myOffset)) / myMaxValue;
myXUnit = (float) (this.Width - (2 * myOffset)) / (myValues.Length-1);
}
}
}
so in the end this is what i want to do :
extract the frames from the video file in memory using the ffmpeg.
instead using Directory.GetFiles i want to make the histogram manipulation on each frame from the memory that is extracted by the ffmpeg.
each extracted frame image to use the histogram to find if there is a lightning(weather lightning) in the image.
if there is a lightning save the frame image to the hard disk.
For ffmpeg, try FFmpeg.AutoGen
But you need learn about ffmpeg api for demuxer and decoder to get raw frame.
For opencv, try emgucv (recomend)
You can try search example somewhere like this

Get the windows current display as images - C# [duplicate]

I am making a screen capturing application and everything is going fine. All I need to do is capture the active window and take a screenshot of this active window. Does anyone know how I can do this?
Rectangle bounds = Screen.GetBounds(Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using(Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
bitmap.Save("test.jpg", ImageFormat.Jpeg);
}
for capturing current window use
Rectangle bounds = this.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.Left,bounds.Top), Point.Empty, bounds.Size);
}
bitmap.Save("C://test.jpg", ImageFormat.Jpeg);
}
ScreenCapture sc = new ScreenCapture();
// capture entire screen, and save it to a file
Image img = sc.CaptureScreen();
// display image in a Picture control named imageDisplay
this.imageDisplay.Image = img;
// capture this window, and save it
sc.CaptureWindowToFile(this.Handle,"C:\\temp2.gif",ImageFormat.Gif);
http://www.developerfusion.com/code/4630/capture-a-screen-shot/
I suggest next solution for capturing any current active window (not only our C# application) or entire screen with cursor position determination relative to left-top corner of window or screen respectively:
public enum enmScreenCaptureMode
{
Screen,
Window
}
class ScreenCapturer
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public Bitmap Capture(enmScreenCaptureMode screenCaptureMode = enmScreenCaptureMode.Window)
{
Rectangle bounds;
if (screenCaptureMode == enmScreenCaptureMode.Screen)
{
bounds = Screen.GetBounds(Point.Empty);
CursorPosition = Cursor.Position;
}
else
{
var foregroundWindowsHandle = GetForegroundWindow();
var rect = new Rect();
GetWindowRect(foregroundWindowsHandle, ref rect);
bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
}
var result = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(result))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return result;
}
public Point CursorPosition
{
get;
protected set;
}
}
Here is a snippet to capture either the desktop or the active window.
It has no reference to Windows Forms.
public class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
public static Image CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}
public static Bitmap CaptureActiveWindow()
{
return CaptureWindow(GetForegroundWindow());
}
public static Bitmap CaptureWindow(IntPtr handle)
{
var rect = new Rect();
GetWindowRect(handle, ref rect);
var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var graphics = Graphics.FromImage(result))
{
graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return result;
}
}
How to capture the whole screen:
var image = ScreenCapture.CaptureDesktop();
image.Save(#"C:\temp\snippetsource.jpg", ImageFormat.Jpeg);
How to capture the active window:
var image = ScreenCapture.CaptureActiveWindow();
image.Save(#"C:\temp\snippetsource.jpg", ImageFormat.Jpeg);
Originally found here: http://www.snippetsource.net/Snippet/158/capture-screenshot-in-c
KvanTTT's code worked great. I extended it a bit to allow a little more flexibility on save format, as well as the ability to save by hWnd, .NET Control/Form. You can get a bitmap or save to file, with a few options.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MosaiqPerformanceMonitor {
public enum CaptureMode {
Screen, Window
}
public static class ScreenCapturer {
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
/// <summary> Capture Active Window, Desktop, Window or Control by hWnd or .NET Contro/Form and save it to a specified file. </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="mode">Optional. The default value is CaptureMode.Window.</param>
/// <param name="format">Optional file save mode. Default is PNG</param>
public static void CaptureAndSave(string filename, CaptureMode mode = CaptureMode.Window, ImageFormat format = null) {
ImageSave(filename, format, Capture(mode));
}
/// <summary> Capture a specific window (or control) and save it to a specified file. </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="handle">hWnd (handle) of the window to capture</param>
/// <param name="format">Optional file save mode. Default is PNG</param>
public static void CaptureAndSave(string filename, IntPtr handle, ImageFormat format = null) {
ImageSave(filename, format, Capture(handle));
}
/// <summary> Capture a specific window (or control) and save it to a specified file. </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="c">Object to capture</param>
/// <param name="format">Optional file save mode. Default is PNG</param>
public static void CaptureAndSave(string filename, Control c, ImageFormat format = null) {
ImageSave(filename, format, Capture(c));
}
/// <summary> Capture the active window (default) or the desktop and return it as a bitmap </summary>
/// <param name="mode">Optional. The default value is CaptureMode.Window.</param>
public static Bitmap Capture(CaptureMode mode = CaptureMode.Window) {
return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
}
/// <summary> Capture a .NET Control, Form, UserControl, etc. </summary>
/// <param name="c">Object to capture</param>
/// <returns> Bitmap of control's area </returns>
public static Bitmap Capture(Control c) {
return Capture(c.Handle);
}
/// <summary> Capture a specific window and return it as a bitmap </summary>
/// <param name="handle">hWnd (handle) of the window to capture</param>
public static Bitmap Capture(IntPtr handle) {
Rectangle bounds;
var rect = new Rect();
GetWindowRect(handle, ref rect);
bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(result))
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
return result;
}
/// <summary> Position of the cursor relative to the start of the capture </summary>
public static Point CursorPosition;
/// <summary> Save an image to a specific file </summary>
/// <param name="filename">Filename.
/// <para>* If extension is omitted, it's calculated from the type of file</para>
/// <para>* If path is omitted, defaults to %TEMP%</para>
/// <para>* Use %NOW% to put a timestamp in the filename</para></param>
/// <param name="format">Optional file save mode. Default is PNG</param>
/// <param name="image">Image to save. Usually a BitMap, but can be any
/// Image.</param>
static void ImageSave(string filename, ImageFormat format, Image image) {
format = format ?? ImageFormat.Png;
if (!filename.Contains("."))
filename = filename.Trim() + "." + format.ToString().ToLower();
if (!filename.Contains(#"\"))
filename = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? #"C:\Temp", filename);
filename = filename.Replace("%NOW%", DateTime.Now.ToString("yyyy-MM-dd#hh.mm.ss"));
image.Save(filename, format);
}
}
}
I assume you use Graphics.CopyFromScreen to get the screenshot.
You can use P/Invoke to GetForegroundWindow (and then get its position and size) to determine which region you need to copy from.
You can use the code from this question: How can I save a screenshot directly to a file in Windows?
Just change WIN32_API.GetDesktopWindow() to the Handle property of the window you want to capture.
If you want to use managed code: This will capture any window via the ProcessId.
I used the following to make the window active.
Microsoft.VisualBasic.Interaction.AppActivate(ProcessId);
Threading.Thread.Sleep(20);
I used the print screen to capture a window.
SendKeys.SendWait("%{PRTSC}");
Threading.Thread.Sleep(40);
IDataObject objData = Clipboard.GetDataObject();
Use the following code :
// Shot size = screen size
Size shotSize = Screen.PrimaryScreen.Bounds.Size;
// the upper left point in the screen to start shot
// 0,0 to get the shot from upper left point
Point upperScreenPoint = new Point(0, 0);
// the upper left point in the image to put the shot
Point upperDestinationPoint = new Point(0, 0);
// create image to get the shot in it
Bitmap shot = new Bitmap(shotSize.Width, shotSize.Height);
// new Graphics instance
Graphics graphics = Graphics.FromImage(shot);
// get the shot by Graphics class
graphics.CopyFromScreen(upperScreenPoint, upperDestinationPoint, shotSize);
// return the image
pictureBox1.Image = shot;
Works if the Desktop scaling is set.
public class ScreenCapture
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
public static Image CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}
public static Bitmap CaptureActiveWindow()
{
return CaptureWindow(GetForegroundWindow());
}
public static Bitmap CaptureWindow(IntPtr handle)
{
var rect = new Rect();
GetWindowRect(handle, ref rect);
GetScale getScale = new GetScale();
var bounds = new Rectangle(rect.Left, rect.Top, (int)((rect.Right - rect.Left)* getScale.getScalingFactor()), (int)((rect.Bottom - rect.Top )* getScale.getScalingFactor()));
var result = new Bitmap(bounds.Width, bounds.Height);
using (var graphics = Graphics.FromImage(result))
{
graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return result;
}
}
A little tweak to method static void ImageSave() will grant you the option where to save it. Credit goes to Microsoft (http://msdn.microsoft.com/en-us/library/sfezx97z.aspx)
static void ImageSave(string filename, ImageFormat format, Image image, SaveFileDialog saveFileDialog1)
{
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Enregistrer un image";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (saveFileDialog1.FilterIndex)
{
case 1:
image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}
fs.Close();
}
}
Your button_click event should be coded something like this...
private void btnScreenShot_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
ScreenCapturer.CaptureAndSave(filename, mode, format, saveFileDialog1);
}//
Based on ArsenMkrt's reply, but this one allows you to capture a control in your form (I'm writing a tool for example that has a WebBrowser control in it and want to capture just its display). Note the use of PointToScreen method:
//Project: WebCapture
//Filename: ScreenshotUtils.cs
//Author: George Birbilis (http://zoomicon.com)
//Version: 20130820
using System.Drawing;
using System.Windows.Forms;
namespace WebCapture
{
public static class ScreenshotUtils
{
public static Rectangle Offseted(this Rectangle r, Point p)
{
r.Offset(p);
return r;
}
public static Bitmap GetScreenshot(this Control c)
{
return GetScreenshot(new Rectangle(c.PointToScreen(Point.Empty), c.Size));
}
public static Bitmap GetScreenshot(Rectangle bounds)
{
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bitmap))
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
return bitmap;
}
public const string DEFAULT_IMAGESAVEFILEDIALOG_TITLE = "Save image";
public const string DEFAULT_IMAGESAVEFILEDIALOG_FILTER = "PNG Image (*.png)|*.png|JPEG Image (*.jpg)|*.jpg|Bitmap Image (*.bmp)|*.bmp|GIF Image (*.gif)|*.gif";
public const string CUSTOMPLACES_COMPUTER = "0AC0837C-BBF8-452A-850D-79D08E667CA7";
public const string CUSTOMPLACES_DESKTOP = "B4BFCC3A-DB2C-424C-B029-7FE99A87C641";
public const string CUSTOMPLACES_DOCUMENTS = "FDD39AD0-238F-46AF-ADB4-6C85480369C7";
public const string CUSTOMPLACES_PICTURES = "33E28130-4E1E-4676-835A-98395C3BC3BB";
public const string CUSTOMPLACES_PUBLICPICTURES = "B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5";
public const string CUSTOMPLACES_RECENT = "AE50C081-EBD2-438A-8655-8A092E34987A";
public static SaveFileDialog GetImageSaveFileDialog(
string title = DEFAULT_IMAGESAVEFILEDIALOG_TITLE,
string filter = DEFAULT_IMAGESAVEFILEDIALOG_FILTER)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = title;
dialog.Filter = filter;
/* //this seems to throw error on Windows Server 2008 R2, must be for Windows Vista only
dialog.CustomPlaces.Add(CUSTOMPLACES_COMPUTER);
dialog.CustomPlaces.Add(CUSTOMPLACES_DESKTOP);
dialog.CustomPlaces.Add(CUSTOMPLACES_DOCUMENTS);
dialog.CustomPlaces.Add(CUSTOMPLACES_PICTURES);
dialog.CustomPlaces.Add(CUSTOMPLACES_PUBLICPICTURES);
dialog.CustomPlaces.Add(CUSTOMPLACES_RECENT);
*/
return dialog;
}
public static void ShowSaveFileDialog(this Image image, IWin32Window owner = null)
{
using (SaveFileDialog dlg = GetImageSaveFileDialog())
if (dlg.ShowDialog(owner) == DialogResult.OK)
image.Save(dlg.FileName);
}
}
}
Having the Bitmap object you can just call Save on it
private void btnCapture_Click(object sender, EventArgs e)
{
webBrowser.GetScreenshot().Save("C://test.jpg", ImageFormat.Jpeg);
}
The above assumes the GC will grab the bitmap, but maybe it's better to assign the result of someControl.getScreenshot() to a Bitmap variable, then dispose that variable manually when finished with each image, especially if you're doing this grabbing often (say you have a list of webpages you want to load and save screenshots of them):
private void btnCapture_Click(object sender, EventArgs e)
{
Bitmap bitmap = webBrowser.GetScreenshot();
bitmap.ShowSaveFileDialog();
bitmap.Dispose(); //release bitmap resources
}
Even better, could employ a using clause, which has the added benefit of releasing the bitmap resources even in case of an exception occuring inside the using (child) block:
private void btnCapture_Click(object sender, EventArgs e)
{
using(Bitmap bitmap = webBrowser.GetScreenshot())
bitmap.ShowSaveFileDialog();
//exit from using block will release bitmap resources even if exception occured
}
Update:
Now WebCapture tool is ClickOnce-deployed (http://gallery.clipflair.net/WebCapture) from the web (also has nice autoupdate support thanks to ClickOnce) and you can find its source code at https://github.com/Zoomicon/ClipFlair/tree/master/Server/Tools/WebCapture
public static void ScreenCapFull_Clip(string strDestinationFolder, string strDestinationFile, bool bWriteToFile, bool bWriteToClipboard, out string strError)
{
strError = String.Empty;
if (strDestinationFile.Trim() == String.Empty) // Sort of hokey
bWriteToFile = false; // strDestinationFile = "c:\\temp\\clip.jpg";
int screenLeft = SystemInformation.VirtualScreen.Left;
int screenTop = SystemInformation.VirtualScreen.Top;
int screenWidth = SystemInformation.VirtualScreen.Width;
int screenHeight = SystemInformation.VirtualScreen.Height;
// Create a bitmap of the appropriate size to receive the full-screen screenshot.
using (Bitmap bitmap = new Bitmap(screenWidth, screenHeight))
{
// Draw the screenshot into our bitmap.
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(screenLeft, screenTop, 0, 0, bitmap.Size);
}
//Save the screenshot as a Jpg image
var uniqueFileName = strDestinationFile; // "C:\\temp\\a.Jpg";
try
{
if(bWriteToFile == true)
bitmap.Save(uniqueFileName, ImageFormat.Jpeg);
}
catch (Exception ex)
{
strError = "Error: Could not write to file: " + uniqueFileName + "-" + ex.Message + "...";
}
if (bWriteToClipboard == false)
return;
try
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
PictureBox pb = new PictureBox();
pb.Image = bitmap; //???
//Put the image in a memorystream. VpaResult.VpaImage is a picturebox.
//VpaResult.VpaImage.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
pb.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
DataObject m_data = new DataObject();
m_data.SetData("PNG", true, ms);
Clipboard.SetDataObject(m_data, true);
}
catch (Exception ex)
{
strError += "Error: Could not write to Clipboard: " + ex.Message + "...";
strError = ex.Message;
}
}
} // End public static void ScreenCapFull_Clip

C# How to Make a BalloonToolTip from a Non-Form Application

Okay, So I'm attempting to make a Simple Screenshot program and I need the program to show a BalloonToolTip when a screenshot is taken and when the program is set to run on start up. The code below show my entire program, there is no form, nor is there a designer. Just a program that runs from Program.cs, and its not a console application.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using FullscreenShot.Properties;
using GlobalHotKey;
using System.Windows.Input;
using System.Timers;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace FullscreenShot
{
class Program
{
private static NotifyIcon notifyIcon;
private static HotKeyManager hotKeyManager;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
// We need to dispose here, or the icon will not remove until the
// system tray is updated.
System.Windows.Forms.Application.ApplicationExit += delegate{ notifyIcon.Dispose(); };
CreateNotifyIcon();
System.Windows.Forms.Application.Run();
}
/// <summary>
/// Creates the icon that sits in the system tray.
/// </summary>
private static void CreateNotifyIcon()
{
notifyIcon = new NotifyIcon
{
Icon = Resources.AppIcon,
ContextMenu = GetContextMenu()
};
notifyIcon.Visible = true;
/*------------------------------------------------------------*/
hotKeyManager = new HotKeyManager();//Creates Hotkey manager from GlobalHotKey
var hotKey = hotKeyManager.Register(Key.F12, ModifierKeys.Control); // Sets Hotkey to Control + F12, ModifierKeys must be pressed first/
hotKeyManager.KeyPressed += HotKeyManagerPressed; //Creates void for "HotKeyManagerPressed"
void HotKeyManagerPressed(object sender, KeyPressedEventArgs e) //Checks to see if hotkey is pressed
{
if(e.HotKey.Key == Key.F12)
{
TakeFullScreenShotAsync();
BalloonTip();
// MessageBox.Show("Screenshot Taken");
}
}
}
/// <summary>
/// Creates BalloonTip to notify you that your Screenshot was taken.
/// </summary>
private static void BalloonTip()
{
notifyIcon.Visible = true;
notifyIcon.Icon = SystemIcons.Information;
notifyIcon.ShowBalloonTip(740, "Important From Screenshot", "Screenshot Taken", ToolTipIcon.Info);
}
private static void BalloonTip2()
{
notifyIcon.Visible = true;
notifyIcon.Icon = SystemIcons.Information;
notifyIcon.ShowBalloonTip(1, "Important From Screenshot", "Screenshot will now begin on startup", ToolTipIcon.Info);
}
///<summary>
///Creates the contextmenu for the Icon
///<summary>
private static ContextMenu GetContextMenu()
{
string myPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add("Take Screenshot (Ctrl+F12)", delegate { TakeFullScreenShotAsync(); });
menu.MenuItems.Add("Open Folder", delegate { prc.Start(); });
menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); });
menu.MenuItems.Add("Run On Startup", delegate { RunOnStartup(); });
return menu;
}
/// <summary>
/// Simple function that finds Registry and adds the Application to the startup
/// </summary>
private static void RunOnStartup()
{
RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
reg.SetValue("MyApp", Application.ExecutablePath.ToString());
BalloonTip2();
MessageBox.Show("The Program will now start on startup");
}
/// <summary>
/// Gets points for the screen uses those points to build a bitmap of the screen and saves it.
/// </summary>
private static async void TakeFullScreenShotAsync()
{
await Task.Delay(750);//Allows task to be waited on for .75ths of a second. Time In ms.
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(screenshot))
{
System.Drawing.Point origin = new System.Drawing.Point(0, 0);
System.Drawing.Size screenSize = Screen.PrimaryScreen.Bounds.Size;
//Copy Entire screen to entire bitmap.
graphics.CopyFromScreen(origin, origin, screenSize);
}
//Check to see if the file exists, if it does, append.
int append = 1;
while (File.Exists($"Screenshot{append}.jpg"))
append++;
string fileName = $"Screenshot{append}.jpg";
screenshot.Save(fileName, ImageFormat.Jpeg);
}
}
}
}
Now you may not need all of that but I want to make sure i didn't mess anything up in the process, and yes my Resources are being found and the Icon is set, I'm just not understanding why this isn't working.
I just copied all your code there's a small problem, not sure it's you copy paste error You need to keep HotKeyManagerPressed outside. It worked for me with this, I see the notification for me it's windows 10 notification.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using GlobalHotKey;
using System.Windows.Input;
using System.Timers;
using System.Threading.Tasks;
using Microsoft.Win32;
using FullScreenShot.Properties;
namespace FullScreenShot
{
class Program
{
private static NotifyIcon notifyIcon;
private static HotKeyManager hotKeyManager;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
// We need to dispose here, or the icon will not remove until the
// system tray is updated.
System.Windows.Forms.Application.ApplicationExit += delegate { notifyIcon.Dispose(); };
CreateNotifyIcon();
System.Windows.Forms.Application.Run();
}
/// <summary>
/// Creates the icon that sits in the system tray.
/// </summary>
private static void CreateNotifyIcon()
{
notifyIcon = new NotifyIcon
{
Icon = Resources.AppIcon,
ContextMenu = GetContextMenu()
};
notifyIcon.Visible = true;
/*------------------------------------------------------------*/
hotKeyManager = new HotKeyManager();//Creates Hotkey manager from GlobalHotKey
var hotKey = hotKeyManager.Register(Key.F12, ModifierKeys.Control); // Sets Hotkey to Control + F12, ModifierKeys must be pressed first/
hotKeyManager.KeyPressed += HotKeyManagerPressed; //Creates void for "HotKeyManagerPressed"
}
private static void HotKeyManagerPressed(object sender, KeyPressedEventArgs e) //Checks to see if hotkey is pressed
{
if (e.HotKey.Key == Key.F12)
{
TakeFullScreenShotAsync();
BalloonTip();
// MessageBox.Show("Screenshot Taken");
}
}
/// <summary>
/// Creates BalloonTip to notify you that your Screenshot was taken.
/// </summary>
private static void BalloonTip()
{
notifyIcon.Visible = true;
notifyIcon.Icon = SystemIcons.Information;
notifyIcon.ShowBalloonTip(740, "Important From Screenshot", "Screenshot Taken", ToolTipIcon.Info);
}
private static void BalloonTip2()
{
notifyIcon.Visible = true;
notifyIcon.Icon = SystemIcons.Information;
notifyIcon.ShowBalloonTip(1, "Important From Screenshot", "Screenshot will now begin on startup", ToolTipIcon.Info);
}
///<summary>
///Creates the contextmenu for the Icon
///<summary>
private static ContextMenu GetContextMenu()
{
string myPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add("Take Screenshot (Ctrl+F12)", delegate { TakeFullScreenShotAsync(); });
menu.MenuItems.Add("Open Folder", delegate { prc.Start(); });
menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); });
menu.MenuItems.Add("Run On Startup", delegate { RunOnStartup(); });
return menu;
}
/// <summary>
/// Simple function that finds Registry and adds the Application to the startup
/// </summary>
private static void RunOnStartup()
{
RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
reg.SetValue("MyApp", Application.ExecutablePath.ToString());
BalloonTip2();
MessageBox.Show("The Program will now start on startup");
}
/// <summary>
/// Gets points for the screen uses those points to build a bitmap of the screen and saves it.
/// </summary>
private static async void TakeFullScreenShotAsync()
{
await Task.Delay(750);//Allows task to be waited on for .75ths of a second. Time In ms.
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(screenshot))
{
System.Drawing.Point origin = new System.Drawing.Point(0, 0);
System.Drawing.Size screenSize = Screen.PrimaryScreen.Bounds.Size;
//Copy Entire screen to entire bitmap.
graphics.CopyFromScreen(origin, origin, screenSize);
}
//Check to see if the file exists, if it does, append.
int append = 1;
while (File.Exists($"Screenshot{append}.jpg"))
append++;
string fileName = $"Screenshot{append}.jpg";
screenshot.Save(fileName, ImageFormat.Jpeg);
}
}
}
}
Check the image
You haven't called the BalloonTip() method in your TakeFullScreenShotAsync() method.
private static async void TakeFullScreenShotAsync()
{
await Task.Delay(750);//Allows task to be waited on for .75ths of a second. Time In ms.
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(screenshot))
{
System.Drawing.Point origin = new System.Drawing.Point(0, 0);
System.Drawing.Size screenSize = Screen.PrimaryScreen.Bounds.Size;
//Copy Entire screen to entire bitmap.
graphics.CopyFromScreen(origin, origin, screenSize);
}
//Check to see if the file exists, if it does, append.
int append = 1;
while (File.Exists($"Screenshot{append}.jpg"))
append++;
string fileName = $"Screenshot{append}.jpg";
screenshot.Save(fileName, ImageFormat.Jpeg);
// Call the Show Tip Message Here...
BalloonTip();
}
}
Line: hotKeyManager.Register(...) threw an error since I have a hotkey already registered hence I change it to something else that worked for me.

How can I create 'pages' from scratch to be printed or print previewed in c#?

Due to the overwhelming complexity and/or limited license capabilities of the components available for this job, I have decided to write this component from scratch. This is something I have fully functional in PHP, and in VB6. but I am hitting a wall when trying to add a page .
A lot of great examples on how to print from file, or how to print a single page (all graphics etc are hard coded for the pages inside the Print event), but nothing on how to setup a collection to hold the page data, and then send those to be printed.
In vb6, you can obtain the pagebounds and call new page, but in .NET, there doesn't seem to be a new page method.
Following is the source I have so far, which is pretty rough due to the apparent lack of this basic functionality.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using PdfFileWriter;
using System.Drawing.Printing;
using System.ComponentModel;
using System.IO;
using System.Drawing.Printing;
class PDF : PrintDocument {
/// <summary>
/// Logo to display on invoice
/// </summary>
public Image Logo { get; set; }
/// <summary>
/// Pages drawn in document
/// </summary>
private List<Graphics> Pages;
private int CurrentPage;
private string directory;
private string file;
/// <summary>
/// Current X position
/// </summary>
public int X { get; set; }
/// <summary>
/// Current X position
/// </summary>
public int Y { get; set; }
/// <summary>
/// Set the folder where backups, downloads, etc will be stored or retrieved from
/// </summary>
[Editor( typeof( System.Windows.Forms.Design.FolderNameEditor ), typeof( System.Drawing.Design.UITypeEditor ) )]
public string Folder { get { return directory; } set { directory=value; } }
public PDF() {
file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
CurrentPage = 0;
// initialize pages array
Pages = new List<Graphics>();
PrinterSettings = new PrinterSettings() {
PrinterName = "Microsoft Print to PDF",
PrintToFile = true,
PrintFileName = Path.Combine(directory, file + ".pdf"),
};
DefaultPageSettings = new PageSettings(PrinterSettings) {
PaperSize=new PaperSize("Letter", 850, 1100 ),
Landscape = false,
Margins = new Margins(left: 50, right: 50, top: 50, bottom: 50),
};
}
/// <summary>
/// Get specific page
/// </summary>
/// <param name="page">page number. 1 based array</param>
/// <returns></returns>
public Graphics GetPage( int page ) {
int p = page - 1;
if ( p<0||p>Pages.Count ) { return null; }
return Pages[p];
}
public Graphics GetCurrentPage() {
return GetPage(CurrentPage);
}
protected override void OnBeginPrint( PrintEventArgs e ) {
base.OnBeginPrint( e );
}
protected override void OnPrintPage( PrintPageEventArgs e ) {
base.OnPrintPage( e );
}
protected override void OnEndPrint( PrintEventArgs e ) {
base.OnEndPrint( e );
}
/// <summary>
/// Add a new page to the document
/// </summary>
public void NewPage() {
// Add a new page to the page collection and set it as the current page
Graphics g = Graphics.CreateCraphics(); // not sure if this works, but no CreateGraphics is available
Pages.Add( g );
}
/// <summary>
/// Add a new string to the current page
/// </summary>
/// <param name="text">The string to print</param>
/// <param name="align">Optional alignment of the string</param>
public void DrawString(string text, System.Windows.TextAlignment align = System.Windows.TextAlignment.Left ) {
// add string to document
Pages[CurrentPage].DrawString(text, new Font("Arial", 10), new SolidBrush(Color.Black), new PointF(X, Y));
}
/// <summary>
/// Save the contents to PDF
/// </summary>
/// <param name="FileName"></param>
public void Save( string FileName ) {
// Start the print job looping through pages.
foreach ( Graphics page in Pages ) {
// there doesn't seem to be an addpage method
}
/*
* From stackoverflow article on how to 'print' a pdf to filename as the poster complained
* that the PrinterSettings.PrintFileName property is ignored. Havn't tested yet. Also, no
* such function as 'PrintOut' so further research required.
*
PrintOut(
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
FileName,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value
);
*/
}
}
I am not looking for a really long winded massive project on how to write PDF documents as they are all very restrictive, each of them has at least one limitation which is a problem for the layout I intend to design (upgrade from PHP which was an upgrade from VB6). The end result layout looks like this >
First Page (Invoice main page )
Second Page(s) [summary]
This report may have more pages depending on how many items are in payments and services. Header of the sub-report that continues rolls over to the next page if there are many items. For example, if a customer has 200 services, those items will continue in a similar fashion using the same "Payments" header block at the start of each consecutive page.
Detailed Reports
There may be multiple detail reports, each one starting at the beginning of a new page, and the page counter is reset and printed for those pages. So page 6 of the invoice might actually be Page 3 of the second detail report. Each report starts and ends like the following (and picture depicts layout of field data, etc)
Report first page
Report last page
What am I looking for ?
A reliable way to make the above multi-report invoice layout work in Visual Studio .NET. I am looking to port code away from php and vb6, and I am not interested in using libraries that are either massive in distribution size, or ridiculously complex / limited license restrictions. Microsoft provides some very powerful tools built-in, and I am not adverse to using the built-in PDF print driver and spooling the data, even though that is a bit of a hack, it does seem to be the least complex method to make this functional without the restrictions or bloat of 3rd party controls. (including open source, as the ones I looked at tend to do some very strange conversions to char, then maybe latex or something, not entirely sure what all the conversion stuff is about).
NOTE
It is very important to understand that the combination of the above report styles make up ONE invoice, and thus only one pdf file per client. If it helps, here is a VB6 backwards compatability method exposing the traditional 'Print' object printing compatability vb6. This should help clarify the native functionality I am looking to create/use.
I am having a difficult time swallowing the above "no direct equivalent" statement, as adding a new page when creating a document in memory seems to be a pretty basic (and essential) function of creating a document for print. It doesn't make sense that everything needed to be printed MUST be loaded from a file first.
I've created a very bare bones demo of the .NET printing system that mimics the basic invoice layout you specified. Full code is available here, but I will summarize the important parts below. I'm going to continue working on this and improving it because it was kind of fun to create.
As of now, it produces output that looks like this:
Example of text quality:
InvoiceDocument is responsible for printing an instance of an Invoice:
class InvoiceDocument : PrintDocument
{
public InvoiceDocument(Invoice invoice)
{
_invoice = invoice;
_currentSection = new MainPage(this);
}
private Invoice _invoice;
public Invoice Invoice => _invoice;
private InvoiceSection _currentSection;
public InvoiceSection CurrentSection => _currentSection;
#region Fonts
private Font _titleFont = new Font(FontFamily.GenericSansSerif, 18, FontStyle.Bold);
public Font TitleFont => _titleFont;
private Font _headerFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
public Font HeaderFont => _headerFont;
private Font _regularFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
public Font RegularFont => _regularFont;
private Font _boldFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
public Font BoldFont => _boldFont;
#endregion
protected override void OnPrintPage(PrintPageEventArgs e)
{
_currentSection?.Render(e);
}
public void ChangeSection(InvoiceSection nextSection)
{
_currentSection = nextSection;
}
}
InvoiceDocument is composed of subclasses of InvoiceSection. Each section knows how to print different parts of the invoice, the main page, summary, details, etc. It is also responsible for knowing when and how to wrap to the next page:
abstract class InvoiceSection
{
protected InvoiceSection(InvoiceDocument invoiceDocument)
{
this.InvoiceDocument = invoiceDocument;
}
public InvoiceDocument InvoiceDocument { get; }
public abstract void Render(PrintPageEventArgs e);
public Invoice Invoice => InvoiceDocument?.Invoice;
}
internal class MainPage : InvoiceSection
{
public MainPage(InvoiceDocument invoiceDocument) : base(invoiceDocument) { }
public override void Render(PrintPageEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Green, e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Left + 100, e.MarginBounds.Top + 100);
e.Graphics.DrawString(Invoice.CompanyName, InvoiceDocument.TitleFont, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top + 30);
e.HasMorePages = true;
InvoiceDocument.ChangeSection(new SummmarySection(InvoiceDocument));
}
}
internal class SummmarySection : InvoiceSection
{
public SummmarySection(InvoiceDocument invoiceDocument) : base(invoiceDocument)
{
}
public override void Render(PrintPageEventArgs e)
{
e.Graphics.FillRectangle(Brushes.LightGray, e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, 20);
e.Graphics.DrawString("Payments", InvoiceDocument.HeaderFont, Brushes.Black, e.MarginBounds.Left + 200, e.MarginBounds.Top + 2);
int y = e.MarginBounds.Top + 25;
while (_currentPaymentIndex < Invoice.Payments.Count && y < e.MarginBounds.Bottom)
{
Payment payment = Invoice.Payments[_currentPaymentIndex];
e.Graphics.DrawString(payment.Description, InvoiceDocument.RegularFont, Brushes.Black, e.MarginBounds.Left + 150, y);
e.Graphics.DrawString($"{payment.Amount:C}", InvoiceDocument.RegularFont, Brushes.Black, e.MarginBounds.Right - 150, y);
y = y + InvoiceDocument.RegularFont.Height;
_currentPaymentIndex++;
}
if (_currentPaymentIndex < Invoice.Payments.Count)
{
e.HasMorePages = true;
}
}
private int _currentPaymentIndex = 0;
}
This is my implementation of a working solution to this problem, allowing a user to completely design the document in a reusable manner without ever requiring to send the .Print command.
The concept of using an Image to store the data was in part due to a comment by Bradley Uffner on this question regarding combining two Graphics objects
There are several advantages and disadvantages to handling the process in this manner broken down below.
Advantages
The printer stuff is more modular, and reusable in other projects with different print requirements
Pages can be removed or even inserted (depending on the type of reports being done, this could save a lot of database query time where a cover page requires a summary, but the details for that summary are to be printed later)
Pages can be saved individually as image files
Pages can be serialized
The OnPrintPage isn't overly complex
Dynamic page positioning on current page or any other page in the array. Very easy to switch and place data elsewhere.
Disadvantages
Uses a bit more resources. Potential memory limitations if the Image array gets really big.
Class File
This also demonstrates how portable this is, as anyone can reuse it quickly. I am still working on wrapping the Draw methods, however this code demonstrates the objective needing only to really extend with more draw methods, and possibly some other features I may have missed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Printing;
using System.ComponentModel;
using System.IO;
class PDF : PrintDocument {
/// <summary>
/// Logo to display on invoice
/// </summary>
public Image Logo { get; set; }
/// <summary>
/// Current X position on canvas
/// </summary>
public int X { get; set; }
/// <summary>
/// Current Y position on canvas
/// </summary>
public int Y { get; set; }
/// <summary>
/// Set the folder where backups, downloads, etc will be stored or retrieved from
/// </summary>
[Editor( typeof( System.Windows.Forms.Design.FolderNameEditor ), typeof( System.Drawing.Design.UITypeEditor ) )]
public string Folder { get { return directory; } set { directory=value; } }
/// <summary>
/// Current font used to print
/// </summary>
public Font Font { get; set; }
/// <summary>
/// Current font color
/// </summary>
public Color ForeColor { get; set; }
private int CurrentPagePrinting { get; set; }
/// <summary>
/// Set printer margins
/// </summary>
public Margins PrintMargins {
get { return DefaultPageSettings.Margins; }
set { DefaultPageSettings.Margins = value; }
}
/// <summary>
/// Pages drawn in document
/// </summary>
public List<Image> Pages { get; private set; }
/// <summary>
/// The current selected page number. 0 if nothing selected
/// </summary>
private int CurrentPage;
/// <summary>
/// The current working directory to save files to
/// </summary>
private string directory;
/// <summary>
/// The currently chosen filename
/// </summary>
private string file;
/// <summary>
/// Public acceisble object to all paperSizes as set
/// </summary>
public List<PrintPaperSize> PaperSizes { get; private set; }
/// <summary>
/// Object for holding papersizes
/// </summary>
public class PrintPaperSize {
public string Name { get; set; }
public double Height { get; set; }
public double Width { get; set; }
public PaperKind Kind { get; set; }
public PrintPaperSize() {
Height = 0;
Width = 0;
Name = "";
Kind = PaperKind.Letter;
}
public PrintPaperSize( string name, double height, double width, PaperKind kind ) {
Height=height;
Width=width;
Name=name;
Kind=kind;
}
}
/// <summary>
/// Set the spacing between lines in percentage. Affects Y position. Range(%): 1 - 1000
/// </summary>
private int lineSpacing;
public int LineSpacing {
get {
return lineSpacing;
}
set {
if(value > 0 && value < 1000) {
lineSpacing = value;
}
}
}
/// <summary>
/// Current papersize selected. used for some calculations
/// </summary>
public PrintPaperSize CurrentPaperSize { get; private set; }
public PDF() {
// set the file name without extension to something safe
file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
// set the save directory to MyDocuments
directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
CurrentPage = 0;
// initialize pages array
Pages = new List<Image>();
// Set the initial font and color
Font = new System.Drawing.Font("Arial", (float)11.25, FontStyle.Regular, GraphicsUnit.Point);
ForeColor = Color.Black;
lineSpacing = 100;
// set the printer to Microsoft's PDF printer and generate and ensure it will save to a file
PrinterSettings = new PrinterSettings() {
PrinterName = "Microsoft Print to PDF",
PrintToFile = true,
PrintFileName = Path.Combine(directory, file + ".pdf"),
};
// hide the notice 'printing' while spooling job.
PrintController = new StandardPrintController();
// set the printer quality to maximum so we can use this for getting the dpi at this setting
DefaultPageSettings.PrinterResolution.Kind = PrinterResolutionKind.High;
// store all paper sizes at 1 dpi [ reference: https://social.msdn.microsoft.com/Forums/vstudio/en-US/05169a47-04d5-4890-9b0a-7ad11a6a87f2/need-pixel-width-for-paper-sizes-a4-a5-executive-letter-legal-executive?forum=csharpgeneral ]
PaperSizes = new List<PrintPaperSize>();
foreach ( PaperSize P in PrinterSettings.PaperSizes ) {
double W=P.Width/100.0;
double H=P.Height/100.0;
PaperSizes.Add(
new PrintPaperSize() {
Height = H,
Width = W,
Name = P.PaperName,
Kind = P.Kind
}
);
if ( P.PaperName=="Letter" ) {
CurrentPaperSize = PaperSizes[PaperSizes.Count-1];
}
}
// setup the initial page type, orientation, margins,
using ( Graphics g=PrinterSettings.CreateMeasurementGraphics() ) {
DefaultPageSettings = new PageSettings(PrinterSettings) {
PaperSize=new PaperSize( CurrentPaperSize.Name, (Int32)(CurrentPaperSize.Width*g.DpiX), (Int32)(CurrentPaperSize.Height*g.DpiY) ),
Landscape = false,
Margins = new Margins(left: 100, right: 100, top: 10, bottom: 10),
PrinterResolution=new PrinterResolution() {
Kind = PrinterResolutionKind.High
}
};
}
// constrain print within margins
OriginAtMargins = false;
}
public void SetPaperSize( PaperKind paperSize ) {
// TODO: Use Linq on paperSizes
}
/// <summary>
/// Get specific page
/// </summary>
/// <param name="page">page number. 1 based array</param>
/// <returns></returns>
public Image GetPage( int page ) {
int p = page - 1;
if ( p<0||p>Pages.Count ) { return null; }
return Pages[p];
}
/// <summary>
/// Get the current page
/// </summary>
/// <returns>Image</returns>
public Image GetCurrentPage() {
return GetPage(CurrentPage);
}
/// <summary>
/// Before printing starts
/// </summary>
/// <param name="e">PrintEventArgs</param>
protected override void OnBeginPrint( PrintEventArgs e ) {
CurrentPagePrinting=0;
base.OnBeginPrint( e );
}
/// <summary>
/// Print page event
/// </summary>
/// <param name="e">PrintPageEventArgs</param>
protected override void OnPrintPage( PrintPageEventArgs e ) {
CurrentPagePrinting++;
// if page count is max exit print routine
if ( CurrentPagePrinting==Pages.Count ) { e.HasMorePages=false; } else { e.HasMorePages=true; }
// ensure high resolution / clarity of image so text doesn't fuzz
e.Graphics.CompositingMode=CompositingMode.SourceOver;
e.Graphics.CompositingQuality=CompositingQuality.HighQuality;
// Draw image and respect margins (unscaled in addition to the above so text doesn't fuzz)
e.Graphics.DrawImageUnscaled(
Pages[CurrentPagePrinting-1],
// new Point(0,0)
new Point(
DefaultPageSettings.Margins.Left,
DefaultPageSettings.Margins.Top
)
);
base.OnPrintPage( e );
}
/// <summary>
/// After printing has been completed
/// </summary>
/// <param name="e">PrintEventArgs</param>
protected override void OnEndPrint( PrintEventArgs e ) {
base.OnEndPrint( e );
}
/// <summary>
/// Add a new page to the document
/// </summary>
public void NewPage() {
// Add a new page to the page collection and set it as the current page
Bitmap bmp;
using(Graphics g = PrinterSettings.CreateMeasurementGraphics()) {
int w=(Int32)( CurrentPaperSize.Width*g.DpiX )-(Int32)( ( ( DefaultPageSettings.Margins.Left+DefaultPageSettings.Margins.Right )/100 )*g.DpiX );
int h=(Int32)( CurrentPaperSize.Height*g.DpiY )-(Int32)( ( ( DefaultPageSettings.Margins.Top+DefaultPageSettings.Margins.Bottom )/100 )*g.DpiY );
bmp = new Bitmap( w, h );
bmp.SetResolution(g.DpiX, g.DpiY);
}
// reset X and Y positions
Y=0;
X=0;
// Add new page to the collection
Pages.Add( bmp );
CurrentPage++;
}
/// <summary>
/// Change the current page to specified page number
/// </summary>
/// <param name="page">page number</param>
/// <returns>true if page change was successful</returns>
public bool SetCurrentPage( int page ) {
if ( page<1 ) { return false; }
if ( page>Pages.Count ) { return false; }
CurrentPage = page - 1;
return true;
}
/// <summary>
/// Remove the specified page #
/// </summary>
/// <param name="page">page number</param>
/// <returns>true if successful</returns>
public bool RemovePage(int page) {
if ( page<1 ) { return false; }
if ( page>Pages.Count ) { return false; }
if ( Pages.Count-page==0 ) {
CurrentPage = 0;
Pages.RemoveAt(page - 1);
} else {
if ( page==CurrentPage && CurrentPage == 1 ) {
Pages.RemoveAt(page - 1);
} else {
CurrentPage = CurrentPage - 1;
Pages.RemoveAt(page -1);
}
}
return true;
}
/// <summary>
/// Add a new string to the current page
/// </summary>
/// <param name="text">The string to print</param>
/// <param name="align">Optional alignment of the string</param>
public void DrawString(string text, System.Windows.TextAlignment align = System.Windows.TextAlignment.Left ) {
// add string to document
using ( Graphics g=Graphics.FromImage( Pages[CurrentPage - 1] ) ) {
g.CompositingQuality = CompositingQuality.HighQuality;
// get linespacing and adjust by user specified linespacing
int iLineSpacing=(Int32)( g.MeasureString( "X", Font ).Height*(float)( (float)LineSpacing/(float)100 ) );
switch ( align ) {
case System.Windows.TextAlignment.Left:
case System.Windows.TextAlignment.Justify:
g.DrawString( text, Font, new SolidBrush( ForeColor ), new PointF( X, Y ) );
break;
case System.Windows.TextAlignment.Right:
g.DrawString( text, Font, new SolidBrush( ForeColor ), new PointF( Pages[CurrentPage - 1].Width - g.MeasureString( text, Font ).Width, Y ) );
break;
case System.Windows.TextAlignment.Center:
g.DrawString( text, Font, new SolidBrush( ForeColor ), new PointF( ( Pages[CurrentPage-1].Width+g.MeasureString( text, Font ).Width )/2, Y ) );
break;
}
Y+=iLineSpacing;
if( Y + iLineSpacing > Pages[CurrentPage-1].Height ) {
NewPage();
}
}
}
}
Example Usage
// initialize a new PrintDocument
PDF print = new PDF();
// set the font
print.Font = new Font("Helvetica", (float)12, FontStyle.Regular, GraphicsUnit.Point);
// change the color (can be used for shapes, etc once their draw methods are added to the PDF() class)
print.ForeColor = Color.Red;
// create a new page !!!!
print.NewPage();
// add some text
print.DrawString( "Hello World !!" );
// add some right aligned text
print.DrawString( "Aligned Right", System.Windows.TextAlignment.Right );
// add some centered text
print.DrawString( "Aligned Right", System.Windows.TextAlignment.Center );
// change line spacing ( percentage between 1% and 1000% )
print.LineSpacing = 50; // 50% of drawstrings detected line height
// add another page
print.NewPage();
// print a couple lines
print.DrawString( "Hello World" );
print.DrawString( "Hello World" );
// change the color again and print another line
ForeColor = Color.Yellow;
print.DrawString( "Hello World" );
// duplicate a page (clone page 1 as page 3 )
print.NewPage();
print.Pages[print.Pages -1] = print.GetPage(1);
// go back to page 1 and print some more text at specified coordinates
print.SetCurrentPage(1);
print.X = 400;
print.Y = 300;
print.DrawString( "Drawn after 3rd page created" );
// send the print job
print.Print();
// reprint
print.Print();
// show a preview of the 2nd page
/*
Image img = print.GetPage(1);
pictureBox1.Height=(Int32)(print.CurrentPaperSize.Height*img.VerticalResolution);
pictureBox1.Width = (Int32)(print.CurrentPaperSize.Width*img.HorizontalResolution);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = img;
*/

Categories