Several methods of C # background processing pictures _c# Tutorial

Source: Internet
Author: User
Tags httpcontext

This article describes several ways to use C # to handle pictures in the background, with the following specific code:

First: Save uploaded images directly to local

 var supportedtypes = new[] {"JPG", "JPEG", "PNG", "GIF", "BMP"};
          var fileName = system.web.httpcontext.current.request.files[0]. FileName;
          var fileext = System.IO.Path.GetExtension (fileName). Substring (1);
          if (!supportedtypes.contains (fileext))
          {return
            Json (new {msg =-1});
          }

          Random r = new Random ();
          var filename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + R.next (10000) + "." + Fileext;
          var filepath = Path.Combine (Server.MapPath ("~/avatar/temp"), filename);
          Head. SaveAs (filepath);

Second: Convert a picture to a byte type

FilePath Picture Physical Address
FileStream fs = new FileStream (FilePath, FileMode.Open);
          byte[] Bydata = new Byte[fs. Length];
          Fs. Read (bydata, 0, bydata.length);
          Fs. Close ();

Third: Convert uploaded images to byte type

 Httppostedfile file = system.web.httpcontext.current.request.files[0];

        if ((file = = null))
        {return
          Json (new {Success = false, Msg = "Upload picture failed", Path = ""});
        }
        else
        {
          System.Drawing.Image Image = System.Drawing.Image.FromStream (file. InputStream);

          MemoryStream ms = new MemoryStream ();
          Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);

          byte[] Bytedata = new Byte[ms. Length];
          Ms. Position = 0;
          Ms. Read (bytedata, 0, bytedata.length);
          Ms. Close ();
          Image. Dispose ();

         
        }
      }

The above is a small series to introduce the C # background processing pictures of several ways, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.