按照指定大小,生成缩略图
2024-07-21 02:17:07
供稿:网友
/*
说明:按照指定大小,生成缩略图
作者: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;
}
}
}