protected void btnUploadAccommImage_Click(object sender, EventArgs e) { object sesobj = Session["imageuploadpreview"]; Session.Remove("imageuploadpreview"); Validate("uploadimage"); if (IsValid && sesobj != null) { byte[] buffer = (byte[])sesobj; MemoryStream ms = new MemoryStream(buffer); System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(ms); var cropinfo = (from p in hfCrop.Value.Split(',') select int.Parse(p)).ToArray(); bmp = bmp.Clone(new System.Drawing.Rectangle(cropinfo[0], cropinfo[1], cropinfo[2], cropinfo[3]), bmp.PixelFormat); ImageResizer resizer = new ImageResizer(); resizer.OutputFormat = ImageFormat.Jpeg; resizer.ImgQuality = IMAGE_QUALITY; resizer.MaxHeight = IMAGE_MAX_H; resizer.MaxWidth = IMAGE_MAX_W; byte[] imgRes = resizer.Resize(bmp); bmp.Dispose(); string title = tbxAddAccomImageTitle.Text.Trim(); string description = tbxAddAccomImageDescription.Text.Trim(); string fileName = String.Format("accomm_{0}_image_{1}.jpg", accommodation.Name.Replace(' ', '_').Replace('+','-'), "{0}"); string filePath = ACCOMIMAGE_UPLOAD_PATH.Last() == '/' ? ACCOMIMAGE_UPLOAD_PATH : ACCOMIMAGE_UPLOAD_PATH + "/"; string srcLocation = filePath + fileName; UltimateDC.Image image = new UltimateDC.Image() { Src = "", Alt = "" }; dc.Images.InsertOnSubmit(image); //Nakon submitchanges se pojavi id u image, a to mi treba za filename i src atribut dc.SubmitChanges(); srcLocation = string.Format(srcLocation, image.Id); image.Src = string.Format(srcLocation, image.Id); image.Alt = accommodation.Name + " " + title; image.Title = title; image.Description = description; AccommodationImage accommImage = new AccommodationImage(); dc.AccommodationImages.InsertOnSubmit(accommImage); accommImage.Image = image; accommImage.Accommodation = accommodation; dc.SubmitChanges(); string saveLocation = Server.MapPath(srcLocation); if (!System.IO.Directory.Exists(saveLocation)) System.IO.Directory.CreateDirectory(Path.GetDirectoryName(saveLocation)); File.WriteAllBytes(saveLocation, imgRes); imgThumb.Visible = false; RefreshImages(); } }
private void UploadAndSetLogo(UltimateDC.Agency agency) { if (fuLogo.HasFile) { HttpPostedFile file = fuLogo.PostedFile; if (Common.IsImage(file.ContentType)) { string fileName = String.Format("logo_{0}_{1}{2}", agency.Name.Replace(' ', '_').Replace('+','-'), agency.Id, Path.GetExtension(file.FileName)); string filePath = LOGO_UPLOAD_PATH.Last() == '/' ? LOGO_UPLOAD_PATH : LOGO_UPLOAD_PATH + "/"; string srcLocation = filePath + fileName; string saveLocation = Server.MapPath(srcLocation); ImageResizer resizer = new ImageResizer(); resizer.OutputFormat = ImageFormat.Jpeg; resizer.ImgQuality = IMAGE_QUALITY; resizer.MaxHeight = IMAGE_MAX_H; resizer.MaxWidth = IMAGE_MAX_W; byte[] imgRes = resizer.Resize(file); if (!System.IO.Directory.Exists(saveLocation)) System.IO.Directory.CreateDirectory(Path.GetDirectoryName(saveLocation)); File.WriteAllBytes(saveLocation, imgRes); UltimateDC.Image logo; if (agency.Image == null) { logo = new UltimateDC.Image(); dc.Images.InsertOnSubmit(logo); } else { logo = agency.Image; string oldSaveLocation = Server.MapPath(logo.Src); if (oldSaveLocation != saveLocation && File.Exists(oldSaveLocation)) File.Delete(oldSaveLocation); } logo.Src = srcLocation; logo.Alt = String.Format("{0} logo", agency.Name); agency.Image = logo; } } }