Jim Lawless' Blog


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

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: Command-Line Image Format Conversion
Next post:Book Review : Using Google App Engine


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Along Came AWK

A TCP Command Line Interface in Rhino JavaScript

Throwaway Software: HangUp

An SMTP Server Simulator in Perl

Preserving my Favorite HN Links

Twimmando: A Command-line Twitter Client

COM Scripting in C by way of JavaScript

The Protection Racket

WSH2EXE - The Final Chapter

My Foray into Shareware


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer