Link to home
Create AccountLog in
Avatar of nuvium
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="Horizontal">
<HeaderTemplate>
 <table cellspacing="5">
 <tr>
         </HeaderTemplate>
          <ItemTemplate>
            <asp:Label ID="lblNoData" runat="server" Visible="false" />
           <td><div class="ProductTitle"><%#Eval("ImageName")%></div><br />
            <a href="ShowPic.aspx?iid=<%#Eval("ImageID") %>">
            <img src=<%#Eval("ImagePhysicalLocation")%><%#Eval("ImageFileName")%> border="0" />
              </a><br /><span class="eventText">Download:<br /><a href="<%#Eval("ImagePhysicalLocation")%><%#Eval("ImageFileName")%>">Lo-Res</a>&nbsp;|&nbsp;<a href="<%#Eval("ImagePhysicalLocation")%><%#Eval("ImageFileName")%>">Hi-Res</a></span>
                            </td>
                            </ItemTemplate>
                            <FooterTemplate>
                             </tr>
                             </table>
                            </FooterTemplate>
   </asp:DataList>

--- VB.NET Back End DataList binder ----

 Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)
        Dim cmd As New SqlCommand("uspGetImagesSearch", cn)
        cmd.CommandType = Data.CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@ProductCategoryID", Data.SqlDbType.Int))
        cmd.Parameters("@ProductCategoryID").Value = iProductCatID
        cmd.Parameters.Add(New SqlParameter("@SubCategoryID", Data.SqlDbType.Int))
        cmd.Parameters("@SubCategoryID").Value = iSubCatID
        cmd.Parameters.Add(New SqlParameter("@ProductModelNumber", Data.SqlDbType.VarChar))
        cmd.Parameters("@ProductModelNumber").Value = sModelNumber
        cmd.Parameters.Add(New SqlParameter("@Keyword", Data.SqlDbType.VarChar))
        cmd.Parameters("@Keyword").Value = Me.txtKeywordsImageSearch.Text.ToUpper
        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\jpg\")
        ' Retrieve name of file to resize from query string
        strFilename = strServerPath & Request.QueryString("filename")
        ' Retrieve file, or error.gif if not available
        Try
            objImage = objImage.FromFile(strFilename)
        Catch
            objImage = objImage.FromFile(strServerPath & "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.OutputStream, 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!!
ASKER CERTIFIED SOLUTION
Avatar of kennethfine
kennethfine

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer