DotNetNukeŽ Silverlight ToolBar

//
// 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.SilverlightToolBar
{
    public partial class Edit : DotNetNuke.Entities.Modules.PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        #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(), ".png", 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 .png, .jpg, or jpeg files may be used.";
                return;
            }
 
            string strTempfilename;
            string strNewfilename;
            string path = MapPath(@"~\DesktopModules\SilverlightToolBar\images\");
            EnsureDirectory(new System.IO.DirectoryInfo(path));
            strTempfilename = Convert.ToString(ModuleId) + "_" + UniqueString() + Path.GetExtension(txtFileName.FileName).ToLower();
            strNewfilename = Convert.ToString(ModuleId) + "_" + UniqueString() + "_resize_" + Path.GetExtension(txtFileName.FileName).ToLower();
 
            // Save the file
            txtFileName.SaveAs(path + strTempfilename);
 
            MakeThumbnail(strTempfilename, strNewfilename, path);
 
            PicturesInfo objPicturesInfo = new PicturesInfo();
            objPicturesInfo.ModuleId = ModuleId;
            objPicturesInfo.Picture = strNewfilename;
            objPicturesInfo.Url = txtURL.Text;
 
            Edit_DAL.InsertPicture(objPicturesInfo);
            gvPictures.DataBind();
 
            txtURL.Text = "http://";
 
        }
 
        private static void MakeThumbnail(string strTempfilename, string strNewfilename, string path)
        {
            //  Make Thumbnail and save
            System.Drawing.Image objImage;
            System.Drawing.Image objThumbnail;
            objImage = System.Drawing.Image.FromFile(path + strTempfilename);
 
            float scale = 50.0f / System.Math.Max(objImage.Height, objImage.Width);
            objThumbnail = objImage.GetThumbnailImage((int)(objImage.Width * scale), (int)(objImage.Height * scale), null, System.IntPtr.Zero);
 
            //  Send down to client
            objThumbnail.Save(path + strNewfilename);
            //  Tidy up
            objImage.Dispose();
            objThumbnail.Dispose();
 
            // Delete temp file
            File.Delete(path + strTempfilename);
        }
 
        #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_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int PictureID = Convert.ToInt32(e.Keys["PictureID"]);
            string strPictureName = Edit_DAL.GetPictureName(PictureID);
            DeletePicture(strPictureName);
        }
 
        #endregion
 
        #region Utility
 
        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\SilverlightToolBar\images\");
 
            // Delete picture
            if (PictureName != "")
            {
                File.Delete(path + PictureName);
            }
        }
        #endregion
 
    }
}