Advertisement
bjawnie

save.aspx.cs

Jun 1st, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Web;
  7. using umbraco;
  8. using umbraco.cms.businesslogic.media;
  9.  
  10. namespace Decode.Umb
  11. {
  12.   public partial class Save : System.Web.UI.Page
  13.   {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.             // if the user exited the editor close the window
  17.       if (Request.QueryString["close"] == "1") {
  18.                 closeWindow();
  19.                 return;
  20.             }
  21.            
  22.  
  23.       // get media id and media key
  24.       if (Request.QueryString["id"] == null) return;
  25.       string[] qry = Request.QueryString["id"].Split('|');
  26.      
  27.             int mediaId; if (!int.TryParse(qry[0], out mediaId)) return;
  28.             string mediaKey = qry[1];
  29.  
  30.  
  31.       // get media
  32.       Media m = new Media(mediaId);
  33.       if (m == null) return;
  34.  
  35.             // verify our hashed key matches the media's
  36.             if (Decode.Umb.Hash.GetMediaMd5Sum(m) != mediaKey) {
  37.                 handleError();
  38.                 return;
  39.             }
  40.  
  41.  
  42.       if (Request.Files.Count > 0) {
  43.         if (Request.Files["image"].ContentLength > 0) {
  44.          
  45.           // settings
  46.           HttpPostedFile file   = Request.Files["image"];
  47.           string imageType      = Request.Form["type"];
  48.                     string mediaPath            = (GlobalSettings.FullpathToRoot + Path.GetDirectoryName(m.getProperty("umbracoFile").Value.ToString()).Replace("/", "\\") + "\\").Replace("\\\\", "\\");
  49.                     string mediaVirUrl      = Path.GetDirectoryName(m.getProperty("umbracoFile").Value.ToString()).Replace("\\", "/") + "/";
  50.           string mediaName      = Path.GetFileNameWithoutExtension(m.getProperty("umbracoFile").Value.ToString()) + "." + imageType;
  51.           string savePath       = mediaPath + mediaName;
  52.           string thumbnailPath  = mediaPath + mediaName.Replace("." + imageType, "_thumb.jpg");
  53.                    
  54.  
  55.           // verify the returned file is an image file
  56.           if (imageType != "jpg" && imageType != "png" && imageType != "gif" && imageType != "bmp") {
  57.                         handleError("The selected image type can not be saved.");
  58.             return;
  59.           }
  60.  
  61.  
  62.           // save image
  63.           file.SaveAs(savePath);
  64.  
  65.           // get the saved image for thumbnail use
  66.           System.Drawing.Image image = Image.FromStream(file.InputStream);
  67.  
  68.           // create the thumbnail
  69.           generateThumbnail(image, 100, image.Width, image.Height, thumbnailPath);
  70.  
  71.           // update media data
  72.           m.getProperty("umbracoFile").Value      = mediaVirUrl + mediaName;
  73.           m.getProperty("umbracoHeight").Value    = image.Height.ToString();
  74.           m.getProperty("umbracoWidth").Value     = image.Width.ToString();
  75.           m.getProperty("umbracoBytes").Value     = file.ContentLength.ToString();
  76.           m.getProperty("umbracoExtension").Value = imageType;
  77.  
  78.          
  79.           // reload umbraco media page and close this window
  80.           ClientScript.RegisterStartupScript(this.GetType(), "finish", "opener.location.reload();window.close();", true);
  81.         }
  82.       }
  83.  
  84.  
  85.             closeWindow();
  86.     }
  87.  
  88.  
  89.  
  90.         private void handleError()
  91.         {
  92.             handleError("Ups... An unknown error occurred. The image could not be saved.");
  93.         }
  94.  
  95.         private void handleError(string text)
  96.         {
  97.             string jscript = String.Format("opener.location.reload();alert('{0}');window.close();", text);
  98.             ClientScript.RegisterStartupScript(this.GetType(), "error", jscript, true);
  99.         }
  100.  
  101.  
  102.         private void closeWindow()
  103.         {
  104.             ClientScript.RegisterStartupScript(this.GetType(), "error", "opener.location.reload();window.close();", true);
  105.         }
  106.        
  107.  
  108.  
  109.     private void generateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string thumbnailFileName)
  110.     {
  111.       // Generate thumbnail
  112.       float fx = (float)fileWidth / (float)maxWidthHeight;
  113.       float fy = (float)fileHeight / (float)maxWidthHeight;
  114.       // must fit in thumbnail size
  115.       float f = Math.Max(fx, fy); //if (f < 1) f = 1;
  116.       int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);
  117.  
  118.       // fixes for empty width or height
  119.       if (widthTh == 0)
  120.           widthTh = 1;
  121.       if (heightTh == 0)
  122.           heightTh = 1;
  123.  
  124.       // Create new image with best quality settings
  125.       Bitmap bp = new Bitmap(widthTh, heightTh);
  126.       Graphics g = Graphics.FromImage(bp);
  127.       g.SmoothingMode = SmoothingMode.HighQuality;
  128.       g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  129.       g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  130.  
  131.       // Copy the old image to the new and resized
  132.       Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
  133.       g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
  134.  
  135.       // Copy metadata
  136.       ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  137.       ImageCodecInfo codec = null;
  138.       for (int i = 0; i < codecs.Length; i++) {
  139.         if (codecs[i].MimeType.Equals("image/jpeg"))
  140.             codec = codecs[i];
  141.       }
  142.  
  143.       // Set compresion ratio to 90%
  144.       EncoderParameters ep = new EncoderParameters();
  145.       ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
  146.  
  147.       // Save the new image
  148.       bp.Save(thumbnailFileName, codec, ep);
  149.       bp.Dispose();
  150.       g.Dispose();
  151.     }
  152.   }  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement