Click here to Skip to main content
15,881,659 members
Articles / Web Development / ASP.NET
Article

Thumbnail Images in GridView using C#

Rate me:
Please Sign up or sign in to vote.
3.90/5 (11 votes)
22 Oct 2007CPOL1 min read 123.2K   5.4K   32   9
Thumbnail Images in GridView using C#
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)


Written By
Web Developer
India India
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.

Comments and Discussions

 
GeneralMy vote of 1 Pin
manzoorafridi2-Aug-12 19:07
manzoorafridi2-Aug-12 19:07 
GeneralMy vote of 4 Pin
shivkumar singh4-Nov-11 2:45
shivkumar singh4-Nov-11 2:45 
GeneralMy vote of 1 Pin
chartierpw22-Aug-10 11:56
chartierpw22-Aug-10 11:56 
GeneralHuge bug in this code!!! Pin
evgtun13-Oct-09 3:14
evgtun13-Oct-09 3:14 
GeneralI cannot see any images after running the application Pin
ivix4u3-Jun-09 18:26
ivix4u3-Jun-09 18:26 
QuestionHow to achieve this with Windows Application? Pin
Narendra Reddy Vajrala26-May-09 4:58
Narendra Reddy Vajrala26-May-09 4:58 
Hi,
how can i add a image to gridview in c#.net 3.5 windows application?
please helpme.
GeneralMy vote of 2 Pin
rushi1234565-May-09 1:42
rushi1234565-May-09 1:42 
QuestionSir am getting error what to do..? Pin
janeshh13-Feb-08 19:01
janeshh13-Feb-08 19:01 
QuestionWhy not a handler? Pin
Dmitry Salko22-Oct-07 22:29
Dmitry Salko22-Oct-07 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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