|
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Clipper_c {
/// <summary>
/// Copyright Nicholas Symmonds 2002
/// This software is for instructional purposes only.
/// It may not be sold as is.
/// </summary>
public class Clipper : System.Windows.Forms.Form {
Bitmap bmp;
NotifyIcon trayIcon = new NotifyIcon();
ContextMenu trayIconMenu = new ContextMenu();
private System.Windows.Forms.Button cmdCatch;
private System.Windows.Forms.Button cmdQuit;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Clipper() {
InitializeComponent();
this.Icon = new Icon("icon.ico");
this.BackColor = Color.BlanchedAlmond;
this.TransparencyKey = this.BackColor;
this.cmdCatch.BackColor = Color.Tomato;
this.cmdQuit.BackColor = Color.Tomato;
trayIconMenu.MenuItems.Add("Catch",
new EventHandler(this.cmdCatch_Click));
trayIconMenu.MenuItems.Add("Always On Top",
new EventHandler(this.ClipperOnTop));
trayIconMenu.MenuItems.Add("Show",
new EventHandler(this.Show_Main));
trayIconMenu.MenuItems.Add("Quit",
new EventHandler(this.cmdQuit_Click));
trayIcon.Icon = new Icon("icon.ico");
trayIcon.Text = "Clipper - Screen Capture";
trayIcon.ContextMenu = trayIconMenu;
trayIcon.Visible = true;
this.ShowInTaskbar = false;
}
protected override void Dispose( bool disposing ) {
if( disposing ) {
if (components != null) {
components.Dispose();
}
if (bmp != null)
bmp.Dispose();
trayIcon.Dispose();
}
base.Dispose( disposing );
}
#region Windows Form 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() {
this.cmdCatch = new System.Windows.Forms.Button();
this.cmdQuit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cmdCatch
//
this.cmdCatch.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.cmdCatch.Location = new System.Drawing.Point(16, 24);
this.cmdCatch.Name = "cmdCatch";
this.cmdCatch.Size = new System.Drawing.Size(88, 32);
this.cmdCatch.TabIndex = 0;
this.cmdCatch.Text = "&Capture";
this.cmdCatch.Click += new System.EventHandler(this.cmdCatch_Click);
//
// cmdQuit
//
this.cmdQuit.Location = new System.Drawing.Point(144, 24);
this.cmdQuit.Name = "cmdQuit";
this.cmdQuit.Size = new System.Drawing.Size(64, 32);
this.cmdQuit.TabIndex = 1;
this.cmdQuit.Text = "&Quit";
this.cmdQuit.Click += new System.EventHandler(this.cmdQuit_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(224, 75);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdQuit,
this.cmdCatch});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Screen Capture";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main() {
Application.Run(new Clipper());
}
private void Form1_Load(object sender, System.EventArgs e) {
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
if (this.WindowState == FormWindowState.Minimized)
this.Opacity = 0;
else
this.Opacity = 1;
}
/// <summary>
/// The desktop capture method makes this form invisible before
/// showing the picture. Once the Save form has run this form becomes
/// visible. Making this form invisible is done via the forms opacity.
/// </summary>
private void cmdCatch_Click(object sender, System.EventArgs e) {
bmp = DeskTop.Capture();
//Make the form invisible
this.Opacity = 0;
dtBitmap bmpShow = new dtBitmap(bmp);
bmp = bmpShow.GetBitmap;
if (bmp != null) {
frmSave frm = new frmSave(bmp);
frm.ShowDialog();
}
this.Opacity = 1;
}
private void cmdQuit_Click(object sender, System.EventArgs e) {
trayIcon.Visible = false;
this.Close();
}
private void Show_Main(object sender, System.EventArgs e) {
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
private void ClipperOnTop(object sender, System.EventArgs e) {
if ( trayIconMenu.MenuItems[1].Checked ) {
trayIconMenu.MenuItems[1].Checked = false;
this.TopMost = false;
} else {
trayIconMenu.MenuItems[1].Checked = true;
this.TopMost = true;
}
}
}
/// <summary>
/// Copyright Nicholas Symmonds 2002
/// This software is for instructional purposes only.
/// It may not be sold as is.
///
/// Allow the user to select a frame for the image before saving it.
/// </summary>
public class frmSave : System.Windows.Forms.Form {
private PictureBox m_Pic;
private Bitmap m_bmp;
private Bitmap m_OriginalBmp;
private PrintPreviewDialog Pv;
private PageSetupDialog Ps;
private PrintDocument Pd;
private PrintDialog Pr;
private Font FooterFont = new Font("Arial", 8);
private int PrintCount = 0;
private System.Windows.Forms.Panel P1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem mnuFile;
private System.Windows.Forms.MenuItem mnuSave;
private System.Windows.Forms.MenuItem mnuPrint;
private System.Windows.Forms.MenuItem mnuClose;
private System.Windows.Forms.MenuItem mnuPrintPreview;
private System.Windows.Forms.MenuItem mnuPrintNow;
private System.Windows.Forms.MenuItem NoMenu;
private System.Windows.Forms.MenuItem mnuAttr;
private System.Windows.Forms.MenuItem mnuBorder;
private System.ComponentModel.Container components = null;
public frmSave(Bitmap bmp) {
InitializeComponent();
m_bmp = (Bitmap)bmp.Clone();
m_OriginalBmp = (Bitmap)bmp.Clone();
P1.BackgroundImage = GetPanelImage();
P1.Dock = DockStyle.Fill;
m_Pic = new PictureBox();
m_Pic.BorderStyle = BorderStyle.None;
m_Pic.SizeMode = PictureBoxSizeMode.AutoSize;
m_Pic.Image = m_bmp;
P1.Controls.Add(m_Pic);
P1.Controls[0].Location = new Point(1, 1);
//Set up the prnting
Pv = new PrintPreviewDialog();
Ps = new PageSetupDialog();
Pr = new PrintDialog();
Pd = new PrintDocument();
Pd.DocumentName = "ScreenShot";
Pv.Document = Pd;
Ps.Document = Pd;
Pr.Document = Pd;
Pd.BeginPrint += new PrintEventHandler(this.pd_BeginPrint);
Pd.PrintPage += new PrintPageEventHandler(this.pd_Print);
}
protected override void Dispose( bool disposing ) {
if( disposing ) {
if(components != null) {
components.Dispose();
}
P1.Dispose();
}
base.Dispose( disposing );
}
#region Windows Form 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() {
this.P1 = new System.Windows.Forms.Panel();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuSave = new System.Windows.Forms.MenuItem();
this.mnuClose = new System.Windows.Forms.MenuItem();
this.NoMenu = new System.Windows.Forms.MenuItem();
this.mnuAttr = new System.Windows.Forms.MenuItem();
this.mnuBorder = new System.Windows.Forms.MenuItem();
this.mnuPrint = new System.Windows.Forms.MenuItem();
this.mnuPrintPreview = new System.Windows.Forms.MenuItem();
this.mnuPrintNow = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// P1
//
this.P1.AutoScroll = true;
this.P1.BackColor = System.Drawing.SystemColors.Control;
this.P1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.P1.Location = new System.Drawing.Point(8, 16);
this.P1.Name = "P1";
this.P1.Size = new System.Drawing.Size(768, 520);
this.P1.TabIndex = 0;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFile,
this.NoMenu,
this.mnuPrint});
//
// mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuSave,
this.mnuClose});
this.mnuFile.Text = "&File";
//
// mnuSave
//
this.mnuSave.Index = 0;
this.mnuSave.Text = "&Save";
this.mnuSave.Click += new System.EventHandler(this.mnuSave_Click);
//
// mnuClose
//
this.mnuClose.Index = 1;
this.mnuClose.Text = "&Close";
this.mnuClose.Click += new System.EventHandler(this.mnuClose_Click);
//
// NoMenu
//
this.NoMenu.Index = 1;
this.NoMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuAttr,
this.mnuBorder});
this.NoMenu.Text = "&Attributes";
this.NoMenu.Click += new System.EventHandler(this.mnuAttr_Click);
//
// mnuAttr
//
this.mnuAttr.Index = 0;
this.mnuAttr.Text = "Resolution";
this.mnuAttr.Click += new System.EventHandler(this.mnuAttr_Click);
//
// mnuBorder
//
this.mnuBorder.Index = 1;
this.mnuBorder.Text = "Border";
this.mnuBorder.Click += new System.EventHandler(this.mnuBorder_Click);
//
// mnuPrint
//
this.mnuPrint.Index = 2;
this.mnuPrint.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuPrintPreview,
this.mnuPrintNow});
this.mnuPrint.Text = "&Print";
//
// mnuPrintPreview
//
this.mnuPrintPreview.Index = 0;
this.mnuPrintPreview.Text = "Pre&view";
this.mnuPrintPreview.Click += new System.EventHandler(this.mnuPrintPreview_Click);
//
// mnuPrintNow
//
this.mnuPrintNow.Index = 1;
this.mnuPrintNow.Text = "&Print";
this.mnuPrintNow.Click += new System.EventHandler(this.mnuPrintNow_Click);
//
// frmSave
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(792, 553);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.P1});
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Name = "frmSave";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Save Image";
this.Load += new System.EventHandler(this.frmSave_Load);
this.ResumeLayout(false);
}
#endregion
private void frmSave_Load(object sender, System.EventArgs e) {
}
/// <summary>
/// This routine makes a blank image and colors it with a hatch brush
/// This image is handed back to the caller who then uses this image as
/// the background image for the panel. The panel will tile this image
/// for as many times as it takes to fill the panel.
/// </summary>
private Image GetPanelImage() {
Image i = new Bitmap(50, 50);
using(Graphics G = Graphics.FromImage(i)) {
//No need for high quality here. We need Speed!!!
G.SmoothingMode = SmoothingMode.HighSpeed;
Brush B = new HatchBrush(HatchStyle.Cross, Color.Cyan, Color.LightCyan);
G.FillRectangle(B, 0, 0, i.Width, i.Height);
}
return i;
}
private void mnuAttr_Click(object sender, System.EventArgs e) {
Attributes frm = new Attributes(m_bmp.HorizontalResolution, m_bmp.Size);
frm.ShowDialog();
m_bmp.SetResolution(frm.SaveRes, frm.SaveRes);
}
private void mnuClose_Click(object sender, System.EventArgs e) {
this.Close();
}
private void mnuSave_Click(object sender, System.EventArgs e) {
SaveFileDialog sd = new SaveFileDialog();
sd.Filter = "Bitmap (*.bmp)|*.bmp|" +
"JPEG (*.jpg)|*.jpg|" +
"GIF (*.Gif)|*.gif|" +
"TIFF (*.tif)|*.tif|" +
"PNG (*.png)|*.png|" +
"EMF (*.emf)|*.emf" ;
sd.FilterIndex = 1 ;
sd.RestoreDirectory = true ;
sd.AddExtension = true;
if(sd.ShowDialog() == DialogResult.OK) {
if (sd.FileName.Length != 0) {
switch(sd.FilterIndex) {
case 1:
//Save as bitmap
m_bmp.Save(sd.FileName, ImageFormat.Bmp);
break;
case 2:
//Save as JPEG
m_bmp.Save(sd.FileName, ImageFormat.Jpeg);
break;
case 3:
//Save as GIF
m_bmp.Save(sd.FileName, ImageFormat.Gif);
break;
case 4:
//Save as TIFF
m_bmp.Save(sd.FileName, ImageFormat.Tiff);
break;
case 5:
//Save as PNG
m_bmp.Save(sd.FileName, ImageFormat.Png);
break;
case 6:
//Save as EMF
m_bmp.Save(sd.FileName, ImageFormat.Emf);
break;
default:
break;
}
}
}
}
#region Printer routines
private void pd_BeginPrint ( object sender, PrintEventArgs e) {
Pd.DocumentName = "ScreenShot " + (++PrintCount).ToString();
}
private void pd_Print(object sender, PrintPageEventArgs e) {
Graphics G = e.Graphics;
float LeftMargin = e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
float BottomMargin = e.MarginBounds.Bottom;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Center;
Rectangle Border = e.MarginBounds;
Border.Inflate(1, 1);
RectangleF Footer = new Rectangle(e.MarginBounds.Left,
e.MarginBounds.Bottom,
e.MarginBounds.Width,
e.PageBounds.Bottom -
e.MarginBounds.Bottom);
//Type in the footer
G.DrawString(Pd.DocumentName, FooterFont, Brushes.Black, Footer, sf);
sf.Alignment = StringAlignment.Near;
G.DrawString(DateTime.Now.ToLongDateString(), FooterFont,
Brushes.Black,
Footer, sf);
//Draw the rectangle and the image. Image is stretched to fit!!!
G.DrawRectangle(Pens.Black, Border);
G.DrawImage(m_bmp, e.MarginBounds);
sf.Dispose();
}
private void mnuPrintPreview_Click(object sender, System.EventArgs e) {
Pv.WindowState = FormWindowState.Maximized;
Pv.ShowDialog();
}
private void mnuPrintNow_Click(object sender, System.EventArgs e) {
if (Pr.ShowDialog() == DialogResult.OK)
Pd.Print();
}
#endregion
private void mnuBor
|