This is a migrated thread and some comments may be shown as answers.

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

1 Answer 91 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Roelande
Top achievements
Rank 1
Roelande asked on 24 Apr 2012, 08:08 AM
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

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 25 Apr 2012, 03:04 PM
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.
Tags
Editor
Asked by
Roelande
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Share this question
or