#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Runtime.Serialization.Formatters.Binary;
#endregion
namespace ClipboardImageArchiver
{
partial class MainForm : Form
{
private Popup popupWin = new Popup();
private Point ptOffset;
private short imgFormat = 0; //default JPEG
private string fileExt = "jpg";
private int curImgWidth, curImgHeight;
public MainForm()
{
InitializeComponent();
ShowPopup("Written by Ashish Patil [ashish_@rediffmail.com]");
}
private void label2_Click(object sender, EventArgs e)
{
this.Hide();
}
private void label1_Click(object sender, EventArgs e)
{
this.Close();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
string unqFilename = "BMP_" + Guid.NewGuid().ToString() + "."+this.fileExt;
bool bsuccess = CopyClipboardToFile(unqFilename);
if (bsuccess == true)
{
ZipImageObject zObj = CompressFile(unqFilename, this.imgFormat);
File.Delete(unqFilename);
SaveImageZiptoFile(zObj, "data.dat");
File.Delete(unqFilename);
ShowPopup(this.fileExt+" file (" + this.curImgWidth + " X " +this.curImgHeight+ ") saved .");
}
else
ShowPopup("No valid image data on clipboard ");
}
private void OnDragEnter(object sender, DragEventArgs e)
{
statusLabel.Text = "Dragging";
}
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
statusLabel.Text = "Dragging";
}
private void TitleClicked(object sender, MouseEventArgs e)
{
ptOffset = new Point(-e.X - label3.Left,-e.Y - label3.Top);
}
private void TitleMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(ptOffset.X, ptOffset.Y);
this.Location = mousePos;
}
}
private 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;
}
private void SaveImageZiptoFile(ZipImageObject zipObj, string destFile)
{
BinaryFormatter objFormatter = new BinaryFormatter();
FileStream fsZipData = new FileStream(destFile, FileMode.OpenOrCreate);
fsZipData.Seek(0, SeekOrigin.End);
objFormatter.Serialize(fsZipData, zipObj);
fsZipData.Close();
}
private void ExportToDirectory(string zipDataFile , string DestinationPath )
{
FileStream fsZipData = new FileStream(zipDataFile, FileMode.Open);
BinaryFormatter objFormatter = new BinaryFormatter();
progressBar1.Value = 0;
while (fsZipData.Position < fsZipData.Length - 1)
{
ZipImageObject obj = (ZipImageObject)(objFormatter.Deserialize(fsZipData));
MemoryStream ms = new MemoryStream(obj.ImgData);
FileStream fs = new FileStream(DestinationPath + "\\" + obj.Filename, FileMode.Create);
GZipInputStream gzipOutput = new GZipInputStream(ms);
int size = 2048;
byte[] writeData = new byte[2048];
while (true)
{
size = gzipOutput.Read(writeData, 0, size);
if (size > 0)
fs.Write(writeData, 0, size);
else
break;
}
gzipOutput.Close();
fs.Close();
ms.Close();
progressBar1.Increment((int)(100 *( fsZipData.Position/fsZipData.Length)));
}
fsZipData.Close();
}
private bool CopyClipboardToFile(string bmpFilename )
{
IDataObject d = Clipboard.GetDataObject();
Bitmap bmpImage ;
if ((d!=null) &&(d.GetDataPresent(DataFormats.Bitmap)))
{
bmpImage = (Bitmap)(Clipboard.GetDataObject().GetData(DataFormats.Bitmap));
curImgHeight = bmpImage.Height;
curImgWidth = bmpImage.Width;
bmpImage.Save(bmpFilename,GetImgFormatFromCode( this.imgFormat));
return true;
}
return false;
}
private void ShowPopup(string message)
{
Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
popupWin.Show();
popupWin.popupText = message;
popupWin.SetDesktopLocation(workingRectangle.Width - popupWin.Width, workingRectangle.Height - popupWin.Height);
}
private void button1_Click(object sender, EventArgs e)
{
if (rdoZip.Checked == false)
{
folderDialog1.SelectedPath = "";
folderDialog1.ShowDialog();
txtExportPath.Text = folderDialog1.SelectedPath;
}
else
{
saveFileDialog1.FileName = "";
saveFileDialog1.ShowDialog();
txtExportPath.Text = saveFileDialog1.FileName;
}
}
private void UpdateExportLabel(object sender, EventArgs e)
{
if (rdoZip.Checked == true)
{ lblExportPath.Text = "File Location"; txtExportPath.Text = ""; }
else
{ lblExportPath.Text = "Export Directory"; txtExportPath.Text = ""; }
}
private void btnExport_Click(object sender, EventArgs e)
{
if (txtExportPath.Text == "")
{
MessageBox.Show("Please select a valid directory or File path");
return;
}
if (rdoZip.Checked == false)
{
ExportToDirectory("data.dat", txtExportPath.Text);
ShowPopup("Images exported succesfully to " + txtExportPath.Text);
statusLabel.Text = "Operation Successful";
progressBar1.Value = 0;
}
else
{
ExportToZip("data.dat", txtExportPath.Text);
ShowPopup("Images exported succesfully to " + txtExportPath.Text);
statusLabel.Text = "Operation Successful";
progressBar1.Value = 0;
}
}
private void OmFormClick(object sender, MouseEventArgs e)
{
progressBar1.Value = 0;
statusLabel.Text = "";
}
private void AddFileToArchive(byte[] bmpData, ZipOutputStream zipOutput, string imgFileName)
{
ZipEntry zFileEntry = new ZipEntry(imgFileName);
Crc32 crc = new Crc32();
zFileEntry = new ZipEntry(imgFileName);
zFileEntry.Size = bmpData.Length;
zipOutput.SetLevel(9);
crc.Reset();
crc.Update(bmpData);
zFileEntry.Crc = crc.Value;
zFileEntry.DateTime = DateTime.Now;
zipOutput.PutNextEntry(zFileEntry);
zipOutput.Write(bmpData, 0, bmpData.Length);
}
private void ExportToZip(string zipDataFile, string archiveFile)
{
FileStream fsZipData = new FileStream(zipDataFile, FileMode.OpenOrCreate);
BinaryFormatter objFormatter = new BinaryFormatter();
FileStream fszipFile = new FileStream(archiveFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
ZipOutputStream zipOutput = new ZipOutputStream(fszipFile);
int bufsize = 2048;
int i = 0;
progressBar1.Value = 0;
while (fsZipData.Position < fsZipData.Length )
{
ZipImageObject obj = (ZipImageObject)(objFormatter.Deserialize(fsZipData));
MemoryStream ms = new MemoryStream(obj.ImgData);
GZipInputStream gzipOutput = new GZipInputStream(ms);
byte[] writeData = new byte[obj.Length];
bufsize = 2048;
i = 0;
while (true)
{
bufsize = gzipOutput.Read(writeData, i, bufsize);
i += bufsize;
if (i >= obj.Length) break;
}
AddFileToArchive(writeData, zipOutput, obj.Filename);
gzipOutput.Close();
ms.Close();
progressBar1.Increment((int)(100 * (fsZipData.Position / fsZipData.Length)));
}
zipOutput.Finish();
zipOutput.Close();
fsZipData.Close();
fszipFile.Close();
}
private void ResetFormatMenu()
{
jPEGToolStripMenuItem.Checked = false;
bITMAPToolStripMenuItem.Checked = false;
gIFToolStripMenuItem.Checked = false;
pNGToolStripMenuItem.Checked = false;
wMFToolStripMenuItem.Checked = false;
eMFToolStripMenuItem.Checked = false;
tIFFToolStripMenuItem.Checked = false;
}
private void FormatOnClick(object sender, ToolStripItemClickedEventArgs e)
{
switch (e.ClickedItem.Text)
{
case ("JPEG"): { this.imgFormat = 0; this.fileExt = "jpg"; break; }
case ("BITMAP"): { this.imgFormat = 1; this.fileExt = "bmp"; break; }
case ("GIF"): { this.imgFormat = 2; this.fileExt = "gif"; break; }
case ("PNG"): { this.imgFormat = 3; this.fileExt = "png"; break; }
case ("WMF"): { this.imgFormat = 4; this.fileExt = "wmf"; break; }
case ("EMF"): { this.imgFormat = 5; this.fileExt = "emf"; break; }
case ("TIFF"): { this.imgFormat = 6; this.fileExt = "tif"; break; }
default: { this.imgFormat = 0; this.fileExt = "jpg"; break; }
}
ResetFormatMenu();
}
private ImageFormat GetImgFormatFromCode(short code)
{
switch (code)
{
case 0: return ImageFormat.Jpeg;
case 1: return ImageFormat.Bmp;
case 2: return ImageFormat.Gif;
case 3: return ImageFormat.Png;
case 4: return ImageFormat.Wmf;
case 5: return ImageFormat.Emf;
case 6: return ImageFormat.Tiff;
default : return ImageFormat.Jpeg;
}
}
}
}