·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » 按照指定大小,生成缩略图

按照指定大小,生成缩略图

类别: ASP.NET教程  评论数:0 总得分:0
/*
说明:按照指定大小,生成缩略图
作者:Blue.Dream
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//声明加密字符生成的随机数的位数
public const int LENGTH = 32;
//声明加密字符前后的分隔长度
public const int LEN = 4;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button btn_Browser;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.PictureBox imgSource;
private System.Windows.Forms.PictureBox imgObject;
private string SourceFile;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox local;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.Init();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

private void Init()
{
DataTable ht = new DataTable();
DataColumn objcol = new DataColumn("Keys");
objcol.DataType = typeof(System.String);
ht.Columns.Add(objcol);
objcol = new DataColumn("Value");
objcol.DataType = typeof(System.Int32);
ht.Columns.Add(objcol);
DataRow row = ht.NewRow();
row[0] = "800*600";
row[1] = 1;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "640*480";
row[1] = 2;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "420*360";
row[1] = 3;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "340*270";
row[1] = 4;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "240*180";
row[1] = 5;
ht.Rows.Add(row);
row = ht.NewRow();
row[0] = "120*90";
row[1] = 6;
ht.Rows.Add(row);


this.local.DataSource = ht;
this.local.DisplayMember = "Keys";
this.local.ValueMember = "Value";
}

/// <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.groupBox1 = new System.Windows.Forms.GroupBox();
this.imgSource = new System.Windows.Forms.PictureBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.imgObject = new System.Windows.Forms.PictureBox();
this.btn_Browser = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.local = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.imgSource});
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(288, 296);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "源图";
//
// imgSource
//
this.imgSource.Location = new System.Drawing.Point(8, 16);
this.imgSource.Name = "imgSource";
this.imgSource.Size = new System.Drawing.Size(272, 272);
this.imgSource.TabIndex = 0;
this.imgSource.TabStop = false;
//
// groupBox2
//
this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.imgObject});
this.groupBox2.Location = new System.Drawing.Point(304, 0);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(352, 296);
this.groupBox2.TabIndex = 0;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "结果图";
//
// imgObject
//
this.imgObject.Location = new System.Drawing.Point(8, 16);
this.imgObject.Name = "imgObject";
this.imgObject.Size = new System.Drawing.Size(336, 272);
this.imgObject.TabIndex = 0;
this.imgObject.TabStop = false;
//
// btn_Browser
//
this.btn_Browser.Location = new System.Drawing.Point(24, 312);
this.btn_Browser.Name = "btn_Browser";
this.btn_Browser.TabIndex = 1;
this.btn_Browser.Text = "源 图";
this.btn_Browser.Click += new System.EventHandler(this.btn_Browser_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(552, 312);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "生 成";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// local
//
this.local.Location = new System.Drawing.Point(256, 312);
this.local.Name = "local";
this.local.Size = new System.Drawing.Size(112, 20);
this.local.TabIndex = 4;
this.local.Text = "--请选择--";
//
// label1
//
this.label1.Location = new System.Drawing.Point(216, 312);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 23);
this.label1.TabIndex = 6;
this.label1.Text = "大小";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(664, 365);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.local,
this.button2,
this.btn_Browser,
this.groupBox1,
this.groupBox2});
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

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

private void btn_Browser_Click(object sender, System.EventArgs e)
{
using(OpenFileDialog dlg = new OpenFileDialog())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
string file = dlg.FileName;
if(File.Exists(file))
{
this.SourceFile = file;
imgSource.Image = new Bitmap(file);
}
}
dlg.Dispose();
}
}

private void button2_Click(object sender, System.EventArgs e)
{
int select = (int)this.local.SelectedValue;
Point p = new Point(340,270);
switch(select)
{
case 1:
p.X = 800;
p.Y = 600;
break;
case 2:
p.X = 640;
p.Y = 480;
break;
case 3:
p.X = 480;
p.Y = 360;
break;
case 4:
p.Y = 340;
p.X = 270;
break;
case 5:
p.X = 240;
p.Y = 180;
break;
case 6:
p.X = 120;
p.Y = 90;
break;
}

//生成目标文件地址
FileInfo objFile = new FileInfo(this.SourceFile);
string filepath = objFile.DirectoryName;
string filename = objFile.Name;
string saveFile = filepath + "//Thumb"+filename;
if(MakeThumbnailImage(this.SourceFile,saveFile,p.X,p.Y,objFile.Extension.ToLower()))
this.imgObject.Image = new Bitmap(saveFile);
else
MessageBox.Show("FAILD");

}

/// <summary>
/// 按照指定大小,生成缩略图
/// </summary>
/// <param name="sourceFile">源文件</param>
/// <param name="saveFile">存储文件</param>
/// <param name="width">缩略图的宽度</param>
/// <param name="height">缩略图的高度</param>
/// <param name="strType">文件类型</param>
/// <returns></returns>
private bool MakeThumbnailImage(string sourceFile,string saveFile,int width,int height,string strType)
{
if( !File.Exists(sourceFile) ) //判断文件是否存在
return false;
bool result = true;
Bitmap sourceImage = new Bitmap(sourceFile);
System.Drawing.Image.GetThumbnailImageAbort myCallBack = new Image.GetThumbnailImageAbort(ThumbnailCallBack);
Image objImage = sourceImage.GetThumbnailImage(width,height,myCallBack,IntPtr.Zero); //取得缩略图

try
{
objImage.Save(saveFile,GetImageType(strType));
}
catch
{
result = false;
}
finally
{
objImage.Dispose();
sourceImage.Dispose();
}
return result;

}

private System.Drawing.Imaging.ImageFormat GetImageType(string strType)
{
switch(strType)
{
case ".jpg":
return ImageFormat.Jpeg;
case ".jpeg":
return ImageFormat.Jpeg;
case ".gif":
return ImageFormat.Gif;
case ".bmp":
return ImageFormat.Bmp;
case ".tiff":
return ImageFormat.Tiff;
case ".ico":
return ImageFormat.Icon;
case ".png":
return ImageFormat.Png;
case ".emf":
return ImageFormat.Emf;
default:
return ImageFormat.Jpeg;
}
}

private bool ThumbnailCallBack()
{
return false;
}

}
}






-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1