How to Resize Images
RadUpload has been replaced by RadAsyncUpload, Telerik’s next-generation ASP.NET upload component. If you are considering Telerik’s Upload control for new development, check out the documentation of RadAsyncUpload or the control’s product page. If you are already using RadUpload in your projects, you may be interested in reading how easy the transition to RadAsyncUpload is and how you can benefit from it in this blog post. The official support for RadUpload has been discontinued in June 2013 (Q2’13), although it is still be available in the suite. We deeply believe that RadAsyncUpload can better serve your upload needs and we kindly ask you to transition to it to make sure you take advantage of its support and the new features we constantly add to it.
A common requirement is to resize uploaded images. To accomplish this, you can use the classes inSystem. Drawing namespace.
The following example demonstrates how to save uploaded images and create thumbnails.
For more information, see:https://msdn2.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
using System.Drawing;
using System.IO;
using Telerik.Web.UI;
...
public bool ThumbnailCallback()
{
return false;
}
protected void Button1_Click( object sender, EventArgs e)
{
string target = Server.MapPath("~/UploadedImages" );
Image.GetThumbnailImageAbort thumbnailImageAbortDelegate =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
foreach (UploadedFile file in RadUpload1.UploadedFiles)
{
file.SaveAs(Path.Combine(target, file.GetName()));
using (Bitmap originalImage = new Bitmap(file.InputStream))
{
//You can implement additional logic to compute
// the width / height according your requirements
int width = originalImage.Width / 2;
int height = originalImage.Height / 2;
using (Image thumbnail = originalImage.GetThumbnailImage(width, height, thumbnailImageAbortDelegate, IntPtr.Zero))
{
string thumbnailFileName = Path.Combine(target,
string.Format("{0}_thumb{1}" , file.GetNameWithoutExtension(), file.GetExtension()));
thumbnail.Save(thumbnailFileName);
}
}
}
}
This method is useful when the produced thumbnail image is less than 120 x 120px. If you need a bigger thumbnail you can use the following one:
// This method can create big thumbnails using resizing
private void CreateBigThumbNail(int newWidth, UploadedFile file)
{
string target = Server.MapPath("~/UploadedImages");
if (!Directory.Exists(target))
{
Directory.CreateDirectory(target);
}
file.SaveAs(Path.Combine(target, file.GetName()));
using (Bitmap originalImage = new Bitmap(file.InputStream))
{
int width = newWidth;
int height = (originalImage.Height * newWidth) / originalImage.Width;
Bitmap thumbnail = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)thumbnail))
g.DrawImage(originalImage, 0, 0, width, height);
string thumbnailFileName = Path.Combine(target,
string.Format("{0}_bthumb{1}", file.GetNameWithoutExtension(), file.GetExtension()));
thumbnail.Save(thumbnailFileName);
}
}