Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Bitmap Manipulation Class With Support For Format Conversion, Bitmap Retrieval from a URL, Overlays, etc.

, 31 Aug 2003
Provides an overview and discussion of the author's BitmapManipulator C# class, including examples of each feature, as well as numerous .NET/GDI+ caveats.
bitmapmanip_src.zip
bin
Release
BitmapManip.exe
App.ico
BitmapManip.csproj.user
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace apocryph.BitmapManip
{
	/// <summary>
	/// Summary description for BitmapManipTestForm.
	/// </summary>
	public class BitmapManipTestForm : System.Windows.Forms.Form {
		private System.Windows.Forms.Label inputLbl;
		private System.Windows.Forms.TextBox inputTextBox;
		private System.Windows.Forms.Button fromFileBtn;
		private System.Windows.Forms.Button fromUrlBtn;
		private System.Windows.Forms.PictureBox imgPicBox;
		private System.Windows.Forms.Button rotate90Btn;
		private System.Windows.Forms.Button rotate180Btn;
		private System.Windows.Forms.Button rotate270;
		private System.Windows.Forms.Button scaleBtn;
		private System.Windows.Forms.TextBox scaleFactorTextBox;
		private System.Windows.Forms.Button resizeBtn;
		private System.Windows.Forms.TextBox resizeWidthTextBox;
		private System.Windows.Forms.TextBox resizeHeightTextBox;
		private System.Windows.Forms.Label xLabel;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox thumbnailHeightTextBox;
		private System.Windows.Forms.TextBox thumbnailWidthTextBox;
		private System.Windows.Forms.Button thumbnailBtn;
		private System.Windows.Forms.Button reverseBtn;
		private System.Windows.Forms.Button flipBtn;
		private System.Windows.Forms.Button cropBtn;
		private System.Windows.Forms.TextBox bottomRightYTextBox;
		private System.Windows.Forms.TextBox bottomRightXTextBox;
		private System.Windows.Forms.TextBox topLeftYTextBox;
		private System.Windows.Forms.TextBox topLeftXTextBox;
		private System.Windows.Forms.Label topLeftLbl;
		private System.Windows.Forms.Label bottomRightLbl;
		private System.Windows.Forms.Button overlayBtn;
		private System.Windows.Forms.TextBox overlayImgUrlTextBox;
		private System.Windows.Forms.Label overlayImgLbl;
		private System.Windows.Forms.Label alphaLbl;
		private System.Windows.Forms.TextBox alphaTextBox;
		private System.Windows.Forms.Label overlayPositionLbl;
		private System.Windows.Forms.ComboBox overlayPositionCombo;
		private System.Windows.Forms.Button revertBtn;
		private System.Windows.Forms.Label currentFormatLbl;
		private System.Windows.Forms.Label currentFormatValueLbl;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button jpgBtn;
		private System.Windows.Forms.Label jpgQualityLbl;
		private System.Windows.Forms.TextBox jpgQualityTextBox;
		private System.Windows.Forms.Button bmpBtn;
		private System.Windows.Forms.Button gifBtn;
		private System.Windows.Forms.Button tiffBtn;
		private System.Windows.Forms.Button pngBtn;
		private System.Windows.Forms.Label imgDimensionsLbl;

		private System.Drawing.Bitmap originalBitmap;
		private System.Drawing.Bitmap modifiedBitmap;
		private System.Drawing.Bitmap OverlayBitmap;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox tiffCompressionCombo;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
			static void Main() {
			Application.Run(new BitmapManipTestForm());
		}

		public BitmapManipTestForm() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing ) {
			if ( disposing ) {
				if (components != null) {
					components.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.inputLbl = new System.Windows.Forms.Label();
			this.inputTextBox = new System.Windows.Forms.TextBox();
			this.fromFileBtn = new System.Windows.Forms.Button();
			this.fromUrlBtn = new System.Windows.Forms.Button();
			this.imgPicBox = new System.Windows.Forms.PictureBox();
			this.rotate90Btn = new System.Windows.Forms.Button();
			this.rotate180Btn = new System.Windows.Forms.Button();
			this.rotate270 = new System.Windows.Forms.Button();
			this.scaleBtn = new System.Windows.Forms.Button();
			this.scaleFactorTextBox = new System.Windows.Forms.TextBox();
			this.resizeBtn = new System.Windows.Forms.Button();
			this.resizeWidthTextBox = new System.Windows.Forms.TextBox();
			this.resizeHeightTextBox = new System.Windows.Forms.TextBox();
			this.xLabel = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.thumbnailHeightTextBox = new System.Windows.Forms.TextBox();
			this.thumbnailWidthTextBox = new System.Windows.Forms.TextBox();
			this.thumbnailBtn = new System.Windows.Forms.Button();
			this.reverseBtn = new System.Windows.Forms.Button();
			this.flipBtn = new System.Windows.Forms.Button();
			this.cropBtn = new System.Windows.Forms.Button();
			this.bottomRightYTextBox = new System.Windows.Forms.TextBox();
			this.bottomRightXTextBox = new System.Windows.Forms.TextBox();
			this.topLeftYTextBox = new System.Windows.Forms.TextBox();
			this.topLeftXTextBox = new System.Windows.Forms.TextBox();
			this.topLeftLbl = new System.Windows.Forms.Label();
			this.bottomRightLbl = new System.Windows.Forms.Label();
			this.overlayBtn = new System.Windows.Forms.Button();
			this.overlayImgUrlTextBox = new System.Windows.Forms.TextBox();
			this.overlayImgLbl = new System.Windows.Forms.Label();
			this.alphaLbl = new System.Windows.Forms.Label();
			this.alphaTextBox = new System.Windows.Forms.TextBox();
			this.overlayPositionLbl = new System.Windows.Forms.Label();
			this.overlayPositionCombo = new System.Windows.Forms.ComboBox();
			this.revertBtn = new System.Windows.Forms.Button();
			this.currentFormatLbl = new System.Windows.Forms.Label();
			this.currentFormatValueLbl = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.jpgBtn = new System.Windows.Forms.Button();
			this.jpgQualityLbl = new System.Windows.Forms.Label();
			this.jpgQualityTextBox = new System.Windows.Forms.TextBox();
			this.bmpBtn = new System.Windows.Forms.Button();
			this.gifBtn = new System.Windows.Forms.Button();
			this.tiffBtn = new System.Windows.Forms.Button();
			this.pngBtn = new System.Windows.Forms.Button();
			this.imgDimensionsLbl = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.tiffCompressionCombo = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// inputLbl
			// 
			this.inputLbl.Location = new System.Drawing.Point(8, 8);
			this.inputLbl.Name = "inputLbl";
			this.inputLbl.TabIndex = 0;
			this.inputLbl.Text = "Input:";
			// 
			// inputTextBox
			// 
			this.inputTextBox.Location = new System.Drawing.Point(48, 5);
			this.inputTextBox.Name = "inputTextBox";
			this.inputTextBox.Size = new System.Drawing.Size(288, 20);
			this.inputTextBox.TabIndex = 1;
			this.inputTextBox.Text = "http://www.ex-astris-scientia.org/gallery/stmagazine/worf-martok1.jpg";
			// 
			// fromFileBtn
			// 
			this.fromFileBtn.Location = new System.Drawing.Point(352, 6);
			this.fromFileBtn.Name = "fromFileBtn";
			this.fromFileBtn.TabIndex = 2;
			this.fromFileBtn.Text = "From &File";
			this.fromFileBtn.Click += new System.EventHandler(this.fromFileBtn_Click);
			// 
			// fromUrlBtn
			// 
			this.fromUrlBtn.Location = new System.Drawing.Point(440, 6);
			this.fromUrlBtn.Name = "fromUrlBtn";
			this.fromUrlBtn.TabIndex = 3;
			this.fromUrlBtn.Text = "From &Url";
			this.fromUrlBtn.Click += new System.EventHandler(this.fromUrlBtn_Click);
			// 
			// imgPicBox
			// 
			this.imgPicBox.Location = new System.Drawing.Point(8, 288);
			this.imgPicBox.Name = "imgPicBox";
			this.imgPicBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
			this.imgPicBox.TabIndex = 4;
			this.imgPicBox.TabStop = false;
			// 
			// rotate90Btn
			// 
			this.rotate90Btn.Location = new System.Drawing.Point(8, 40);
			this.rotate90Btn.Name = "rotate90Btn";
			this.rotate90Btn.TabIndex = 5;
			this.rotate90Btn.Text = "Rotate 90";
			this.rotate90Btn.Click += new System.EventHandler(this.rotate90Btn_Click);
			// 
			// rotate180Btn
			// 
			this.rotate180Btn.Location = new System.Drawing.Point(8, 72);
			this.rotate180Btn.Name = "rotate180Btn";
			this.rotate180Btn.TabIndex = 6;
			this.rotate180Btn.Text = "Rotate 180";
			this.rotate180Btn.Click += new System.EventHandler(this.rotate180Btn_Click);
			// 
			// rotate270
			// 
			this.rotate270.Location = new System.Drawing.Point(8, 104);
			this.rotate270.Name = "rotate270";
			this.rotate270.TabIndex = 7;
			this.rotate270.Text = "Rotate 270";
			this.rotate270.Click += new System.EventHandler(this.rotate270_Click);
			// 
			// scaleBtn
			// 
			this.scaleBtn.Location = new System.Drawing.Point(96, 40);
			this.scaleBtn.Name = "scaleBtn";
			this.scaleBtn.TabIndex = 8;
			this.scaleBtn.Text = "Scale By:";
			this.scaleBtn.Click += new System.EventHandler(this.scaleBtn_Click);
			// 
			// scaleFactorTextBox
			// 
			this.scaleFactorTextBox.Location = new System.Drawing.Point(176, 40);
			this.scaleFactorTextBox.Name = "scaleFactorTextBox";
			this.scaleFactorTextBox.TabIndex = 9;
			this.scaleFactorTextBox.Text = "1.0";
			// 
			// resizeBtn
			// 
			this.resizeBtn.Location = new System.Drawing.Point(96, 72);
			this.resizeBtn.Name = "resizeBtn";
			this.resizeBtn.TabIndex = 10;
			this.resizeBtn.Text = "Resize To:";
			this.resizeBtn.Click += new System.EventHandler(this.resizeBtn_Click);
			// 
			// resizeWidthTextBox
			// 
			this.resizeWidthTextBox.Location = new System.Drawing.Point(176, 72);
			this.resizeWidthTextBox.Name = "resizeWidthTextBox";
			this.resizeWidthTextBox.Size = new System.Drawing.Size(40, 20);
			this.resizeWidthTextBox.TabIndex = 11;
			this.resizeWidthTextBox.Text = "0";
			// 
			// resizeHeightTextBox
			// 
			this.resizeHeightTextBox.Location = new System.Drawing.Point(232, 72);
			this.resizeHeightTextBox.Name = "resizeHeightTextBox";
			this.resizeHeightTextBox.Size = new System.Drawing.Size(40, 20);
			this.resizeHeightTextBox.TabIndex = 12;
			this.resizeHeightTextBox.Text = "0";
			// 
			// xLabel
			// 
			this.xLabel.Location = new System.Drawing.Point(216, 75);
			this.xLabel.Name = "xLabel";
			this.xLabel.Size = new System.Drawing.Size(16, 23);
			this.xLabel.TabIndex = 13;
			this.xLabel.Text = "x";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(216, 104);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(16, 23);
			this.label1.TabIndex = 17;
			this.label1.Text = "x";
			// 
			// thumbnailHeightTextBox
			// 
			this.thumbnailHeightTextBox.Location = new System.Drawing.Point(232, 104);
			this.thumbnailHeightTextBox.Name = "thumbnailHeightTextBox";
			this.thumbnailHeightTextBox.Size = new System.Drawing.Size(40, 20);
			this.thumbnailHeightTextBox.TabIndex = 16;
			this.thumbnailHeightTextBox.Text = "50";
			// 
			// thumbnailWidthTextBox
			// 
			this.thumbnailWidthTextBox.Location = new System.Drawing.Point(176, 104);
			this.thumbnailWidthTextBox.Name = "thumbnailWidthTextBox";
			this.thumbnailWidthTextBox.Size = new System.Drawing.Size(40, 20);
			this.thumbnailWidthTextBox.TabIndex = 15;
			this.thumbnailWidthTextBox.Text = "50";
			// 
			// thumbnailBtn
			// 
			this.thumbnailBtn.Location = new System.Drawing.Point(96, 104);
			this.thumbnailBtn.Name = "thumbnailBtn";
			this.thumbnailBtn.TabIndex = 14;
			this.thumbnailBtn.Text = "Thumbnail:";
			this.thumbnailBtn.Click += new System.EventHandler(this.thumbnailBtn_Click);
			// 
			// reverseBtn
			// 
			this.reverseBtn.Location = new System.Drawing.Point(288, 40);
			this.reverseBtn.Name = "reverseBtn";
			this.reverseBtn.TabIndex = 18;
			this.reverseBtn.Text = "Reverse";
			this.reverseBtn.Click += new System.EventHandler(this.reverseBtn_Click);
			// 
			// flipBtn
			// 
			this.flipBtn.Location = new System.Drawing.Point(376, 40);
			this.flipBtn.Name = "flipBtn";
			this.flipBtn.TabIndex = 19;
			this.flipBtn.Text = "Flip";
			this.flipBtn.Click += new System.EventHandler(this.flipBtn_Click);
			// 
			// cropBtn
			// 
			this.cropBtn.Location = new System.Drawing.Point(288, 72);
			this.cropBtn.Name = "cropBtn";
			this.cropBtn.TabIndex = 20;
			this.cropBtn.Text = "Crop:";
			this.cropBtn.Click += new System.EventHandler(this.cropBtn_Click);
			// 
			// bottomRightYTextBox
			// 
			this.bottomRightYTextBox.Location = new System.Drawing.Point(512, 104);
			this.bottomRightYTextBox.Name = "bottomRightYTextBox";
			this.bottomRightYTextBox.Size = new System.Drawing.Size(40, 20);
			this.bottomRightYTextBox.TabIndex = 25;
			this.bottomRightYTextBox.Text = "50";
			// 
			// bottomRightXTextBox
			// 
			this.bottomRightXTextBox.Location = new System.Drawing.Point(456, 104);
			this.bottomRightXTextBox.Name = "bottomRightXTextBox";
			this.bottomRightXTextBox.Size = new System.Drawing.Size(40, 20);
			this.bottomRightXTextBox.TabIndex = 24;
			this.bottomRightXTextBox.Text = "50";
			// 
			// topLeftYTextBox
			// 
			this.topLeftYTextBox.Location = new System.Drawing.Point(512, 72);
			this.topLeftYTextBox.Name = "topLeftYTextBox";
			this.topLeftYTextBox.Size = new System.Drawing.Size(40, 20);
			this.topLeftYTextBox.TabIndex = 22;
			this.topLeftYTextBox.Text = "0";
			// 
			// topLeftXTextBox
			// 
			this.topLeftXTextBox.Location = new System.Drawing.Point(456, 72);
			this.topLeftXTextBox.Name = "topLeftXTextBox";
			this.topLeftXTextBox.Size = new System.Drawing.Size(40, 20);
			this.topLeftXTextBox.TabIndex = 21;
			this.topLeftXTextBox.Text = "0";
			// 
			// topLeftLbl
			// 
			this.topLeftLbl.Location = new System.Drawing.Point(368, 80);
			this.topLeftLbl.Name = "topLeftLbl";
			this.topLeftLbl.Size = new System.Drawing.Size(56, 23);
			this.topLeftLbl.TabIndex = 28;
			this.topLeftLbl.Text = "Top Left:";
			// 
			// bottomRightLbl
			// 
			this.bottomRightLbl.Location = new System.Drawing.Point(368, 104);
			this.bottomRightLbl.Name = "bottomRightLbl";
			this.bottomRightLbl.Size = new System.Drawing.Size(80, 23);
			this.bottomRightLbl.TabIndex = 29;
			this.bottomRightLbl.Text = "Bottom Right:";
			// 
			// overlayBtn
			// 
			this.overlayBtn.Location = new System.Drawing.Point(8, 144);
			this.overlayBtn.Name = "overlayBtn";
			this.overlayBtn.TabIndex = 30;
			this.overlayBtn.Text = "Overlay:";
			this.overlayBtn.Click += new System.EventHandler(this.overlayBtn_Click);
			// 
			// overlayImgUrlTextBox
			// 
			this.overlayImgUrlTextBox.Location = new System.Drawing.Point(200, 144);
			this.overlayImgUrlTextBox.Name = "overlayImgUrlTextBox";
			this.overlayImgUrlTextBox.Size = new System.Drawing.Size(352, 20);
			this.overlayImgUrlTextBox.TabIndex = 31;
			this.overlayImgUrlTextBox.Text = "http://www.codeproject.com/images/codeproject125x125.gif";
			// 
			// overlayImgLbl
			// 
			this.overlayImgLbl.Location = new System.Drawing.Point(96, 146);
			this.overlayImgLbl.Name = "overlayImgLbl";
			this.overlayImgLbl.TabIndex = 32;
			this.overlayImgLbl.Text = "Overlay Image:";
			// 
			// alphaLbl
			// 
			this.alphaLbl.Location = new System.Drawing.Point(96, 168);
			this.alphaLbl.Name = "alphaLbl";
			this.alphaLbl.TabIndex = 33;
			this.alphaLbl.Text = "Alpha (0-100)";
			// 
			// alphaTextBox
			// 
			this.alphaTextBox.Location = new System.Drawing.Point(200, 168);
			this.alphaTextBox.Name = "alphaTextBox";
			this.alphaTextBox.Size = new System.Drawing.Size(24, 20);
			this.alphaTextBox.TabIndex = 34;
			this.alphaTextBox.Text = "45";
			// 
			// overlayPositionLbl
			// 
			this.overlayPositionLbl.Location = new System.Drawing.Point(312, 168);
			this.overlayPositionLbl.Name = "overlayPositionLbl";
			this.overlayPositionLbl.TabIndex = 35;
			this.overlayPositionLbl.Text = "Position:";
			// 
			// overlayPositionCombo
			// 
			this.overlayPositionCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.overlayPositionCombo.Items.AddRange(new object[] {
																	  "TopLeft",
																	  "TopRight",
																	  "BottomRight",
																	  "BottomLeft",
																	  "Center"});
			this.overlayPositionCombo.Location = new System.Drawing.Point(368, 168);
			this.overlayPositionCombo.Name = "overlayPositionCombo";
			this.overlayPositionCombo.Size = new System.Drawing.Size(121, 21);
			this.overlayPositionCombo.TabIndex = 36;
			// 
			// revertBtn
			// 
			this.revertBtn.Location = new System.Drawing.Point(464, 40);
			this.revertBtn.Name = "revertBtn";
			this.revertBtn.TabIndex = 37;
			this.revertBtn.Text = "Revert";
			this.revertBtn.Click += new System.EventHandler(this.revertBtn_Click);
			// 
			// currentFormatLbl
			// 
			this.currentFormatLbl.Location = new System.Drawing.Point(8, 200);
			this.currentFormatLbl.Name = "currentFormatLbl";
			this.currentFormatLbl.TabIndex = 38;
			this.currentFormatLbl.Text = "Current Format:";
			// 
			// currentFormatValueLbl
			// 
			this.currentFormatValueLbl.Location = new System.Drawing.Point(96, 200);
			this.currentFormatValueLbl.Name = "currentFormatValueLbl";
			this.currentFormatValueLbl.TabIndex = 39;
			this.currentFormatValueLbl.Text = "foo";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(13, 224);
			this.label2.Name = "label2";
			this.label2.TabIndex = 40;
			this.label2.Text = "Convert To:";
			// 
			// jpgBtn
			// 
			this.jpgBtn.Location = new System.Drawing.Point(96, 224);
			this.jpgBtn.Name = "jpgBtn";
			this.jpgBtn.Size = new System.Drawing.Size(48, 23);
			this.jpgBtn.TabIndex = 41;
			this.jpgBtn.Text = "JPEG";
			this.jpgBtn.Click += new System.EventHandler(this.jpgBtn_Click);
			// 
			// jpgQualityLbl
			// 
			this.jpgQualityLbl.Location = new System.Drawing.Point(152, 226);
			this.jpgQualityLbl.Name = "jpgQualityLbl";
			this.jpgQualityLbl.TabIndex = 42;
			this.jpgQualityLbl.Text = "Q=";
			// 
			// jpgQualityTextBox
			// 
			this.jpgQualityTextBox.Location = new System.Drawing.Point(175, 225);
			this.jpgQualityTextBox.Name = "jpgQualityTextBox";
			this.jpgQualityTextBox.Size = new System.Drawing.Size(25, 20);
			this.jpgQualityTextBox.TabIndex = 43;
			this.jpgQualityTextBox.Text = "75";
			// 
			// bmpBtn
			// 
			this.bmpBtn.Location = new System.Drawing.Point(160, 256);
			this.bmpBtn.Name = "bmpBtn";
			this.bmpBtn.Size = new System.Drawing.Size(48, 23);
			this.bmpBtn.TabIndex = 47;
			this.bmpBtn.Text = "BMP";
			this.bmpBtn.Click += new System.EventHandler(this.bmpBtn_Click);
			// 
			// gifBtn
			// 
			this.gifBtn.Location = new System.Drawing.Point(96, 256);
			this.gifBtn.Name = "gifBtn";
			this.gifBtn.Size = new System.Drawing.Size(48, 23);
			this.gifBtn.TabIndex = 50;
			this.gifBtn.Text = "GIF";
			this.gifBtn.Click += new System.EventHandler(this.gifBtn_Click);
			// 
			// tiffBtn
			// 
			this.tiffBtn.Location = new System.Drawing.Point(224, 224);
			this.tiffBtn.Name = "tiffBtn";
			this.tiffBtn.Size = new System.Drawing.Size(48, 23);
			this.tiffBtn.TabIndex = 51;
			this.tiffBtn.Text = "TIFF";
			this.tiffBtn.Click += new System.EventHandler(this.tiffBtn_Click);
			// 
			// pngBtn
			// 
			this.pngBtn.Location = new System.Drawing.Point(224, 256);
			this.pngBtn.Name = "pngBtn";
			this.pngBtn.Size = new System.Drawing.Size(48, 23);
			this.pngBtn.TabIndex = 52;
			this.pngBtn.Text = "PNG";
			this.pngBtn.Click += new System.EventHandler(this.pngBtn_Click);
			// 
			// imgDimensionsLbl
			// 
			this.imgDimensionsLbl.Location = new System.Drawing.Point(336, 200);
			this.imgDimensionsLbl.Name = "imgDimensionsLbl";
			this.imgDimensionsLbl.TabIndex = 54;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(280, 224);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(80, 23);
			this.label3.TabIndex = 56;
			this.label3.Text = "compression=";
			// 
			// tiffCompressionCombo
			// 
			this.tiffCompressionCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.tiffCompressionCombo.Items.AddRange(new object[] {
																	  "CCITT3",
																	  "CCITT4",
																	  "LZW",
																	  "RLE",
																	  "None",
																	  "Unspecified"});
			this.tiffCompressionCombo.Location = new System.Drawing.Point(360, 224);
			this.tiffCompressionCombo.Name = "tiffCompressionCombo";
			this.tiffCompressionCombo.Size = new System.Drawing.Size(168, 21);
			this.tiffCompressionCombo.TabIndex = 57;
			// 
			// BitmapManipTestForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(568, 341);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tiffCompressionCombo,
																		  this.label3,
																		  this.imgDimensionsLbl,
																		  this.pngBtn,
																		  this.tiffBtn,
																		  this.gifBtn,
																		  this.bmpBtn,
																		  this.jpgQualityTextBox,
																		  this.jpgQualityLbl,
																		  this.jpgBtn,
																		  this.label2,
																		  this.currentFormatValueLbl,
																		  this.currentFormatLbl,
																		  this.revertBtn,
																		  this.overlayPositionCombo,
																		  this.overlayPositionLbl,
																		  this.alphaTextBox,
																		  this.alphaLbl,
																		  this.overlayImgLbl,
																		  this.overlayImgUrlTextBox,
																		  this.overlayBtn,
																		  this.bottomRightLbl,
																		  this.topLeftLbl,
																		  this.bottomRightYTextBox,
																		  this.bottomRightXTextBox,
																		  this.topLeftYTextBox,
																		  this.topLeftXTextBox,
																		  this.cropBtn,
																		  this.flipBtn,
																		  this.reverseBtn,
																		  this.label1,
																		  this.thumbnailHeightTextBox,
																		  this.thumbnailWidthTextBox,
																		  this.thumbnailBtn,
																		  this.xLabel,
																		  this.resizeHeightTextBox,
																		  this.resizeWidthTextBox,
																		  this.resizeBtn,
																		  this.scaleFactorTextBox,
																		  this.scaleBtn,
																		  this.rotate270,
																		  this.rotate180Btn,
																		  this.rotate90Btn,
																		  this.imgPicBox,
																		  this.fromUrlBtn,
																		  this.fromFileBtn,
																		  this.inputTextBox,
																		  this.inputLbl});
			this.Name = "BitmapManipTestForm";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.Text = "Adam Nelson\'s Bitmap Manipulation Class Tester";
			this.ResumeLayout(false);

		}
#endregion

		private void Redraw() {
			//Called whenever the user applies an additional modification to the bitmap.
			Bitmap workingBitmap = this.modifiedBitmap;

			if (this.OverlayBitmap != null) {
				//Apply overlay
				int alpha;
				BitmapManipulator.ImageCornerEnum corner = BitmapManipulator.ImageCornerEnum.BottomRight;

				alpha = Int32.Parse(this.alphaTextBox.Text);
				corner = (BitmapManipulator.ImageCornerEnum)Enum.Parse(corner.GetType(), 
																	   (String)this.overlayPositionCombo.SelectedItem);

				workingBitmap = BitmapManipulator.OverlayBitmap(workingBitmap,
																this.OverlayBitmap,
																alpha,
																corner);
			}

			this.imgPicBox.Image = workingBitmap;
			this.imgDimensionsLbl.Text = String.Format("{0:d} x {1:d} pixels",
													   workingBitmap.Size.Width,
													   workingBitmap.Size.Height);
			this.currentFormatValueLbl.Text = BitmapManipulator.MimeTypeFromImageFormat(workingBitmap.RawFormat);
			
			//Grow the form if the whole picture isn't visible
			if (this.Width < this.imgPicBox.Location.X + this.imgPicBox.Width + 25) {
				this.Width = this.imgPicBox.Location.X + this.imgPicBox.Width + 25;
			}
			if (this.Height < this.imgPicBox.Location.Y + this.imgPicBox.Height + 25) {
				this.Height = this.imgPicBox.Location.Y + this.imgPicBox.Height + 25;
			}
		}

		private void fromFileBtn_Click(object sender, System.EventArgs e) {
			//Load image from file
			try {
				this.originalBitmap = new Bitmap(this.inputTextBox.Text);

				this.modifiedBitmap = (Bitmap)this.originalBitmap.Clone();
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error loading file.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void fromUrlBtn_Click(object sender, System.EventArgs e) {
			//Load image from URL
			try {
				this.originalBitmap = BitmapManipulator.GetBitmapFromUri(this.inputTextBox.Text);

				this.modifiedBitmap = (Bitmap)this.originalBitmap.Clone();
				Redraw();
			} catch (BitmapManipulator.BitmapManipException exp) {
				MessageBox.Show(String.Format("Error loading file.\nInner Exception: {0}\nError Message:{1}", 
											  exp.InnerException.GetType().Name,
											  exp.Message),
								"Error");
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error loading file.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void rotate90Btn_Click(object sender, System.EventArgs e) {
			this.modifiedBitmap = BitmapManipulator.RotateBitmapRight90(this.modifiedBitmap);
			Redraw();
		}

		private void rotate180Btn_Click(object sender, System.EventArgs e) {
			this.modifiedBitmap = BitmapManipulator.RotateBitmapRight180(this.modifiedBitmap);
			Redraw();

		}

		private void rotate270_Click(object sender, System.EventArgs e) {
			this.modifiedBitmap = BitmapManipulator.RotateBitmapRight270(this.modifiedBitmap);
			Redraw();
		}

		private void scaleBtn_Click(object sender, System.EventArgs e) {
			try {
				this.modifiedBitmap = BitmapManipulator.ScaleBitmap(this.modifiedBitmap, 
																	Double.Parse(this.scaleFactorTextBox.Text));
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void resizeBtn_Click(object sender, System.EventArgs e) {
			try {
				this.modifiedBitmap = BitmapManipulator.ResizeBitmap(this.modifiedBitmap, 
																	 Int32.Parse(this.resizeWidthTextBox.Text),
																	 Int32.Parse(this.resizeHeightTextBox.Text));
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void thumbnailBtn_Click(object sender, System.EventArgs e) {
			try {
				this.modifiedBitmap = BitmapManipulator.ThumbnailBitmap(this.modifiedBitmap, 
																		Int32.Parse(this.thumbnailWidthTextBox.Text),
																		Int32.Parse(this.thumbnailHeightTextBox.Text));
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void reverseBtn_Click(object sender, System.EventArgs e) {
			this.modifiedBitmap = BitmapManipulator.ReverseBitmap(this.modifiedBitmap);
			Redraw();
		}

		private void flipBtn_Click(object sender, System.EventArgs e) {
			this.modifiedBitmap = BitmapManipulator.FlipBitmap(this.modifiedBitmap);
			Redraw();
		}

		private void cropBtn_Click(object sender, System.EventArgs e) {
			try {
				int topx, topy, bottomx, bottomy;
				
				topx = Int32.Parse(this.topLeftXTextBox.Text);
				topy = Int32.Parse(this.topLeftYTextBox.Text);
				bottomx = Int32.Parse(this.bottomRightXTextBox.Text);
				bottomy = Int32.Parse(this.bottomRightYTextBox.Text);
				
				this.modifiedBitmap = BitmapManipulator.CropBitmap(this.modifiedBitmap, 
																   new Rectangle(topx, topy,
																				 bottomx-topx, bottomy-topy));
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void overlayBtn_Click(object sender, System.EventArgs e) {
			try {
				this.OverlayBitmap = BitmapManipulator.GetBitmapFromUri(this.overlayImgUrlTextBox.Text);

				Redraw();
			} catch (BitmapManipulator.BitmapManipException exp) {
				MessageBox.Show(String.Format("Error loading overlay file.\nInner Exception: {0}\nError Message:{1}", 
											  exp.InnerException.GetType().Name,
											  exp.Message),
								"Error");
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error loading overlay file.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void revertBtn_Click(object sender, System.EventArgs e) {
			this.modifiedBitmap = (Bitmap)this.originalBitmap.Clone();
			this.OverlayBitmap = null;
			Redraw();
		}

		private void jpgBtn_Click(object sender, System.EventArgs e) {
			try {
				this.modifiedBitmap = BitmapManipulator.ConvertBitmapToJpeg(this.modifiedBitmap, 
																	Int32.Parse(this.jpgQualityTextBox.Text));
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void bmpBtn_Click(object sender, System.EventArgs e) {
			try {
				this.modifiedBitmap = BitmapManipulator.ConvertBitmap(this.modifiedBitmap, 
																	  ImageFormat.Bmp);
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void gifBtn_Click(object sender, System.EventArgs e) {
			try {
				this.modifiedBitmap = BitmapManipulator.ConvertBitmap(this.modifiedBitmap, 
																	  ImageFormat.Gif);
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void tiffBtn_Click(object sender, System.EventArgs e) {
			try {
				BitmapManipulator.TiffCompressionEnum compress = BitmapManipulator.TiffCompressionEnum.None;

				compress = (BitmapManipulator.TiffCompressionEnum)Enum.Parse(compress.GetType(), 
																	   (String)this.tiffCompressionCombo.SelectedItem);

				this.modifiedBitmap = BitmapManipulator.ConvertBitmapToTiff(this.modifiedBitmap, 
																			compress);
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}

		private void pngBtn_Click(object sender, System.EventArgs e) {
			try {
				this.modifiedBitmap = BitmapManipulator.ConvertBitmap(this.modifiedBitmap, 
																	  ImageFormat.Png);
				Redraw();
			} catch (Exception exp) {
				MessageBox.Show(String.Format("Error performing this operation.\nException: {0}\nText:{1}", 
											  exp.GetType().Name,
											  exp.Message),
								"Error");
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

Adam Nelson
Web Developer
United States United States
My name is Adam Nelson. I've been a professional programmer since 1996, working on everything from database development, early first-generation web applications, modern n-tier distributed apps, high-performance wireless security tools, to my last job as a Senior Consultant at BearingPoint posted in Baghdad, Iraq training Iraqi developers in the wonders of C# and ASP.NET. I am currently an Engineering Director at Dell.
 
I have a wide range of skills and interests, including cryptography, image processing, computational linguistics, military history, 3D graphics, database optimization, and mathematics, to name a few.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150301.1 | Last Updated 1 Sep 2003
Article Copyright 2003 by Adam Nelson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid