using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ImageContrast
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
Bitmap _image;
float _contrastFactor=1f;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// 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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(448, 280);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Open";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(448, 312);
this.button2.Name = "button2";
this.button2.TabIndex = 0;
this.button2.Text = "Save";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button3.Location = new System.Drawing.Point(448, 344);
this.button3.Name = "button3";
this.button3.TabIndex = 0;
this.button3.Text = "Done";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// trackBar1
//
this.trackBar1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.trackBar1.Location = new System.Drawing.Point(464, 88);
this.trackBar1.Maximum = 100;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Orientation = System.Windows.Forms.Orientation.Vertical;
this.trackBar1.Size = new System.Drawing.Size(45, 176);
this.trackBar1.TabIndex = 1;
this.trackBar1.TickFrequency = 10;
this.trackBar1.Value = 25;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.Location = new System.Drawing.Point(8, 8);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(432, 360);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.label1.Location = new System.Drawing.Point(456, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 23);
this.label1.TabIndex = 3;
this.label1.Text = "1";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(536, 389);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlg=new OpenFileDialog();
dlg.Filter="Image files|*.bmp; *.jpg; *.gif; *.tif; *.png";
if(dlg.ShowDialog()==DialogResult.OK)
{
_image=(Bitmap)Image.FromFile(dlg.FileName,true);
this.Text=dlg.FileName;
}
this.trackBar1.Value=25; //reset the contrast and update the image
UpdateImage();
}
private void button3_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void trackBar1_Scroll(object sender, System.EventArgs e)
{
_contrastFactor=0.04f*this.trackBar1.Value;
this.label1.Text=this._contrastFactor.ToString("0.00");
UpdateImage();
}
/// <summary>
/// Adjusts the contrast of the image by modifying the scale of the original colours using a ColorMatrix
/// </summary>
void UpdateImage()
{
if(_image==null)
return;
Image oldImage=this.pictureBox1.Image; // save the old image so we can dispose of it nicely
Bitmap bm=new Bitmap(this._image.Width, this._image.Height); //create a new image
Graphics g=Graphics.FromImage(bm); //ready to draw on it
ImageAttributes ia=new ImageAttributes();
//create the scaling matrix
ColorMatrix cm=new ColorMatrix(new float[][]{ new float[]{_contrastFactor,0f,0f,0f,0f},
new float[]{0f,_contrastFactor,0f,0f,0f},
new float[]{0f,0f,_contrastFactor,0f,0f},
new float[]{0f,0f,0f,1f,0f},
//including the BLATANT FUDGE
new float[]{0.001f,0.001f,0.001f,0f,1f}});
//use it in the image attributes
ia.SetColorMatrix(cm);
//draw the original to the temporary using the matrix
g.DrawImage(_image,new Rectangle(0,0,_image.Width,_image.Height),0,0,_image.Width,_image.Height,GraphicsUnit.Pixel,ia);
g.Dispose(); //Don't need this anymore;
ia.Dispose(); // or this
this.pictureBox1.Image=bm; //replace the picture;
if(oldImage!=null)
oldImage.Dispose(); // get rid of the old one if it exists.
}
private void button2_Click(object sender, System.EventArgs e)
{
//Saves the image in the chosen format
if(_image==null)
return;
SaveFileDialog dlg=new SaveFileDialog();
dlg.Filter="Image files|*.bmp; *.jpg; *.gif; *.tif; *.png ";
dlg.FileName=this.Text;
if(dlg.ShowDialog()==DialogResult.OK)
{
this.Text=dlg.FileName;
ImageFormat fmt=ImageFormat.Jpeg;
switch(Path.GetExtension(dlg.FileName).ToLower())
{
case ".jpg":
case ".jpeg":
fmt=ImageFormat.Jpeg;
break;
case ".bmp":
fmt=ImageFormat.Bmp;
break;
case ".png":
fmt=ImageFormat.Png;
break;
case ".tif":
case ".tiff":
fmt=ImageFormat.Tiff;
break;
case ".gif":
fmt=ImageFormat.Gif;
break;
}
//make the image in the picturebox the working image
this._image.Dispose();
this.pictureBox1.Image.Save(dlg.FileName,fmt);
this._image=(Bitmap)this.pictureBox1.Image;
}
}
}
}