Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#

Clipboard Image Archiver

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
22 May 20054 min read 45.2K   718   28   1
Method used by ClipboardImageArchiver - merits and demerits
ClipboardImageArchiver overcomes the problem of 'appending to zip' files using a combination of binary serialization and stream compression. This article mainly discusses this method used by ClipboardImageArchiver and its merits/demerits.

Introduction

ClipboardImageArchiver is a utility that archives clipboard images. It saves images from the clipboard to one single destination freeing you from remembering where exactly you saved your images. You can retrieve these images as a single zip file or to a directory. ClipboardImageArchiver also gives you the option of saving images in different formats. ClipboardImageArchiver uses the open source SharpZip compression library. ClipboardImageArchiver overcomes the problem of 'appending to zip' files using a combination of binary serialization and stream compression. This article mainly discusses this method used by ClipboardImageArchiver and its merits/demerits.

Image 1

Screenshot of the main application

All the user has to do is to ‘copy’ an image and right click the ClipboardImageArchiver icon in the task bar, and select ‘Paste’ from the menu. A small popup appears at the right corner of the screen giving information on the size, format and name of the saved image. The user can also choose a format by selecting ‘Format’ from the menu and clicking the desired format. Currently, seven formats are supported: JPEG, Bitmap, GIF, PNG, WMF, EMF and TIFF. By default, JPEG is selected.

Image 2

ClipboardImageArchiver minimized to task bar. Menu is shown on right clicking the icon (left most in task bar)

Image 3

Success message on saving of an image from clipboard

All the saved images can be retrieved simply by clicking ‘Export’ in the main screen. There are two options, one to export as a single file or to export as individual files to a directory. In both cases, the formats of individual files are retained.

The Problem of ‘Appending’ to a Zip File

There is no way we can directly append an entry to an existing zip archive. It is unlike appending text to text file. It is worth understanding here that any data appended to a zip file causes changes throughout the zip file. I mean, it does not cause localized changes.

So this means, every time I have to add something to, say a file named images.zip, these are the steps I need to perform:

  1. Initialize a new ZipOutputStream object. Copy items from old zip file to new zip file as zip entries.
  2. Create and add a new zip entry for the new file to be added.
  3. Call compress method on ZipOutputStreamObject, that saves it to a temp.zip file.
  4. Now delete images.zip and replace it with temp.zip.

This is the exact sequence of events that takes place, when you add a new file to a zip archive. This has the following obvious disadvantages:

  1. Every time you add a new entry, we have to create a temporary zip. Copy all entries from the old file to it.
  2. If the zip archives are very large, they will take up a lot of disk space and memory resources. This is very undesirable for a utility like ClipboardImageArchiver.
  3. Also, as the size of the images.zip file increases, it takes more and more time to add new entries. Very undesirable!!

Overcoming the Problem

To overcome this problem, we use a combination of serialization and stream compression. First, I created a class called ‘ZipImageObject’ that holds compressed data of the image in the following format:

ZipImageObject Class

[Serializable]

  • private long _length;

    The length in bytes of the actual uncompressed image

  • private byte [] _imgData;

    GZipped (binary) image byte stream. This is where the compression is done. The binary image is compressed using GZip algorithm from the library and the resulting data is stored as a byte stream.

  • private short _imgFormat;

    Integer indicating format of image

  • private string _fileName;

    Name of image [GUID]

When the user ‘pastes’ an image into the zip clip, the following sequence of events occurs:

Image 4

Diagram showing internal working of ClipboardImageArchiver.

The code below illustrates how a BMP file is transformed to a ZipImageObject using GZip compression. This ZipImageObject is serialized and stored into a data.dat file. The code is pretty straightforward and self-explanatory:

C#
public ZipImageObject CompressFile(string bmpFilename, short formatIndicator)
{
    FileStream fsbmpFile = new FileStream(bmpFilename, FileMode.Open);
    MemoryStream ms = new MemoryStream(Convert.ToInt32(fsbmpFile.Length));
    GZipOutputStream gzipOutput = new GZipOutputStream(ms);

    Byte[] bmpData = new Byte[Convert.ToInt32(fsbmpFile.Length)];
    fsbmpFile.Read(bmpData, 0, Convert.ToInt32(fsbmpFile.Length));

     gzipOutput.SetLevel(9);
     gzipOutput.Write(bmpData, 0, bmpData.Length);
     gzipOutput.Close();

     ZipImageObject zipImg = new ZipImageObject();
     zipImg.Filename = bmpFilename;
     zipImg.ImgData = ms.ToArray();
     zipImg.Length = fsbmpFile.Length;
     zipImg.ImgFormat =formatIndicator;

     fsbmpFile.Close();
     ms.Close();

     return zipImg;
}

One thing must be noted here that, we may not get an optimum compression here. I mean a file containing ten ZipImageObjects may occupy more space than a single zip file containing all the images. This is a disadvantage of this method. Even then, the difference in their sizes shouldn’t be much. When the user clicks ‘Export’, all the serialized ZipImageObjects are exported to a zip archive. The complete code for this application can be downloaded from the above link.

Important: To compile the code, you will have to add reference to ICSharpCode.SharpZipLib.dll in the IDE.

Conclusion

ClipboardImageArchiver demonstrates a way of avoiding frequent read/writes to disk while working with zip compression. It uses a combination of stream compression and serialization for the same. The technique is very useful when dealing with large data. Also, it is a very useful and handy utility to save images while browsing the web.

Hope you found this article useful. Any comments and criticism is always welcome.

History

  • 23rd May, 2005: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
India India
Ashish is an Electronics Engineer. He loves computers and programming, and always tries to do something different. Ashish started his programming career with FoxPro 2.5 (6 years ago) .Ashish has programmed in number of languages including C\C++ , Tcl\Tk , Assembly Language , JavaScript. Currently he works with ASP.NET, C# and VB.NET. He is currently working in multinational consulting company in India.

He devotes much of his free time to his website: http://ashishware.com , which contains lot of cool stuff on webdevelopment and programing in general.

Comments and Discussions

 
GeneralClipboardImageArchiver project file not available... Pin
xian12323-Feb-07 21:23
xian12323-Feb-07 21:23 
When I open the ClipboardImageArchiver.sln file, VS 2005 tells me that the "ClipboardImageArchiver" project has been removed (is unavailable). It looks like the author has removed the most interesting part of the source code, and that an executable cannot be built. Frown | :(

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.