Image Resize

Posted by mfalac on 11/24/2009 6:28:23 AM

<cc1:GeneratedImage ID="ResizedImageGenerator" runat="server" ImageHandlerUrl="ResizeImageHandler.ashx">
                <Parameters>
                    <cc1:ImageParameter Name="ImageUrl" Value="~/Images/Hiking.jpg" />
                    <cc1:ImageParameter Name="Width" Value="400" />
                </Parameters>
     </cc1:GeneratedImage>

<%@ WebHandler Language="VB" Class="ResizeImageHandler" %>

Imports System
Imports System.IO
Imports System.Drawing
Imports System.Web
Imports Microsoft.Web

Public Class ResizeImageHandler
    Inherits ImageHandler
    Public Sub New()
        MyBase.ContentType = Imaging.ImageFormat.Jpeg
        MyBase.EnableServerCache = True
    End Sub
    Public Overrides Function GenerateImage(ByVal parameters As System.Collections.Specialized.NameValueCollection) As Microsoft.Web.ImageInfo
        'Get the parameters
        If String.IsNullOrEmpty(parameters("ImageUrl")) OrElse (String.IsNullOrEmpty(parameters("Width")) AndAlso String.IsNullOrEmpty(parameters("Height"))) Then
            Throw New ArgumentException("You must supply the ImageUrl parameter along with either the Width or Height parameters")
        End If
        Dim imageUrl As String = parameters("ImageUrl")
        Dim imageFile As String = HttpContext.Current.Server.MapPath(imageUrl)
        ' Add the resize transform logic
        Dim resizeTrans As New ImageResizeTransform
        If Not String.IsNullOrEmpty(parameters("Width")) Then
            resizeTrans.Width = Convert.ToInt32(parameters("Width"))
        Else
            resizeTrans.Height = Convert.ToInt32(parameters("Height"))
        End If
        MyBase.ImageTransforms.Add(resizeTrans)
        If Not File.Exists(imageFile) Then
            Throw New ArgumentException(String.Format("The file {0} could not be found", imageFile))
        End If
        Return New ImageInfo(File.ReadAllBytes(imageFile))
    End Function

End Class





Average rating for this article is: 3.00
This article is 4 times rated.

Comments:


Add Your Comment

Your Name:  
Your Email:    
Your WebPage:  
Your Comment:  
500 character limit:
 

Categories

Ajax
(9 Articles) Ajax Control Toolkit
Data Access
(11 Articles) data entries DAL, BLL, ADO, SQL,...
Generated Image Controls
(2 Articles) Generated Image Controls
Journal
(7 Articles) Page Updates
Music
(15 Articles) Music
Vb
(1 Articles) Visual Basic codes things bla bla

Tag Cloud

Top Rated Articles

SQL count and avg
(Avg:4.00 | Rated:9)
binding the DataTable
(Avg:4.00 | Rated:4)
perinaz.com
(Avg:5.00 | Rated:3)
Image Resize
(Avg:3.00 | Rated:4)
Şebnem FERAH - Sigara
(Avg:3.67 | Rated:3)
getting month value
(Avg:3.33 | Rated:3)
first blog
(Avg:3.33 | Rated:3)
Calendar Control And Events
(Avg:3.00 | Rated:3)
PopUpExtender and Comma
(Avg:3.50 | Rated:2)
Dream Antimatter
(Avg:3.00 | Rated:2)
Loading