Tuesday 28 February 2012

How to : Get thumbnail image for a specific image in asp.net




Introduction :

Photo gallery is the most common module in websites now a days. hence when developing
this module we need to have different size of images for displya purpose. like showing thumbnail for listing.

As we can resize the image as per the size need to fit it in thumbnail listing. but in this article I m going to show you how to generate thumbnail image from original image when we are uploading original image.

C#.Net

Private bool SavePhotoImage(FileUpload fupload, String LargeFileName, String ThumbFileName, String  Location)
     {
        if (fupload.PostedFile.FileName != "")
        {
            fupload.PostedFile.SaveAs(Location + "/" + LargeFileName);

            System.Drawing.Bitmap img_original = new System.Drawing.Bitmap(fupload.PostedFile.InputStream);

            System.Drawing.Image img_thumbnail = Functions.GetThumbnail(img_original);

            img_thumbnail.Save(Location + "/" + ThumbFileName);

        }
        else
        {
            return false;
        }
        return true;
    }

VB.Net

Private Function SavePhotoImage(fupload As FileUpload, LargeFileName As [String], ThumbFileName As [String], Location As [String]) As Boolean

        If fupload.PostedFile.FileName <> "" Then
        fupload.PostedFile.SaveAs(Convert.ToString(Location) & "/" & Convert.ToString(LargeFileName))
        Dim img_original As New System.Drawing.Bitmap(fupload.PostedFile.InputStream)
        Dim img_thumbnail As System.Drawing.Image = Functions.GetThumbnail(img_original)
        img_thumbnail.Save(Convert.ToString(Location) & "/" & Convert.ToString(ThumbFileName))
    Else
        Return False
    End If
        Return True
End Function



In above code you can find having GetThumbnail functional which return thumbnail image.

C#.Net

public System.Drawing.Image GetThumbnail(System.Drawing.Bitmap img_original)
        {
            int width = 0;
            int height = 0;
            if (img_original.Width > 100 | img_original.Height > 100)
            {

                if (img_original.Width > img_original.Height || img_original.Width == img_original.Height)
                {
                    double proportion = (double)img_original.Width / 100;
                    width = 100;
                    height = (int)(img_original.Height / proportion);
                }
                if (img_original.Width < img_original.Height)
                {
                    double proportion = (double)img_original.Height / 100;
                    height = 100;
                    width = (int)(img_original.Width / proportion);
                }

            }
            return img_original.GetThumbnailImage(width, height, null, IntPtr.Zero);
        }

VB.Net

Public Function GetThumbnail(img_original As System.Drawing.Bitmap) As System.Drawing.Image
    Dim width As Integer = 0
    Dim height As Integer = 0
    If img_original.Width > 100 Or img_original.Height > 100 Then
        If img_original.Width > img_original.Height OrElse img_original.Width = img_original.Height Then
            Dim proportion As Double = CDbl(img_original.Width) / 100
            width = 100
            height = CInt(Math.Truncate(img_original.Height / proportion))
        End If

        If img_original.Width < img_original.Height Then
            Dim proportion As Double = CDbl(img_original.Height) / 100
            height = 100
            width = CInt(Math.Truncate(img_original.Width / proportion))
        End If
    End If

    Return img_original.GetThumbnailImage(width, height, Nothing, IntPtr.Zero)

End Function

 I hope this article was useful and I thank you for viewing it. keep visiting blog , you can get more stuff.