Home > ASP.Net, Javascript, Jquery > Display image Thumbnail from Database in GridView and Display Enlarged Popup in ASP.Net

Display image Thumbnail from Database in GridView and Display Enlarged Popup in ASP.Net


Hi all,

Try this example to Display image Thumbnail from Database in GridView and Display Enlarged Popup in ASP.Net

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.23/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/Blitzer/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$('[id*=imgThumbnail]').bind('click', function () {
// $("#divImage").show();
$("[id*=imgBigImage]").attr('src', $(this).attr('alt'));
$("#divImage").dialog({
title: "Image",
modal: true,
resizable: false,
buttons: {
Close: function () {
$(this).dialog('close');
}
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="File Path">
<ItemTemplate>
<asp:Image ID="imgThumbnail" AlternateText='<%# Eval("FilePath") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="divImage" style="display: none;">
<asp:Image ID="imgBigImage" runat="server" Width="300" Height="300" />
</div>
</form>
</body>
</html>

In Code behind :

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("FilePath");
dt.Rows.Add("Jellyfish", "Images/Jellyfish.jpg");
dt.Rows.Add("Koala", "Images/Koala.jpg");
dt.Rows.Add("Penguins", "Images/Penguins.jpg");
dt.Rows.Add("Tulips", "Images/Tulips.jpg");
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Web.UI.WebControls.Image img = e.Row.FindControl("imgThumbnail") as System.Web.UI.WebControls.Image;
string path = Server.MapPath(img.AlternateText);
img.ImageUrl = GenerateThumbnail(path);
}
}

private string GenerateThumbnail(string path)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
{
using (MemoryStream memoryStream = new MemoryStream())
{
thumbnail.Save(memoryStream, ImageFormat.Png);
Byte[] bytes = new Byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes, 0, (int)bytes.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
return "data:image/png;base64," + base64String;
}
}
}

public bool ThumbnailCallback()
{
return false;
}

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript, Jquery
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: