Auto Save Images from the Clipboard
Originally published on: Mon, 05 Oct 2009 23:34:35 +0000
Please see the updated post here:
http://www.mailsend-online.com/blog/auto-save-clipboard-images-redux.html
The new post describes a version of the program below with a GUI interface.
I sometimes need to capture series of screenshots in an ordered sequence. I prefer to avoid having to click into a utility to save them one at a time.
I wrote the utility pic2file so that I could easily capture these screen images or window images.
Here is the C# source code:
pic2file.cs
// pic2file.cs - Automatically save bitmaps on the clipboard
// to the filesystem.
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System ;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.IO;
using System.Windows.Forms;
namespace Pic2File
{
public class Pic2File
{
[STAThread]
public static void Main(string[] args)
{
ImageFormat fmt;
string dir,prefix,outputFile,suffix;
int i;
int count;
fmt=ImageFormat.Jpeg;
suffix=".jpg";
dir=".\";
prefix="p2f_";
bool beep=false;
count=0;
Console.WriteLine(
"\nPic2File by Jim Lawless - jimbo@radiks.net\n");
Syntax();
for(i=0;i<args.Length;i++)
{
// Play a sound when img files are saved
if(args[i].ToLower().Equals("-beep"))
{
beep=true;
}
else
if(args[i].ToLower().Equals("-dir"))
{
dir=args[i+1];
// make sure we have an
// ending slash
if( ! dir.EndsWith("\"))
dir+="\";
i++;
}
else
if(args[i].ToLower().Equals("-prefix"))
{
prefix=args[i+1];
i++;
}
else
if(args[i].ToLower().Equals("-gif"))
{
fmt=ImageFormat.Gif;
suffix=".gif";
}
else
if(args[i].ToLower().Equals("-jpg"))
{
fmt=ImageFormat.Jpeg;
suffix=".jpg";
}
else
if(args[i].ToLower().Equals("-png"))
{
fmt=ImageFormat.Png;
suffix=".png";
}
else
if(args[i].ToLower().Equals("-bmp"))
{
fmt=ImageFormat.Bmp;
suffix=".bmp";
}
else
if(args[i].ToLower().Equals("-tiff"))
{
fmt=ImageFormat.Tiff;
suffix=".tif";
}
else
{
Console.WriteLine("Unknown option " + args[i]);
return;
}
}
// Write the selected options to the console
Console.WriteLine("\nWork directory: " + dir);
Console.WriteLine("Filename prefix: " + prefix);
Console.WriteLine("Image format: " + suffix);
Console.WriteLine("Play beep on save: " + beep );
// Create the work dir, if necessary
if( ! Directory.Exists(dir))
{
Console.WriteLine("Creating directory " + dir);
Directory.CreateDirectory(dir);
}
// Now, loop waiting for data to appear on the Clipboard
Console.WriteLine("\nWaiting...\n");
IDataObject cdata;
for(;;)
{
Thread.Sleep(100);
Application.DoEvents();
cdata=Clipboard.GetDataObject();
if(cdata==null)
continue;
if(cdata.GetDataPresent(DataFormats.Bitmap))
{
// Loop until we have a unique filename
for(;;)
{
outputFile=dir+prefix+count+suffix;
if( ! File.Exists(outputFile))
break;
count++;
}
Console.Write("Saving " + outputFile + " ... " );
Image im=(Image)cdata.GetData(DataFormats.Bitmap,true);
im.Save(outputFile,fmt);
Console.WriteLine("done.");
if(beep)
System.Media.SystemSounds.Beep.Play();
count++;
Clipboard.Clear();
}
}
}
public static void Syntax()
{
Console.WriteLine("Syntax:\tPic2File.exe -dir dir_to_store_pics -prefix filename_prefix -beep [ -gif -png -tiff -jpg -bmp ]\n");
Console.WriteLine("The default output format is -jpg.\n");
}
}
}
Upon invoking pic2file, you will see:
Pic2File by Jim Lawless - jimbo@radiks.net
Syntax: Pic2File.exe -dir dir_to_store_pics -prefix filename_prefix -beep [ -gif
-png -tiff -jpg -bmp ]
The default output format is -jpg.
Work directory: .\
Filename prefix: p2f_
Image format: .jpg
Play beep on save: False
Waiting...
If you have an image waiting on the Clipboard or if you press the Print Screen key, you'll then see a message that might look like the following:
Saving .\p2f_0.jpg ... done.
Pic2file loops continuously. It pauses for 100 milliseconds, then tries to pull bitmap image data from the clipboard. If such data is present, it will save it to an incrementally-numbered file and will conditionally play an audible sound if the -beep option has been specified.
If you right-click an image on a web page and click your browser's option to copy the image to the Clipboard, pic2file will also save the image.
Pressing Alt-Print Screen will copy an image of the current window to the Clipboard. Pic2file will then save this image to a file.
After an image has been saved, pic2file erases the data on the Clipboard.
The source and executable file for pic2file can be downloaded in a single archive at:
http://www.mailsend-online.com/wp/pic2file.zip
Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks