public static string UploadImage(string path, string filename) { var context = HttpContext.Current; string strReturn = string.Empty; strReturn = "no_images.jpg"; try { var file = context.Request.Files[0]; if (file != null && file.ContentLength > 0) { if (!Directory.Exists(context.Server.MapPath(path))) { Directory.CreateDirectory(context.Server.MapPath(path)); } string fileType = Path.GetExtension(file.FileName); if (ValidateExtension(fileType) == true) { string date = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); string folder = context.Server.MapPath(path); string targetPath = Path.Combine(folder, filename + "-" + date + fileType); file.SaveAs(targetPath); strReturn = filename + "-" + date + fileType; } } } catch (Exception ex) { WriteLogs.WriteToLogFile(ex); } return(strReturn); }
public static List <string> UploadImages(string path, string filename) { var httpContext = HttpContext.Current; try { List <string> lst = new List <string>(); if (httpContext.Request.Files.Count > 0) { for (int i = 0; i < httpContext.Request.Files.Count; i++) { var file = httpContext.Request.Files[i]; string imgReturn = ""; if (file.ContentLength > 0) { if (!Directory.Exists(httpContext.Server.MapPath(path))) { Directory.CreateDirectory(httpContext.Server.MapPath(path)); } string fileType = Path.GetExtension(file.FileName); string fileSave = filename + "-0" + i; if (ValidateExtension(fileType) == true) { string date = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); string folder = httpContext.Server.MapPath(path); string targetPath = Path.Combine(folder, fileSave + "-" + date + fileType); file.SaveAs(targetPath); imgReturn = fileSave + "-" + date + fileType; lst.Add(imgReturn); } } } } return(lst); } catch (Exception ex) { WriteLogs.WriteToLogFile(ex); return(null); } }
public static bool SaveResizeImage(Image img, int width, string path) { try { //Lấy chiều cao ban đầu của hình ảnh int originalW = img.Width; int originalH = img.Height; //kích thước resize //int resizeW = width; int resizedH = (originalH * width) / originalW; Bitmap b = new Bitmap(width, resizedH); Graphics g = Graphics.FromImage((Image)b); g.InterpolationMode = InterpolationMode.Bicubic; // Specify here g.DrawImage(img, 0, 0, width, resizedH); g.Dispose(); b.Save(path); return(true); } catch (Exception ex) { WriteLogs.WriteToLogFile(ex); return(false); } }