Generate thumbnails according to the specified size

xiaoxiao2021-03-06  114

/ * Note: In accordance with the specified size, generate thumbnails of: Blue.Dream Disclaimer: I recently discovered that many website quoted the article, saying that the author information are omitted, please reference the author of this site do not omit author of * / using system.drawing; using system.drawing.imaging; using system.collections; using system.bomponentmodel; using system.data; using system.io;

A summary description of Namespace Test {///

/// Form1. /// public class form1: system.windows.Forms.form {// Declare the number of bits of the random number generated by the encrypted character Generated Public const length = 32; // Declaration of the separation length before and after the encrypted character 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; /// /// required design Multiple variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent (); this.init ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

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 (rot); 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";} ///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#Region Windows Form Designer Generated Code ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method.

/// 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 = "source map"; /// ivsource // 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 = "result map";

// // 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 = "source image"; 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 = "generated"; 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 = "- Please select -"; // // 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 = "size"; 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.group, this.groupbox2}; this.name = "Form1" This.Text = "Form1"; this.groupBox1.ResumeLayout (false); this.groupBox2.ResumeLayout (false); this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [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) {intate = (int) this.local.selectedValue; point p = new point (340, 270); switch (select) {case 1: px = 800; py = 600; Break; case 2: px = 640; py = 480; Break; case 3: px = 480; py = 360; Break; case 4: py = 340; px = 270; break; case 5: px = 240; py = 180; Break; Case 6: PX = 120; py = 90; Break;}

// object address generating FileInfo objFile = new FileInfo (this.SourceFile); string filepath = objFile.DirectoryName; string filename = objFile.Name; string saveFile = filepath "// Thumb" filename; if (MakeThumbnailImage (this. Sourcefile, Savefile, PX, Py, Objfile.extension.tolower ()) This.Imgobject.Image = New Bitmap (Savefile); Else MessageBox.show ("Fails");}

///

///> Follow the specified size to generate a thumbnail /// /// Source file /// Storage File /// The width of the thumbnail /// thumbnail /// file type /// private bool MakeThumbnailImage (string sourceFile, string saveFile, int width, int height, string strType) {if (! File.Exists (sourceFile )) // determines whether a file exists 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; // Take thumbnail Try {Objimage.Save (Savefile, getimageType (strty));} 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; casser ".emf ": Return imageformat.emf; default: return imageformat.jpeg;}} private bool thumbnailcallback () {returnaf false;}}}

转载请注明原文地址:https://www.9cbs.com/read-102044.html

New Post(0)