DotNetNukeŽ Silverlight Album

//
// DotNetNuke - http:'www.dotnetnuke.com
// Copyright (c) 2002-2007
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
//
using System;
using System.Collections.Generic;
using System.Collections;
using System.Web.UI.WebControls;
using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Security;
using DotNetNuke.Security.Roles;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using System.IO;
 
namespace DotNetNuke.Modules.SilverlightAlbum
{
    public partial class Edit : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPictureCount();
        }
 
        #region Form Events
 
        protected void lnkBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(Globals.NavigateURL());
        }
 
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (
                string.Compare(Path.GetExtension(txtFileName.FileName).ToLower(), ".gif", true) != 0
                & string.Compare(Path.GetExtension(txtFileName.FileName).ToLower(), ".jpg", true) != 0
                & string.Compare(Path.GetExtension(txtFileName.FileName).ToLower(), ".jpeg", true) != 0
                )
            {
                lblMessage.Text =
                    "Only .gif, .jpg, or jpeg files may be used.";
                return;
            }
 
            string strfilename;
            string path = MapPath(@"~\DesktopModules\SilverlightAlbum\images\");
            EnsureDirectory(new System.IO.DirectoryInfo(path));
            strfilename = Convert.ToString(ModuleId) + "_" + UniqueString() + Path.GetExtension(txtFileName.FileName).ToLower();
            path = path + strfilename;
            txtFileName.SaveAs(path);
 
            PicturesInfo objPicturesInfo = new PicturesInfo();
            objPicturesInfo.ModuleId = ModuleId;
            objPicturesInfo.Picture = strfilename;
 
            Edit_DAL.InsertPicture(objPicturesInfo);
            gvPictures.DataBind();
            CheckPictureCount();
        }
 
        #endregion
 
        #region ObjectDataSource Events
 
        protected void ODS_Pictures_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["ModuleId"] = ModuleId.ToString();
        }
 
        #endregion
 
        #region GridView Events
 
        protected void gvPictures_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            CheckPictureCount();
        }
 
        protected void gvPictures_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int PictureID = Convert.ToInt32(e.Keys["PictureID"]);
            string strPictureName = Edit_DAL.GetPictureName(PictureID);
            DeletePicture(strPictureName);
        }
 
        protected void gvPictures_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                PicturesInfo objPicturesInfo = (PicturesInfo)e.Row.DataItem;
                if (objPicturesInfo != null)
                {
                    Image objImage = (Image)e.Row.FindControl("image1");
                    string path = MapPath(@"~\DesktopModules\SilverlightAlbum\" + objPicturesInfo.Picture);
                    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(path);
                    float scale = 150.0f / System.Math.Max(bmp.Height, bmp.Width);
                    System.Drawing.Image thumb = bmp.GetThumbnailImage(
                        (int)(bmp.Width * scale), (int)(bmp.Height * scale),
                        null, System.IntPtr.Zero);
 
                    Unit objHeightUnit = new Unit(thumb.Height);
                    Unit objWidthUnit = new Unit(thumb.Width);
 
                    objImage.Height = objHeightUnit;
                    objImage.Width = objWidthUnit;
 
                    bmp.Dispose();
                    thumb.Dispose();
                }
            }
        }
 
        #endregion
 
        #region Utility
 
        private void CheckPictureCount()
        {
            // Do not show panel if 12 images are already uploaded
            pnlUpload.Visible = (Edit_DAL.CountPictures(ModuleId) > 11) ? false : true;
            lblMaxUploaded.Visible = (Edit_DAL.CountPictures(ModuleId) > 11) ? true : false;
        }
 
        public static void EnsureDirectory(System.IO.DirectoryInfo oDirInfo)
        {
            if (oDirInfo.Parent != null)
                EnsureDirectory(oDirInfo.Parent);
            if (!oDirInfo.Exists)
            {
                oDirInfo.Create();
            }
        }
 
        public string UniqueString()
        {
            //  Create a unique file name
            DateTime myDate = DateTime.Now;
            string myTimeString = myDate.ToLongTimeString().Replace(":", "u");
            myTimeString = myTimeString.Replace(" AM", "11");
            myTimeString = myTimeString.Replace(" PM", "99");
            myTimeString = (myTimeString + Session.SessionID);
            return myTimeString;
        }
 
        private void DeletePicture(string PictureName)
        {
            //Get the file upload directory
            string path = MapPath(@"~\DesktopModules\SilverlightAlbum\images\");
 
            // Delete picture
            if (PictureName != "")
            {
                File.Delete(path + PictureName);
            }
        }
        #endregion
 
    }
}