Resizing an image in asp.net

This is one of the answers I posted on dev-exchange about a question about how to resize the images in asp.net. This example shows how to resize an image into thumbnail and full image using createthumb_fullimages function.

Add this function to your code behind


Code:

Function createthumb_fullimages(ByVal sOriginalImg As String, _
                                    ByVal sThumbImgFolder As String, _
                                    Optional ByVal sFullImgFolder As String = "") As Boolean

        Try
            'delegate to produce a dummy call back
            Dim fakeCallBack As System.Drawing.Image.GetThumbnailImageAbort
            fakeCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf FakeMethod)

            ' Original file name
            Dim sOrigFileName As String = System.IO.Path.GetFileName(sOriginalImg)

            Dim oImg As System.Drawing.Image
            oImg = System.Drawing.Image.FromFile(sOriginalImg)
            oImg = oImg.GetThumbnailImage(80, 80, fakeCallBack, IntPtr.Zero)
            oImg.Save(sThumbImgFolder + sOrigFileName, oImg.RawFormat)

            ' if full image is passed
            If Trim(sFullImgFolder) <> "" Then
                oImg = oImg.GetThumbnailImage(150, 150, fakeCallBack, IntPtr.Zero)
                oImg.Save(sFullImgFolder + sOrigFileName, oImg.RawFormat)
            End If

            Return True
        Catch ex As Exception
            ' some error occured. add message here
            Return False
        End Try
    End Function

    Function FakeMethod() As Boolean
        Return False
    End Function


Then call this on your upload button. Make sure that you are passing full path to image
and also you are giving the image directory path.


Code:

Protected Sub Uploaded_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

        ' Create Thumb nail and full image from the uploaded image
        If (createthumb_fullimages(Server.MapPath("~") + "/someuploaded.jpg", _
                                   Server.MapPath("~") + "/thumimgs/", _
                                   Server.MapPath("~") + "/fullimgs/") = True) Then

            Response.Write("Images created OK")
        Else
            Response.Write("Error Couldnt create Images")
        End If

    End Sub


This may help someone out there.

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net