/*
 說明:按照指定大小,生成縮略圖
 作者: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
 {
 //聲明加密字符生成的隨機數(shù)的位數(shù)
 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>
 /// 必需的設(shè)計器變量。
 /// </summary>
 private system.componentmodel.container components = null;
 public form1()
 {
 //
 // windows 窗體設(shè)計器支持所必需的
 //
 initializecomponent();
 this.init();
 //
 // todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
 //
 }
 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>
 /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
 /// 此方法的內(nèi)容。
 /// </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 = "結(jié)果圖";
 // 
 // 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>
 /// 應(yīng)用程序的主入口點。
 /// </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; 
 }
 //生成目標(biāo)文件地址 
 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;
 }
 
 }
}