How can I automatically resize images when they're uploaded?

2 posts, 0 answers
  1. Roelande
    Roelande avatar
    50 posts
    Member since:
    May 2006

    Posted 24 Apr 2012 Link to this post

    Hello,

    I'm trying to automatically resize images when they're being uploaded. After reading on the forums for a while, I found out I can use a FileSystemContentProvider for this.

    Here's what I've come up with:

    public class TextEditor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            radEditor.ImageManager.ContentProviderTypeName = typeof(MyResizer).AssemblyQualifiedName;
             
            radEditor.ImageManager.ViewPaths = new[] { "~/Images" };
            radEditor.ImageManager.UploadPaths = new[] { "~/Images" };
            radEditor.ImageManager.DeletePaths = new[] { "~/Images" };
        }
    }
     
    public class MyResizer : FileSystemContentProvider
    {
        public MyResizer(HttpContext context, string[] searchPatterns, string[] viewPaths, string[] uploadPaths, string[] deletePaths, string selectedUrl,
                      string selectedItemTag) : base(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)
        {
        }
     
        public override string StoreFile(Telerik.Web.UI.UploadedFile file, string path, string name, params string[] arguments)
        {
            string result = base.StoreFile(file, path, name, arguments);
     
            using (System.Drawing.Image originalImage = Bitmap.FromStream(file.InputStream))
            {
                int originalWidth = originalImage.Width;
                int originalHeight = originalImage.Height;
     
                int newWidth = 500;
                int newHeight = originalHeight / originalWidth * newWidth;
     
                System.Drawing.Image resizedImage = ResizeImage(originalImage, newWidth, newHeight);
     
                string fileName = Path.Combine(path, name);
                resizedImage.Save(Context.Server.MapPath(fileName));
            }
     
            return result;
        }
         
        private Image ResizeImage(Image originalImage, int newWidth, int newHeight)
        {
            // Resize Image
        }
    }

    This works fine, but as you can see the new width of the image (500) is hard coded. How can I turn this into a parameter?

    We access the page with the Editor on it like this: /TextEditor.aspx?textid=1&ImageWidth=500

    How can we pass the ImageWidth parameter in the querystring to our implementation of the FileSystemContentProvider?

    Regards,
    Kristof
  2. Rumen
    Admin
    Rumen avatar
    14361 posts

    Posted 25 Apr 2012 Link to this post

    Hello,

    Using the information provided in the following KB article Get reference to the Page object inside a custom FileBrowserContentProvider, you can access the query string information of the parent page and get the value of the ImageWidth parameter inside the File Browser Content Provider and its StoreFile function, e.g.

    Page.Request.QueryString["ImageWidth"];

    All the best,
    Rumen
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top