Hi
I wanted to resize images to a maximum width (In my case 600px) and retain the aspect ratio.
So I modified the VB script in the code example at
http://www.telerik.com/community/code-library/aspnet-ajax/editor/resizing-an-image-during-upload.aspx
As Follows
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports Telerik.Web.UI
Partial Class Default_Vb
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim paths As String() = New String() {"~/Images"}
RadEditor1.ImageManager.ViewPaths = paths
RadEditor1.ImageManager.UploadPaths = paths
RadEditor1.ImageManager.DeletePaths = paths
RadEditor1.ImageManager.ContentProviderTypeName = GetType(ChangeImageSizeProvider).AssemblyQualifiedName
End Sub
Public Class ChangeImageSizeProvider
Inherits Telerik.Web.UI.Widgets.FileSystemContentProvider
Public Sub New(ByVal context As HttpContext, ByVal searchPatterns As String(), ByVal viewPaths As String(), ByVal uploadPaths As String(), ByVal deletePaths As String(), ByVal selectedUrl As String, _
ByVal selectedItemTag As String)
MyBase.New(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, _
selectedItemTag)
End Sub
Public Overloads Overrides Function StoreFile(ByVal file As UploadedFile, ByVal path As String, ByVal name As String, ByVal ParamArray arguments As String()) As String
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(file.InputStream)
Dim physicalPath As String = Context.Server.MapPath(path)
'Set Maximum Width of Image
Dim ImageMaxwidth As Integer = 600
' Finds height and width of original image
Dim OriginalHeight As Single = Image.Height
Dim OriginalWidth As Single = Image.Width
' Finds height and width of resized image
Dim NewWidth As Integer
Dim NewHeight As Integer
If OriginalWidth >= ImageMaxwidth Then
If OriginalHeight > OriginalWidth Then
NewHeight = ImageMaxwidth
NewWidth = (OriginalWidth / OriginalHeight) * ImageMaxwidth
Else
NewWidth = ImageMaxwidth
NewHeight = (OriginalHeight / OriginalWidth) * ImageMaxwidth
End If
'Mycode
Else
NewHeight = OriginalHeight
NewWidth = OriginalWidth
'My Code
End If
Dim resultImage As System.Drawing.Image = ResizeImage(image, New Size(newWidth, newHeight))
resultImage.Save(physicalPath + name)
Dim result As String = path + name
Return result
End Function
Private Function ResizeImage(ByVal sourceImage As System.Drawing.Image, ByVal newSize As Size) As System.Drawing.Image
Dim bitmap As New Bitmap(newSize.Width, newSize.Height)
Dim g As Graphics = Graphics.FromImage(DirectCast(bitmap, System.Drawing.Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.CompositingQuality = CompositingQuality.HighQuality
g.SmoothingMode = SmoothingMode.HighQuality
g.DrawImage(sourceImage, 0, 0, newSize.Width, newSize.Height)
g.Dispose()
Return DirectCast(bitmap, System.Drawing.Image)
End Function
End Class
End Class
You can just set the ImageMaxWidth variable to whatever you'd like
Oh, and I just tweaked the resize quality a bit by adding the following
Works like a charm.
Thanks for the original code, really savied me a lot of time and effort.