private string SaveSitePartImage(HttpPostedFileBase picture) { var filename = ""; if (picture != null && picture.ContentLength > 0) { ImageFormat format = null; var ext = new GetFileExtension().GetExtension(picture.FileName); filename = DateTime.Now.ToString("ddMMyyhhmmss") + "." + ext; switch (ext) { case "png": { format = ImageFormat.Png; break; } default: { format = ImageFormat.Jpeg; break; } } var path = Server.MapPath("~/Images/Banner"); int width = 1172, height = 355; int x = 0, y = 0; var source = new Bitmap(picture.InputStream); if ((source.Width / source.Height) > (width / height)) { var newW = (width * source.Height) / height; var mod = (source.Width - newW) / 2; mod = mod < 0 ? 0 : mod; x = mod; } else { var newH = (height * source.Width) / width; var mod = (source.Height - newH) / 2; mod = mod < 0 ? 0 : mod; y = mod; } var cropped = source.Clone(new Rectangle(x, y, source.Width - (x * 2), source.Height - (y * 2)), source.PixelFormat); Bitmap target = null; if (cropped.Width > width) { target = new Bitmap(width, height); } else { target = new Bitmap(cropped.Width, cropped.Height); } using (var graphics = Graphics.FromImage(target)) graphics.DrawImage(cropped, 0, 0, target.Width, target.Height); target.Save($"{path}\\Large\\{filename}", format); target = new Bitmap(50, 50); using (var graphics = Graphics.FromImage(target)) graphics.DrawImage(cropped, 0, 0, target.Width, target.Height); target.Save($"{path}\\Small\\{filename}", format); } return filename; }
public ActionResult EditGallery(validationEditGalleryTab validationEditGalleryTab, string alts) { if (IsValidSessions()) { // string filename = ""; bool isok = true; GetFileExtension Ext = new GetFileExtension(); ServiceTab ServiceTab = _RService.DetailsServiceTab(validationEditGalleryTab.id); if (ServiceTab.ServiceTabFile != null) { var altsArray = string.IsNullOrEmpty(alts) ? new string[0] { } : alts.Split(new string[] { "|" }, StringSplitOptions.None); foreach (var item in ServiceTab.ServiceTabFile) { if (altsArray.Length > 0) { foreach (var i in altsArray) { var j = i.Split(new string[] { ":" }, StringSplitOptions.None); if (int.Parse(j[0]) == item.Id) { item.Alt = j[1]; } } } } } if (isok) { ServiceTab.Name = validationEditGalleryTab.TabName; ServiceTab.ModifiedDate = DateTime.Now.Date; _RService.SaveServiceTab(ServiceTab); TempData["result"] = "OK"; TempData["Message"] = "عملیات با موفقیت انجام شد."; return RedirectToAction("ProductList"); } ViewBag.ServiceTabFileList = EService.GetServiceTabFileAdmin(0, 5, validationEditGalleryTab.id).ToList(); TempData["Count"] = EService.GetCountServiceTabFileAdmin(validationEditGalleryTab.id); ViewBag.PageNumber = 1; return View(); } else return RedirectToAction("Login", "Home"); }
//writtenBy Azizi 95_03_12 private string SaveImage(System.Web.HttpPostedFileBase Picture) { string filename = ""; GetFileExtension Ext = new GetFileExtension(); if (Ext.GetExtension(Picture.FileName) == "jpg" || Ext.GetExtension(Picture.FileName) == "png" || Ext.GetExtension(Picture.FileName) == "jpeg" || Ext.GetExtension(Picture.FileName) == "png") { filename = DateTime.Now.ToString("ddMMyyhhmmss") + Session["admin"].ToString() + "." + Ext.GetExtension(Picture.FileName); string filePath = Path.Combine( Server.MapPath("~/Images/TabGalleryService"), Path.GetFileName(filename) ); Picture.SaveAs(filePath); WebImage img = new WebImage(Picture.InputStream); img.Resize(750, 464, false, false); img.Save(filePath); System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath("~/Images/TabGalleryService/" + filename)); System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = (ThumbnailCallback); int height = fullSizeImg.Height; int width = fullSizeImg.Width; if (height > width) { height = (height * 190) / width; width = 190; } else { width = (width * 190) / height; height = 190; } System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(width, height, dummyCallBack, IntPtr.Zero); thumbNailImg.Save(Server.MapPath("~/Images/TabGalleryService/thum/") + filename); ImageResizer.ImageBuilder.Current.Build(Server.MapPath("~/Images/TabGalleryService/thum/") + filename, Server.MapPath("~/Images/TabGalleryService/thum/") + filename, new ImageResizer.ResizeSettings(190, 190, ImageResizer.FitMode.Crop, "")); thumbNailImg.Dispose(); fullSizeImg.Dispose(); } return filename; }
private string SaveFlash(HttpPostedFileBase Picture, string Image) { string filename1 = ""; GetFileExtension Ext = new GetFileExtension(); if (Ext.GetExtension(Picture.FileName) == "swf") { if (Image != null) { System.IO.File.Delete(Server.MapPath("~/Files/FlashService/" + Image)); } DateTime Dt = DateTime.Now.Date; filename1 = DateTime.Now.Ticks + Dt.Year + Dt.Month + Dt.Day + "." + Ext.GetExtension(Picture.FileName); string filePath = Path.Combine( Server.MapPath("~/Files/FlashService"), Path.GetFileName(filename1) ); Picture.SaveAs(filePath); } return filename1; }
public ActionResult UploadFiles(SelectedFileModel elem) { if (IsValidSessions()) { var fileName = elem.fileName; GetFileExtension Ext = new GetFileExtension(); if (Ext.GetExtension(fileName) == "jpg" || Ext.GetExtension(fileName) == "png" || Ext.GetExtension(fileName) == "jpeg" || Ext.GetExtension(fileName) == "png") { Tabs Tab = (Tabs)TempData["Tabs"]; int value1 = Tab.GroupId; string TabName = Tab.TabName; int ServiceTabId = Tab.TabId; if (_RService.DetailsServiceTab(ServiceTabId) == null) { ServiceTab ServiceTab = new ServiceTab() { CreationDate = DateTime.Now.Date, Name = TabName, ServiceGroupId = Convert.ToInt32(value1), TabType = 2 }; _RService.SaveServiceTab(ServiceTab); ServiceTabId = ServiceTab.Id; Tab.TabId = ServiceTab.Id; } TempData["Tabs"] = Tab; var fileContent = elem.fileContent; var category = elem.category; var title = elem.title; var base64String = fileContent.Split(',')[1]; fileName = DateTime.Now.Ticks + "_" + category + "_" + title + "_" + CommonMethods.ChangeUnKnownCharacters(Path.GetFileNameWithoutExtension(fileName)) + "." + Ext.GetExtension(fileName); ServiceTabFile ServiceTabFile = new ServiceTabFile { File = fileName, ServiceTabId = ServiceTabId }; _RService.SaveServiceTabFile(ServiceTabFile); var filePath = Server.MapPath("~/Images/TabGalleryService/") + fileName; var bytes = Convert.FromBase64String(base64String); System.IO.File.WriteAllBytes(filePath, bytes); ImageResizer.ImageBuilder.Current.Build(Server.MapPath("~/Images/TabGalleryService/") + fileName, Server.MapPath("~/Images/TabGalleryService/thum/") + fileName, new ImageResizer.ResizeSettings(200, 200, ImageResizer.FitMode.Crop, "")); #region Comments //ImageResizer.ImageBuilder.Current.Build(Server.MapPath("~/Images/TabGalleryService/") + fileName, // Server.MapPath("~/Images/TabGalleryService/") + fileName, // new ImageResizer.ResizeSettings(400, 400, ImageResizer.FitMode.Stretch, "")); //System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath("~/Images/TabGalleryService/" + fileName)); //System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = (ThumbnailCallback); //int height = fullSizeImg.Height; //int width = fullSizeImg.Width; //if (height > width) //{ // height = (height * 200) / width; // width = 200; //} //else //{ // width = (width * 200) / height; // height = 200; //} //System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(width, height, dummyCallBack, IntPtr.Zero); //thumbNailImg.Save(Server.MapPath("~/Images/TabGalleryService/thum/") + fileName); //ImageResizer.ImageBuilder.Current.Build(Server.MapPath("~/Images/TabGalleryService/thum/") + fileName, Server.MapPath("~/Images/TabGalleryService/thum/") + fileName, new ImageResizer.ResizeSettings(200, 200, ImageResizer.FitMode.Crop, "")); //thumbNailImg.Dispose(); //fullSizeImg.Dispose(); #endregion TempData["result"] = "OK"; TempData["Message"] = "عملیات با موفقیت ثبت شد."; return Json(fileName, JsonRequestBehavior.AllowGet); } return Json("error", JsonRequestBehavior.AllowGet); } return RedirectToAction("Login", "Home"); }
public ActionResult UploadFiles(SelectedFileModel selectedFile) { if (IsValidSessions()) { int LanguageId = Convert.ToInt32(Session["Language"].ToString()); var fileName = selectedFile.fileName; try { GetFileExtension Ext = new GetFileExtension(); var fileContent = selectedFile.fileContent; var category = selectedFile.category; var title = selectedFile.title; var base64String = fileContent.Split(',')[1]; fileName = DateTime.Now.Ticks + fileName; var filePath = Server.MapPath("~/Files/Upload/") + fileName; var bytes = Convert.FromBase64String(base64String); System.IO.File.WriteAllBytes(filePath, bytes); Upload Upload = new Upload(); Upload.LanguageId = LanguageId; Upload.FileName = fileName; Upload.CreationDate = DateTime.Now.Date; _RUpload.SaveUpload(Upload); } catch (Exception) { return Json("خطا در آپلود فایل"); } return Json(fileName, JsonRequestBehavior.AllowGet); } else return RedirectToAction("Login", "Home"); }