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!