nuvium
asked on
Generating thumbnails in an ASP.NET datalist
Hey guys, I have a question that has two parts. The first part if by far the most important, the second part is more for my own general knowledge.
I have a site written in ASP.NET and VB.NET that displays image library information in a datalist. These images are not stored in the database, but their references (filename, location, etc) are, and they point to a repository on the webserver.
So, part 1 of the question is. If the images in the repository are all fullsize (very large), how can I display a thumbnailed version of the image in the datalist? I believe I already have the pieces of the puzzle, I just dont know how to make them work together. Listed below are the two (what I think are) main elements. The script that pulls the images out of the database and loads them into the datalist, and the script that creates a thumbnail on the fly. The problem is, I don't know how to tie the two together. The thumbnailing script someone else on EE helped me with, and it expects all instructions (filename and width for resizing) to come through the querystring, which obviously isnt going to help in this databound environment.
Here are the two snippets:
--- ASP.NET Front End DataList control --
<asp:DataList ID="dtlImages" runat="server" RepeatColumns="3" RepeatDirection="Horizonta l">
<HeaderTemplate>
<table cellspacing="5">
<tr>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNoData" runat="server" Visible="false" />
<td><div class="ProductTitle"><%#Ev al("ImageN ame")%></d iv><br />
<a href="ShowPic.aspx?iid=<%# Eval("Imag eID") %>">
<img src=<%#Eval("ImagePhysical Location") %><%#Eval( "ImageFile Name")%> border="0" />
</a><br /><span class="eventText">Download :<br /><a href="<%#Eval("ImagePhysic alLocation ")%><%#Eva l("ImageFi leName")%> ">Lo-Res</ a> |& nbsp;<a href="<%#Eval("ImagePhysic alLocation ")%><%#Eva l("ImageFi leName")%> ">Hi-Res</ a></span>
</td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
--- VB.NET Back End DataList binder ----
Dim cn As New SqlConnection(Configuratio nManager.C onnectionS trings("Co nn").Conne ctionStrin g)
Dim cmd As New SqlCommand("uspGetImagesSe arch", cn)
cmd.CommandType = Data.CommandType.StoredPro cedure
cmd.Parameters.Add(New SqlParameter("@ProductCate goryID", Data.SqlDbType.Int))
cmd.Parameters("@ProductCa tegoryID") .Value = iProductCatID
cmd.Parameters.Add(New SqlParameter("@SubCategory ID", Data.SqlDbType.Int))
cmd.Parameters("@SubCatego ryID").Val ue = iSubCatID
cmd.Parameters.Add(New SqlParameter("@ProductMode lNumber", Data.SqlDbType.VarChar))
cmd.Parameters("@ProductMo delNumber" ).Value = sModelNumber
cmd.Parameters.Add(New SqlParameter("@Keyword", Data.SqlDbType.VarChar))
cmd.Parameters("@Keyword") .Value = Me.txtKeywordsImageSearch. Text.ToUpp er
cn.Open()
dtlImages.DataSource = cmd.ExecuteReader()
dtlImages.DataBind()
cn.Close()
cn = Nothing
--- VB.NET Back End Thumbnailer script (expects filename and width to come from the qs) ---
Private Sub CreateThumbnail()
' Initialize objects
Dim objImage, objThumbnail As System.Drawing.Image
Dim strServerPath, strFilename As String
Dim shtWidth, shtHeight As Short
' Get image folder path on server - use "\" string if root
strServerPath = Server.MapPath("pimages\jp g\")
' Retrieve name of file to resize from query string
strFilename = strServerPath & Request.QueryString("filen ame")
' Retrieve file, or error.gif if not available
Try
objImage = objImage.FromFile(strFilen ame)
Catch
objImage = objImage.FromFile(strServe rPath & "error.gif")
End Try
' Retrieve width from query string
If Request.QueryString("width ") = Nothing Then
shtWidth = objImage.Width
ElseIf Request.QueryString("width ") < 1 Then
shtWidth = 100
Else
shtWidth = Request.QueryString("width ")
End If
' Work out a proportionate height from width
shtHeight = objImage.Height / (objImage.Width / shtWidth)
' Create thumbnail
objThumbnail = objImage.GetThumbnailImage (shtWidth, _
shtHeight, Nothing, System.IntPtr.Zero)
' Send down to client
Response.ContentType = "image/jpeg"
objThumbnail.Save(Response .OutputStr eam, Imaging.ImageFormat.Jpeg)
' Tidy up
objImage.Dispose()
objThumbnail.Dispose()
End Sub
The second part to my question is, how do I alter my code on the backend so that if no images are returned by the stored procedure, I display an error message rather than just an invisible datalist? I have a label in the datalist control ready to go, I just dont know how to handle it on the backend.
Thanks!!
I have a site written in ASP.NET and VB.NET that displays image library information in a datalist. These images are not stored in the database, but their references (filename, location, etc) are, and they point to a repository on the webserver.
So, part 1 of the question is. If the images in the repository are all fullsize (very large), how can I display a thumbnailed version of the image in the datalist? I believe I already have the pieces of the puzzle, I just dont know how to make them work together. Listed below are the two (what I think are) main elements. The script that pulls the images out of the database and loads them into the datalist, and the script that creates a thumbnail on the fly. The problem is, I don't know how to tie the two together. The thumbnailing script someone else on EE helped me with, and it expects all instructions (filename and width for resizing) to come through the querystring, which obviously isnt going to help in this databound environment.
Here are the two snippets:
--- ASP.NET Front End DataList control --
<asp:DataList ID="dtlImages" runat="server" RepeatColumns="3" RepeatDirection="Horizonta
<HeaderTemplate>
<table cellspacing="5">
<tr>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNoData" runat="server" Visible="false" />
<td><div class="ProductTitle"><%#Ev
<a href="ShowPic.aspx?iid=<%#
<img src=<%#Eval("ImagePhysical
</a><br /><span class="eventText">Download
</td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
--- VB.NET Back End DataList binder ----
Dim cn As New SqlConnection(Configuratio
Dim cmd As New SqlCommand("uspGetImagesSe
cmd.CommandType = Data.CommandType.StoredPro
cmd.Parameters.Add(New SqlParameter("@ProductCate
cmd.Parameters("@ProductCa
cmd.Parameters.Add(New SqlParameter("@SubCategory
cmd.Parameters("@SubCatego
cmd.Parameters.Add(New SqlParameter("@ProductMode
cmd.Parameters("@ProductMo
cmd.Parameters.Add(New SqlParameter("@Keyword", Data.SqlDbType.VarChar))
cmd.Parameters("@Keyword")
cn.Open()
dtlImages.DataSource = cmd.ExecuteReader()
dtlImages.DataBind()
cn.Close()
cn = Nothing
--- VB.NET Back End Thumbnailer script (expects filename and width to come from the qs) ---
Private Sub CreateThumbnail()
' Initialize objects
Dim objImage, objThumbnail As System.Drawing.Image
Dim strServerPath, strFilename As String
Dim shtWidth, shtHeight As Short
' Get image folder path on server - use "\" string if root
strServerPath = Server.MapPath("pimages\jp
' Retrieve name of file to resize from query string
strFilename = strServerPath & Request.QueryString("filen
' Retrieve file, or error.gif if not available
Try
objImage = objImage.FromFile(strFilen
Catch
objImage = objImage.FromFile(strServe
End Try
' Retrieve width from query string
If Request.QueryString("width
shtWidth = objImage.Width
ElseIf Request.QueryString("width
shtWidth = 100
Else
shtWidth = Request.QueryString("width
End If
' Work out a proportionate height from width
shtHeight = objImage.Height / (objImage.Width / shtWidth)
' Create thumbnail
objThumbnail = objImage.GetThumbnailImage
shtHeight, Nothing, System.IntPtr.Zero)
' Send down to client
Response.ContentType = "image/jpeg"
objThumbnail.Save(Response
' Tidy up
objImage.Dispose()
objThumbnail.Dispose()
End Sub
The second part to my question is, how do I alter my code on the backend so that if no images are returned by the stored procedure, I display an error message rather than just an invisible datalist? I have a label in the datalist control ready to go, I just dont know how to handle it on the backend.
Thanks!!
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.