+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Image Auto resizing

Image Auto resizing

4 posts, 1 answered
  1. Roberto Modica
    Roberto Modica avatar
    146 posts
    Registered:
    08 Feb 2008
    17 Mar 2010
    Link to this post
    Hi,

    I need to auto resize images that get uploaded to image librarys in the system, but dont know how to go about doing this.

    i tried to use:

    http://www.sitefinity.com/support/kb/sitefinity-3-x/how-to-autoresize-images-for-the-image-documents-module-sitefinity-3-2-sp1.aspx

    but it seems to do some very strange things to my admin, such as stopping the loading of content within the modules section.

    Can anyone help?

    Thanks,

    Rob
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    17 Mar 2010
    Link to this post
    Hi Roberto Modica,

    Thank you for using our services.

    This method works as expected, however there are some differences in later version. Instead of args you have to use e. Consider sample bellow:
    void Application_Start(object sender, EventArgs e)
    {
        Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing);
    }
    void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs e)
    {
        if (e.CommandName == "CreateContent" || e.CommandName == "UpdateContent")
        {
            if (e.CommandArguments is Telerik.Cms.Engine.IContent)
            {
                Telerik.Cms.Engine.IContent currentItem = ((Telerik.Cms.Engine.IContent)e.CommandArguments);
     
                if (Telerik.Cms.Engine.ImagesHelper.IsBrowserImage(currentItem))
                {
                    long width = 0;
                    long height = 0;
     
                    object o2 = currentItem.GetMetaData("Width");
                    if (o2 != null)
                        width = Convert.ToInt64(o2);
     
                    o2 = currentItem.GetMetaData("Height");
                    if (o2 != null)
                        height = Convert.ToInt64(o2);
     
                    if (width > 900 || height > 500)
                    {
                        System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(currentItem.Content as byte[]));
                        img = Telerik.Cms.Engine.ImagesHelper.GenerateThumbnail(900, 500, img, true, true);
     
                        System.IO.MemoryStream stream = new System.IO.MemoryStream();
                        Telerik.Cms.Engine.ImagesHelper.SaveImageToStream(img, stream, currentItem.MimeType);
                        currentItem.Content = stream.GetBuffer();
     
                        currentItem.SetMetaData("Size", ((byte[])currentItem.Content).Length);
                        currentItem.SetMetaData("Width", img.Width);
                        currentItem.SetMetaData("Height", img.Height);
                    }
                }
            
        }
    }


    Kind regards,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  3. Roberto Modica
    Roberto Modica avatar
    146 posts
    Registered:
    08 Feb 2008
    17 Mar 2010
    Link to this post
    Hi Radoslav,

    I have implemented your code but there still seems to be some issues. 

    The main problem is the constraint proportions.  I want the proportions to be kept but i want the width to be the default.  For instance my desired width is 554px so i put that in the method and set the height to 300, but my image as shown in the atteched image dimensions gets resized to 399x300 maintaining the proportions of the height above the width????

    Rob
  4. Roberto Modica
    Roberto Modica avatar
    146 posts
    Registered:
    08 Feb 2008
    17 Mar 2010
    Link to this post
    I made the hieght the larger of the dimensions and that sorted it :)

    Thanks
Register for webinar
4 posts, 1 answered