public static void AddPlacePhoto(PlacesPhoto photo) { using (var db = new EntitiesContext()) { db.PlacesPhotos.Add(photo); db.SaveChanges(); } }
protected void btnAddPlace_Click(object sender, EventArgs e) { Place place = new Place(); place.Type = ddlPlaceTypes.SelectedItem.Text; place.IdCity = Convert.ToInt32(ddlPlaceCities.SelectedItem.Value); place.Name = PlaceName.Text; place.Adress = PlaceAdress.Text; place.Telephone = PlaceTelephone.Text; place.Website = PlaceWebsite.Text; place.Rating = 1; place.RatingFloat = 1; place.AveragePrice = Convert.ToInt32(PlaceAveragePrice.Text); if (PlaceLat.Text != null) { place.Lat = PlaceLat.Text; } if (PlaceLong.Text != null) { place.Long = PlaceLong.Text; } place.OpenTime = TimeSpan.Parse(PlaceOpenTime.Text.ToString()); place.CloseTime = TimeSpan.Parse(PlaceCloseTime.Text.ToString()); place.Confirmed = true; place.IdPartner = UserId; place.Description = PlaceDescription.Text; // Check file exist or not if (PlaceUploadPhoto.PostedFile != null) { // Check the extension of image string extension = Path.GetExtension(PlaceUploadPhoto.FileName); if (extension.ToLower() == ".png" || extension.ToLower() == ".jpg") { Stream strm = PlaceUploadPhoto.PostedFile.InputStream; using (var image = System.Drawing.Image.FromStream(strm)) { // Resize image int newWidth = 1500; // New Width of Image in Pixel int newHeight = 1000; // New Height of Image in Pixel var thumbImg = new Bitmap(newWidth, newHeight); var thumbGraph = Graphics.FromImage(thumbImg); thumbGraph.CompositingQuality = CompositingQuality.HighQuality; thumbGraph.SmoothingMode = SmoothingMode.HighQuality; thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; var imgRectangle = new Rectangle(0, 0, newWidth, newHeight); thumbGraph.DrawImage(image, imgRectangle); // Save the file string filename = place.Name + Guid.NewGuid().ToString() + extension; string targetPath = Server.MapPath(@"~\Content\Images\Places\" + filename); place.Photo = "~/Content/Images/Places/" + filename; thumbImg.Save(targetPath, image.RawFormat); } } } PlacesManager.AddPlace(place); if (PlaceUploadPhotos.PostedFiles != null) { foreach (var file in PlaceUploadPhotos.PostedFiles) { PlacesPhoto photo = new PlacesPhoto(); photo.IdPlace = PlacesManager.GetLastIdPlaceForUserId(UserId); photo.IdUser = UserId; // Check the extension of image string extension = Path.GetExtension(file.FileName); photo.Extension = extension; photo.UploadDate = DateTime.Now; if (extension.ToLower() == ".png" || extension.ToLower() == ".jpg") { Stream strm = file.InputStream; using (var image = System.Drawing.Image.FromStream(strm)) { // Resize image int newWidth = 730; // New Width of Image in Pixel int newHeight = 346; // New Height of Image in Pixel var thumbImg = new Bitmap(newWidth, newHeight); var thumbGraph = Graphics.FromImage(thumbImg); thumbGraph.CompositingQuality = CompositingQuality.HighQuality; thumbGraph.SmoothingMode = SmoothingMode.HighQuality; thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; var imgRectangle = new Rectangle(0, 0, newWidth, newHeight); thumbGraph.DrawImage(image, imgRectangle); // Save the file string filename = place.Name + Guid.NewGuid().ToString() + extension; photo.Name = filename; string targetPath = Server.MapPath(@"~\Content\Images\PlacesPhotos\" + filename); photo.Path = "~/Content/Images/PlacesPhotos/" + filename; thumbImg.Save(targetPath, image.RawFormat); } } PlacesManager.AddPlacePhoto(photo); } } Response.Redirect("~/Pages/ShowPlaces.aspx?cityId=" + place.IdCity); }