Reduce image file size

2 posts, 0 answers
  1. Tim
    Tim avatar
    2 posts
    Member since:
    Nov 2010

    Posted 18 Feb 2011 Link to this post



    1. When a large image is uploaded – say, 3MB(+), does the editor allow to programmatically reduce the image file size saved on server side?
    2. If yes, how to do that? If not, is Telerik planning to support this kind of feature?
    3. If Telerik doesn’t support and is not planning to support this feature, is there any event of the editor that can be used to trigger extra code to do this job?




  2. Rumen
    Rumen avatar
    14324 posts

    Posted 21 Feb 2011 Link to this post

    Hello Tim,

    In order to achieve the requested functionality you should implement a FileSystemContentProvider and
    you will be able to get a reference to the uploaded image file and rename it using the StoreFilemethod. Here is an example demonstrating how to create a thumbnail, which you can modify to fit your scenario:

    using System;  
    using System.Collections;  
    using System.Configuration;  
    using System.Data;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.HtmlControls;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using Telerik.Web.UI;  
    using System.Drawing;  
    using System.IO;  
    public partial class static_test_editor : System.Web.UI.Page  
        protected void Page_Load(object sender, EventArgs e)  
            RadEditor1.ImageManager.ViewPaths = new string[]{"~/"};  
            RadEditor1.ImageManager.UploadPaths = new string[] { "~/" };  
            RadEditor1.ImageManager.DeletePaths = new string[] { "~/" };  
            RadEditor1.ImageManager.ContentProviderTypeName = typeof(myprovider).AssemblyQualifiedName;  
        public class myprovider : Telerik.Web.UI.Widgets.FileSystemContentProvider  
            public myprovider(HttpContext context, string[] searchPatterns, string[] viewPaths, string[] uploadPaths, string[] deletePaths, string selectedUrl, string selectedItemTag)  
                : base(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)  
            public bool ThumbnailCallback()  
                return false;  
            public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments)  
                string result = base.StoreFile(file, path, name, arguments);  
                System.Drawing.Image.GetThumbnailImageAbort myCallback =  
                    new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);  
                string fileName = Path.Combine(path, name);  
                //Create a copy of the image with a different size  
                using (System.Drawing.Image originalImage = Bitmap.FromStream(file.InputStream))  
                    using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(300, 300, myCallback, IntPtr.Zero))  
                return result;  

    You can also see the following KB article on the subject: Custom FileBrowserContent Provider.

    Note that the code above uses FileSystemContentProvider as a base class and not FileBrowserContentProvider. These are two different classes: FileBrowserContentProvider is the base class for all content providers and is the one with the abstract members; FileSystemContentProvider is the default content provider used by the editor - it works with the file system and has implementations for all necessary methods. This is why when you subclass the FileSystemContentProvider class, you do not need to re-define all content provider methods - just the ones you want to change. In the example above we override the StoreFile method, which is called when you upload a new file (image).

    Best regards,
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top