asp.net (C #) Compressed pictures, you can specify the picture template high-width _ practical skills

Source: Internet
Author: User
Copy Code code as follows:

To generate a thumbnail function
Sequential parameters: Source diagram file flow, thumbnail store address, template width, template high
Note: Thumbnail size is controlled within the stencil area
public static void Makesmallimg (System.IO.Stream fromfilestream, String filesaveurl, System.Double templatewidth, System.Double templateheight)
{
Get a Picture object from a file and use the color management information embedded in the stream
System.Drawing.Image myimage = System.Drawing.Image.FromStream (Fromfilestream, true);
Thumbnail width, height
System.Double newwidth = myimage.width, newheight = myimage.height;
Wide in the template of the horizontal map
if (Myimage.width > Myimage.height | | | myimage.width = = myimage.height)
{
if (Myimage.width > Templatewidth)
{
Wide press template, high scaling
Newwidth = Templatewidth;
Newheight = Myimage.height * (newwidth/myimage.width);
}
}
Taller than the template of the vertical diagram
Else
{
if (Myimage.height > Templateheight)
{
High press stencil, wide scaling
Newheight = Templateheight;
Newwidth = Myimage.width * (newheight/myimage.height);
}
}
Get picture size
System.Drawing.Size mysize = new Size (int) newwidth, (int) newheight);
Create a new BMP picture
System.Drawing.Image bitmap = new System.Drawing.Bitmap (mysize.width, mysize.height);
Create a new artboard
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Set high quality interpolation method
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smooth degree
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Clear the canvas.
G.clear (Color.White);
Paint at a specified location
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 ("Song Body", 10);
System.Drawing.Brush b=new SolidBrush (color.black);
g.DrawString ("Myohmine", f,b,10,10);
G.dispose ();
Picture watermark
System.Drawing.Image copyimage = System.Drawing.Image.FromFile (System.Web.HttpContext.Current.Server.MapPath (" Pic/1.gif "));
Graphics a = graphics.fromimage (bitmap);
A.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 thumbnails
Bitmap. Save (Filesaveurl, System.Drawing.Imaging.ImageFormat.Jpeg);
G.dispose ();
Myimage.dispose ();
Bitmap. Dispose ();
}

Copy Code code as follows:

private void button2_click (object sender, EventArgs e)
{
Openfiledi Alog FileDialog = new OpenFileDialog ();
Filedialog.title = "Select Picture file";
//Filedialog.filter = "Excel Files (*.xls) |*.xls";
Filedialog.filterindex = 1;
if (filedialog.showdialog () = System.Windows.Forms.DialogResult.OK)
{

System.IO.FileStream file =sy Stem. Io. File.Open (Filedialog.filename,system.io.filemode.open);
System.IO.Stream strea = file;
file. Close ();
Makesmallimg (strea, thumbnail. jpg, 150, 150);
//file. Close ();

}
}
Related Article

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.