asp.net upload pictures at the same time generate high-definition thumbnail _ practical skills

Source: Internet
Author: User

In asp.net, it is often useful to upload image functions or to generate thumbnails. Baidu or Google,c# method is also a lot of, but a use but found that the thumbnail is not clear ah, thumbnail pictures are too big and so on, the following is my code in the processing of pictures, the effect is good, so take out to share, (effect can achieve some of the effect of drawing software)

The code is as follows:

 <summary>///asp.net upload pictures and generate thumbnails///</summary>///<param name= "Upimage" >htmlinputfile controls < /param>///<param name= "Ssavepath" > Saved paths, some folders that are relative to the server path </param>///<param name= "Sthumbextension" "> Thumbnail thumb</param>///<param name=" intthumbwidth "> Generate thumbnail width </param>///<param name=" Intthumbheight > Generate thumbnail height </param>///<returns> thumbnail name </returns> public string Uploadimage ( HtmlInputFile upimage, String Ssavepath, string sthumbextension, int intthumbwidth, int intthumbheight) {string Sthu
   Mbfile = "";  
   String sfilename = "";
    if (upimage.postedfile!= null) {Httppostedfile myFile = upimage.postedfile;
    int nfilelen = Myfile.contentlength;   
    if (Nfilelen = 0) return "did not choose to upload pictures"; Gets the extension string extendname = System.IO.Path.GetExtension (myfile.filename) of the upimage selection file.
    ToLower (); Determine if the picture format if (extendname!= ". jpg" && extendname!= ". Jpge" &&Amp
    
    Extendname!= ". gif" && extendname!= ". bmp" && extendname!= ". png") return "picture format is not correct";
    byte[] MyData = new Byte[nfilelen];
    MyFile.InputStream.Read (myData, 0, Nfilelen);
    sFileName = System.IO.Path.GetFileName (myfile.filename);
    int file_append = 0; Check if there is a picture under the current folder with the same name, and then in the file name +1 while (System.IO.File.Exists System.Web.HttpContext.Current.Server.MapPath (Ssavepath +
     sFileName))) {file_append++; sFileName = System.IO.Path.GetFileNameWithoutExtension (myfile.filename) + file_append.
    ToString () + extendname;  } System.IO.FileStream newFile = new System.IO.FileStream (System.Web.HttpContext.Current.Server.MapPath (Ssavepath
    + sfilename), System.IO.FileMode.Create, System.IO.FileAccess.Write);
    Newfile.write (myData, 0, mydata.length);
    Newfile.close (); Above for uploading the original try {//original image loaded using (System.Drawing.Image sourceimage = System.Drawing.Image.FromFile (system.we B.httpcontext.current.server.MapPath (Ssavepath + sfilename)) {//original width and height int width = sourceimage.width;
      int height = sourceimage.height;
      int smallwidth;
      int smallheight; Gets the size of the first drawing, (comparing the width/thumbnail of the original image and the height/thumbnail of the original image) if (((decimal) width)/height <= ((decimal) intthumbwidth)/Intthumbhe
       ight) {smallwidth = Intthumbwidth;
      Smallheight = Intthumbwidth * height/width;
       else {smallwidth = Intthumbheight * width/height;
      Smallheight = Intthumbheight;
      //Determine if the thumbnail exists under the current folder with the name file File_append = 0;
      Sthumbfile = sthumbextension + System.IO.Path.GetFileNameWithoutExtension (myfile.filename) + extendname; while (System.IO.File.Exists (System.Web.HttpContext.Current.Server.MapPath (Ssavepath + sthumbfile))) {File_a
       ppend++; Sthumbfile = sthumbextension + System.IO.Path.GetFileNameWithoutExtension (myfile.filename) + file_append.
      ToString () + extendname; }//thumbnail savedAbsolute path String smallimagepath = System.Web.HttpContext.Current.Server.MapPath (ssavepath) + sthumbfile;
      Creates a new storyboard with a minimum proportional compression size to draw the original using (System.Drawing.Image bitmap = new System.Drawing.Bitmap (Smallwidth, Smallheight))
        {//Draw the middle graph using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (bitmap)) {
        HD, smooth g.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
        G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        G.clear (Color.Black); G.drawimage (Sourceimage, New System.Drawing.Rectangle (0, 0, Smallwidth, smallheight), new System.
       Drawing.rectangle (0, 0, width, height), System.Drawing.GraphicsUnit.Pixel); //Create a new storyboard, drawing the intermediate graph using the thumbnail size (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap (Intthumbwidth, Intthu Mbheight)) {//Draw thumbnail http://www.cnblogs.com/sosoft/using (System.Drawing.GrAphics g = System.Drawing.Graphics.FromImage (Bitmap1)) {//HD, smooth G.interpolationmode = System.dr Awing.
         Drawing2D.InterpolationMode.High;
         G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
         G.clear (Color.Black);
         int lwidth = (smallwidth-intthumbwidth)/2;
         int bheight = (smallheight-intthumbheight)/2; G.drawimage (Bitmap, new Rectangle (0, 0, Intthumbwidth, intthumbheight), Lwidth, Bheight, Intthumbwidth, Intthumbheight,
         GraphicsUnit.Pixel);
         G.dispose (); Bitmap1.
        Save (Smallimagepath, System.Drawing.Imaging.ImageFormat.Jpeg); A catch {//error Deletes the System.IO.File.Delete (System.Web.HttpContext.Current.Ser Ver.
     MapPath (Ssavepath + sfilename));
    Return "picture format is not correct";
   //Returns the thumbnail name return sthumbfile;
  Return "no pictures selected"; }

HtmlInputFile control I think we all should know, is the input type=file ....

Here's the call code together C.

 <%@ Page language= "C #" autoeventwireup= "true" codefile= "Default2.aspx.cs" inherits= "Default2"%> <!
 DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  
 

 protected void Button1_Click (object sender, EventArgs e)
  {
   string a = this. Uploadimage (this. File1, "upload/", "Thumb_", 118, 118); 
  

This will be in your upload folder more than two pictures, one is the original image, one is a thumbnail.

Provides a better algorithm, because there is no time to test and debug, for reference only

That is, in the first step, such as the proportion of narrowing, can be divided into several times, that is, the original image to the above code in the middle of the figure to reduce the percentage,

For example: The original image for 500*500 I want to abbreviated to 100*80, the above code program will first draw a 100*100 in the middle of the picture, and then on the image of the 100*80, in the drawing of the 100*100 in the middle of the map, if you first draw the middle of the 300*300, and then in the 300* 300 on the basis of painting 100*100 and then painting 100*80 This will be better than the code above me, the picture clearer, that is, the more the middle of the picture, the better the effect, we can go to try.

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.