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

Forums / Developing with Sitefinity / Resizing Images

Resizing Images

3 posts, 0 answered
  1. Steven Lancaster
    Steven Lancaster avatar
    82 posts
    Registered:
    06 Nov 2009
    01 Sep 2010
    Link to this post
    I have the following code I wrote on another app to resize an image, but I need to add this to sitefinity where you upload. So to explain better I create a News Story and I need to add an image I want to run this code as they upload so when it is put in the library the image has already been resized. Please let me know the best way to do this.

    public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
    {
        System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
      
        // Prevent using images internal thumbnail
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
      
        if (OnlyResizeIfWider)
        {
            if (FullsizeImage.Width <= NewWidth)
            {
                NewWidth = FullsizeImage.Width;
            }
        }
      
        int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
        if (NewHeight > MaxHeight)
        {
            // Resize with height instead
            NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
            NewHeight = MaxHeight;
        }
      
        System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
      
        // Clear handle to original file so that we can overwrite it if necessary
        FullsizeImage.Dispose();
      
        // Save resized picture
        NewImage.Save(NewFile);
    }
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    01 Sep 2010
    Link to this post
    Hi Steven Lancaster,

    It depends on from where and how they will upload the image. If your users are going to use Images and Documents module and its RadUpload control (LibraryItemUpload view) you can subscribe for the Global.asax Application_Start event to the static Executing event of the LibraryManager manager like this:

    void Application_Start(object sender, EventArgs e)  
        Telerik.Libraries.LibraryManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(LibraryManager_Executing); 
      
    void LibraryManager_Executing(object sender, Telerik.ExecutingEventArgs args) 
        //The code continues here  
    }


    Sincerely yours,
    Ivan Dimitrov
    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
  3. Steven Lancaster
    Steven Lancaster avatar
    82 posts
    Registered:
    06 Nov 2009
    01 Sep 2010
    Link to this post
    I will look at the control if I have any more questions I will post. I definitely want them to use the Library.

    Thanks Ivan

    Steve
Register for webinar
3 posts, 0 answered