Click here to Skip to main content
6,822,613 members and growing! (20,383 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Thumbnail Images in GridView using C#

By S.Vinothkumar

Thumbnail Images in GridView using C#
C#2.0, Windows, .NET2.0, ASP.NET, WebForms, VS2005, Dev
Posted:22 Oct 2007
Views:34,018
Bookmarked:24 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 3.00 Rating: 3.86 out of 5

1
1 vote, 16.7%
2

3
4 votes, 66.7%
4
1 vote, 16.7%
5
Screenshot - thumb.gif

Introduction

ASP.Net have the gridview which is very usefull to display such kind of some datas or images like this. Here is I'm going to display images which are in a folder with thumbnail size.

Thumbnail Image

(First of all I need a support page which is create thumbnail image. Here is I'm working with imageresize.cs. This file will help you for creating thumbnail images.

ImageResize class have some functionalities for creating thumbnail images. I'm creating thumbnail images and write the image in that supporting page. In my image viewer page I have created a grid view and give the supporting page as resolving url in every images contain's in that appropriate folder.

Using the code

In my image viewer page

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindData();
    }


private void bindData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("S.No", typeof(string));
        dt.Columns.Add("grdImage", typeof(string));
        DataRow dr;
        int i = 1;
        foreach (string file in Directory.GetFiles(Server.MapPath("Images")+"\\","*.jpg"))
        {
            dr = dt.NewRow();
            dr[0] = i.ToString();
            dr[1] = ResolveUrl("ThumbnailCreator.aspx?ImageId="+file);
            dt.Rows.Add(dr);
            i += 1;
        }
        grdImageViewer.DataSource = dt;
        grdImageViewer.DataBind();
    }

From this above code I'm searching images with .jpg extension in the Images folder. I'm putting row number for every image and binding the thumbnail image from ThumbnailCreator.aspx page for the appropriate image.

In ThumbnailCreator page,

protected void Page_Load(object sender, EventArgs e)
    {
        string imgPath;
        if (Request.QueryString["ImageId"] != null)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ImageId"].ToString()))
            {
                imgPath = Request.QueryString["ImageId"].ToString();
                if (!string.IsNullOrEmpty(imgPath))
                {
                    byte[] imgByte = GetImageByteArr(new Bitmap(imgPath));
                    MemoryStream memoryStream = new MemoryStream();
                    memoryStream.Write(imgByte, 0, imgByte.Length);
                    System.Drawing.Image imagen = System.Drawing.Image.FromStream(memoryStream);
                    Response.ContentType = "image/Jpeg";
                    ImageResize ir = new ImageResize();
                    ir.File = imagen;
                    ir.Height = 60;
                    ir.Width = 60;
                    ir.GetThumbnail().Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }
    }

converting image to byte array

 private static byte[] GetImageByteArr(System.Drawing.Image img)
    {
        byte[] ImgByte;
        using (MemoryStream stream = new MemoryStream())
        {
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            ImgByte = stream.ToArray();
        }
        return ImgByte;
    }

From above,the query string will be come with the path of image which is to be conver to thumbnail image. I'm creating thumbnail page using ImageResize.cs. See the ImageResize.cs file in app_code. Thumbnail image is writing in page using Response.OutputStream.

Response type is "image/Jpeg". It's must for image. This will help to write in page as jpeg file.

Hence the Image has been created thumbnail image and displayed in a gridview.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

S.Vinothkumar


Member
Hi Viewers,

I wish to all. This is Vinoth. This is where I try to condense everything that you need to know about me.

Blog:

visit my blog

Interests:

I'm passionate about a great many things and continually learning about the things that interest me. They are wearable computers, User Interface Design, Artificial life, Industrial music.






Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralHuge bug in this code!!! Pinmemberevgtun4:14 13 Oct '09  
GeneralI cannot see any images after running the application Pinmemberivix4u19:26 3 Jun '09  
QuestionHow to achieve this with Windows Application? PinmemberNarendra Reddy Vajrala5:58 26 May '09  
GeneralMy vote of 2 Pinmemberrushi1234562:42 5 May '09  
GeneralSir am getting error what to do..? Pinmemberjaneshh20:01 13 Feb '08  
GeneralWhy not a handler? PinmemberDmitry Salko23:29 22 Oct '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 22 Oct 2007
Editor:
Copyright 2007 by S.Vinothkumar
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project