Control study -> 图像格式转换 如:将*.jpg -> *.gif进行转换

来源:互联网 发布:四年级数学优化答案 编辑:程序博客网 时间:2024/05/16 16:12
2005年10月07日 21:48:00

(一).功能

一个演示图像格式转换的示例程序.
支持格式: bmp jpeg gif tiff png wmf 等之间的相互转换

(二).图片示例

(二).代码(整个后台代码)

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Imaging;
namespace 图像格式转换
{
/// >summary<
/// Form1 的摘要说明。
/// >/summary<
public class FormMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button buttonOpen;
private System.Windows.Forms.Button buttonConvert;
private System.Windows.Forms.ComboBox comboBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// >summary<
/// 必需的设计器变量。
/// >/summary<
private System.ComponentModel.Container components = null;
private System.Windows.Forms.PictureBox pictureBox;
private System.Drawing.Bitmap bitmap;

public FormMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
bitmap=null;
pictureBox.Width=panel2.Width;
pictureBox.Height=panel2.Height;
pictureBox.Top=panel1.Top;
pictureBox.Left=panel1.Left;
panel2.Resize+=new System.EventHandler(panel2_Resize);
//buttonOpen.Click+=new System.EventHandler(buttonOpen_Click);
comboBox.Items.AddRange(new object[]{
"*.jpg",
"*.bmp",
"*.gif",
"*.png",
"*.tif",
"*.wmf"
});

}

/// >summary<
/// 清理所有正在使用的资源。
/// >/summary<
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// >summary<
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// >/summary<
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.buttonConvert = new System.Windows.Forms.Button();
this.buttonOpen = new System.Windows.Forms.Button();
this.comboBox = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel2 = new System.Windows.Forms.Panel();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.buttonConvert,
this.buttonOpen,
this.comboBox,
this.label1,
this.label2});
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(128, 365);
this.panel1.TabIndex = 0;
//
// buttonConvert
//
this.buttonConvert.Location = new System.Drawing.Point(40, 264);
this.buttonConvert.Name = "buttonConvert";
this.buttonConvert.Size = new System.Drawing.Size(56, 24);
this.buttonConvert.TabIndex = 1;
this.buttonConvert.Text = "转换";
buttonConvert.Click+=new System.EventHandler(buttonConvert_Click);
//
// buttonOpen
//
this.buttonOpen.Location = new System.Drawing.Point(32, 56);
this.buttonOpen.Name = "buttonOpen";
this.buttonOpen.Size = new System.Drawing.Size(64, 24);
this.buttonOpen.TabIndex = 0;
this.buttonOpen.Text = "打开文件";
this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click);
//
// comboBox
//
this.comboBox.Location = new System.Drawing.Point(64, 224);
this.comboBox.Name = "comboBox";
this.comboBox.Size = new System.Drawing.Size(48, 20);
this.comboBox.TabIndex = 0;
this.comboBox.Text = "comboBox1";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 104);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 40);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 224);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 16);
this.label2.TabIndex = 0;
this.label2.Text = "转换为";
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(128, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(3, 365);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pictureBox});
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(131, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(373, 365);
this.panel2.TabIndex = 2;
//
// pictureBox
//
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(369, 361);
this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
// FormMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 365);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel2,
this.splitter1,
this.panel1});
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FormMain";
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// >summary<
/// 应用程序的主入口点。
/// >/summary<
[STAThread]
static void Main()
{
Application.Run(new FormMain());
}

private void buttonOpen_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog=new OpenFileDialog();
openFileDialog.Filter="*.jpg,*.jpeg,*.bmp,*.gif,*.ico,*.png,*.tif,*.wmf|*.jpg;*.jpeg;*.bmp;*.gif;*.ico;*.png;*.tif;*.wmf";
openFileDialog.Title="打开图像文件";
openFileDialog.Multiselect=false;
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
if(bitmap!=null)
{
bitmap.Dispose();
}
string fileName=openFileDialog.FileName;
bitmap=new Bitmap(fileName);
if(bitmap.Width {
pictureBox.Width=panel2.Width;
pictureBox.Height=(int)((double)bitmap.Height*panel2.Width/bitmap.Width);
}
else
{
pictureBox.Height=panel2.Height;
pictureBox.Width=(int)((double)bitmap.Width*panel2.Height/bitmap.Height);
}
pictureBox.Image=bitmap;
FileInfo f=new FileInfo(fileName);
this.Text="图象转换:"+f.Name;
this.label1.Text=f.Name;
buttonConvert.Enabled=true;
}
}
private void panel2_Resize(object sender, System.EventArgs e)
{
pictureBox.Top=panel1.Top;
pictureBox.Left=panel1.Left;
if(bitmap!=null)
{
if(bitmap.Width {
pictureBox.Width=panel2.Width;
pictureBox.Height=(int)((double) bitmap.Height*panel2.Width/bitmap.Width);
}
else
{
pictureBox.Height=panel2.Height;
pictureBox.Width=(int)((double)bitmap.Width*panel2.Height/bitmap.Height);
}
}
else
{
pictureBox.Width=panel2.Width;
pictureBox.Height=panel2.Height;
}
pictureBox.Refresh();
}
private void buttonConvert_Click(object sender, System.EventArgs e)
{
if(comboBox.SelectedItem==null)
{
return;
}
else
{
SaveFileDialog saveFileDialog=new SaveFileDialog();
saveFileDialog.Title="转化为:";
saveFileDialog.OverwritePrompt=true;
saveFileDialog.CheckPathExists=true;
saveFileDialog.Filter=comboBox.Text+"|"+comboBox.Text;
if(saveFileDialog.ShowDialog()==DialogResult.OK)
{
string fileName=saveFileDialog.FileName;
switch(comboBox.Text)
{
case "*.bmp":
bitmap.Save(fileName,ImageFormat.Bmp);
break;
case "*.jpg":
bitmap.Save(fileName,ImageFormat.Jpeg);
break;
case "*.gif":
bitmap.Save(fileName,ImageFormat.Gif);
break;
case "*.tif":
bitmap.Save(fileName,ImageFormat.Tiff);
break;
case "*.png":
bitmap.Save(fileName,ImageFormat.Png);
break;
case "*.wmf":
bitmap.Save(fileName,ImageFormat.Wmf);
break;

}
FileInfo f=new FileInfo(fileName);
this.Text="图像转换:"+f.Name;
label1.Text=f.Name;
}
}
}
}
}

(四).代码示例下载

http://www.cnblogs.com/Files/ChengKing/图像格式转换.rar



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=496731


原创粉丝点击