public void SaveLightboxImage(LightboxImage lightboxImageToSave)
 {
     try
     {
         ISession currentSession = this._sessionManager.OpenSession();
         currentSession.SaveOrUpdate(lightboxImageToSave);
         currentSession.Flush();
     }
     catch (Exception x)
     {
         throw new Exception("Could not save the 'LightboxImage object: " + x.Message);
     }
 }
 public void DeleteLightboxImage(LightboxImage lightboxImageToDelete)
 {
     try
     {
         ISession currentSession = this._sessionManager.OpenSession();
         currentSession.Delete(lightboxImageToDelete);
         currentSession.Flush();
     }
     catch (Exception x)
     {
         throw new Exception("Could not delete the 'LightboxImage object: " + x.Message);
     }
 }
        protected void btnLightBoxImage_Click(object sender, EventArgs e)
        {
            lblMessages.Text = "";
            if (ImageFileUpload.HasFile)
            {

                string extension = System.IO.Path.GetExtension(ImageFileUpload.FileName);
                if (extension == ".jpg" || extension == ".gif" || extension == ".png")
                {
                    LightboxImage l = new LightboxImage();

                    string fileName = ImageFileUpload.FileName;
                    string fileRoot = HttpContext.Current.Server.MapPath("~/UserFiles/CuyaLightBox/" + this.Section.Node.Id.ToString() + "/");

                    if (!Directory.Exists(fileRoot))
                    {
                        Directory.CreateDirectory(fileRoot);
                    }

                    string fileImagePath = Path.Combine(fileRoot, fileName);
                    ImageFileUpload.SaveAs(fileImagePath);

                    l.Filename = "thumb_" + fileName;
                    l.Title = TextBoxTitle.Text;
                    l.AltText = TextBoxAltText.Text;

                    int lbid = Convert.ToInt32(this._Module.Section.Settings["LIGHTBOX_NAME"]);
                    l.LightBoxId = this._Module._cuyaLightBoxDao.GetLightBox(lbid);

                    Bitmap bm = new Bitmap(ImageFileUpload.PostedFile.InputStream);

                    //To do: Handle the sizes of thumbnails from a new module setting 

                    //int newWidth = 80;
                    //int newHeight = bm.Height * newWidth / bm.Width;

                    //Height uniformity is tidy. Widths can vary.
                    int newHeight = 80;
                    int newWidth = bm.Width * newHeight / bm.Height;

                    Bitmap tn = bm.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero) as Bitmap;

                    string fileThumbImagePath = Path.Combine(fileRoot, "thumb_" + fileName);

                    l.Width = tn.Width;
                    l.Height = tn.Height;
                    l.Created = DateTime.Now;
                    l.Active = cbxImageActive.Checked;

                    this._Module._cuyaLightBoxDao.SaveLightboxImage(l);

                    tn.Save(fileThumbImagePath, ImageFormat.Jpeg);
                    bm.Dispose();
                    tn.Dispose();

                    DatabindLightBoxImages(Convert.ToInt32(this._Module.Section.Settings["LIGHTBOX_NAME"].ToString()));
                    //Clear Textboxes
                    TextBoxAltText.Text = "";
                    TextBoxTitle.Text = "";
                }
                else
                {
                    lblMessages.Text = "Only .jpg, .gif and .png image file formats allowed";
                }
            }
        }