Generate thumbnails by the specified size

Source: Internet
Author: User
Tags bool
Thumbnails/*
Description: Generates thumbnails according to the specified size
Author: 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>
Summary description of the Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
The number of digits that declare the random number generated by the cryptographic character
public const int LENGTH = 32;
To declare the length of the separation 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;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
This. Init ();

//
TODO: Add any constructor code after the 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 (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>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

#region Windows Form Designer generated code
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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 = "source Diagram";
//
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 = "Result graph";
//
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 Diagram";
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 = "Build";
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 choose-";
//
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.groupbox1,
This.groupbox2});
This. Name = "Form1";
This. Text = "Form1";
This.groupBox1.ResumeLayout (FALSE);
This.groupBox2.ResumeLayout (FALSE);
This. ResumeLayout (FALSE);

}
#endregion

<summary>
The main entry point for the application.
</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
}

Generate Destination file address
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>
Generate thumbnails by the specified size
</summary>
<param name= "sourcefile" > Source Files </param>
<param name= "SaveFile" > Storage file </param>
<param name= "width" > thumbnail widths </param>
<param name= "height" > Thumbnail Heights </param>
<param name= "strtype" > File type </param>
<returns></returns>
private bool Makethumbnailimage (string sourcefile,string savefile,int width,int height,string strtype)
{
if (! File.exists (sourcefile))//Determine if the 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); Get thumbnail image

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;
}

}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.