Copy codeThe Code is as follows:
// Create a thumbnail Function
// Sequence parameters: source image file stream, thumbnail storage address, template width, and template height
// Note: the thumbnail size is controlled in the template area.
Public static void MakeSmallImg (System. IO. Stream fromFileStream, string fileSaveUrl, System. Double templateWidth, System. Double templateHeight)
{
// Obtain the image object from the file and use the color management information embedded in the stream
System. Drawing. Image myImage = System. Drawing. Image. FromStream (fromFileStream, true );
// Width and height of the thumbnail
System. Double newWidth = myImage. Width, newHeight = myImage. Height;
// The horizontal chart that is larger than the Template
If (myImage. Width> myImage. Height | myImage. Width = myImage. Height)
{
If (myImage. Width> templateWidth)
{
// Scale the width by template and the height by scale
NewWidth = templateWidth;
NewHeight = myImage. Height * (newWidth/myImage. Width );
}
}
// Portrait of a template
Else
{
If (myImage. Height> templateHeight)
{
// High by template, width by proportional Scaling
NewHeight = templateHeight;
NewWidth = myImage. Width * (newHeight/myImage. Height );
}
}
// Obtain the image size
System. Drawing. Size mySize = new Size (int) newWidth, (int) newHeight );
// Create a bmp Image
System. Drawing. Image bitmap = new System. Drawing. Bitmap (mySize. Width, mySize. Height );
// Create a canvas
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmap );
// Set a high quality Interpolation Method
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
// Set high quality and smooth Low Speed
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
// Clear the canvas
G. Clear (Color. White );
// Draw a picture at a specified position
G. DrawImage (myImage, new System. Drawing. Rectangle (0, 0, bitmap. Width, bitmap. Height ),
New System. Drawing. Rectangle (0, 0, myImage. Width, myImage. Height ),
System. Drawing. GraphicsUnit. Pixel );
/// Text watermark
// System. Drawing. Graphics G = System. Drawing. Graphics. FromImage (bitmap );
// System. Drawing. Font f = new Font ("", 10 );
// System. Drawing. Brush B = new SolidBrush (Color. Black );
// G. DrawString ("myohmine", f, B, 10, 10 );
// G. Dispose ();
/// Image Watermark
// System. Drawing. Image copyImage = System. Drawing. Image. FromFile (System. Web. HttpContext. Current. Server. MapPath ("pic/1.gif "));
// Graphics a = Graphics. FromImage (bitmap );
//. DrawImage (copyImage, new Rectangle (bitmap. width-copyImage.Width, bitmap. height-copyImage.Height, copyImage. width, copyImage. height), 0, 0, copyImage. width, copyImage. height, GraphicsUnit. pixel );
// CopyImage. Dispose ();
// A. Dispose ();
// CopyImage. Dispose ();
// Save the thumbnail
Bitmap. Save (fileSaveUrl, System. Drawing. Imaging. ImageFormat. Jpeg );
G. Dispose ();
MyImage. Dispose ();
Bitmap. Dispose ();
}
Copy codeThe Code is as follows:
Private void button2_Click (object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog ();
FileDialog. Title = "select image file ";
// FileDialog. Filter = "excel files (*. xls) | *. xls ";
FileDialog. FilterIndex = 1;
If (fileDialog. ShowDialog () = System. Windows. Forms. DialogResult. OK)
{
System. IO. FileStream file = System. IO. File. Open (fileDialog. FileName, System. IO. FileMode. Open );
System. IO. Stream strea = file;
File. Close ();
MakeSmallImg (strea, "image .jpg", 150,150 );
// File. Close ();
}
}