2013-06-21 47 views
0

我似乎在這裏做錯了什麼。將位圖分配給gridview內的asp:image控件

我想要做的就是在gridview中顯示縮略圖。

ASPX頁面有一個gridview和一個asp:image控件。 asp:image控件的圖像來自網站。

一旦我得到imageUrl,我想使用下面的ResizeImage函數將圖像渲染爲縮略圖。最後,我想如示於下面的代碼

protected void SearchResults_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType.Equals(DataControlRowType.DataRow)) 
    { 
     System.Web.UI.WebControls.Image imgNew = (System.Web.UI.WebControls.Image)e.Row.FindControl("ProductThumbField"); 
     imgNew.Source = Utils.ResizeImage(imgNew.ImageUrl.ToString(),60,60); 
    } 
} 

下面是調整大小的圖像的代碼分配縮略圖到GridView列,用於顯示縮略圖圖像。(摘自一個來自stackoverflow問題&答案的例子)。

private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight) 
{ 
    using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(filename)) 
    { 
     //Caluate new Size 
     int newWidth = originalImage.Width; 
     int newHeight = originalImage.Height; 
     double aspectRatio = (double)originalImage.Width/(double)originalImage.Height; 
     if (aspectRatio <= 1 && originalImage.Width > maxWidth) 
     { 
      newWidth = maxWidth; 
      newHeight = (int)Math.Round(newWidth/aspectRatio); 
     } 
     else if (aspectRatio > 1 && originalImage.Height > maxHeight) 
     { 
      newHeight = maxHeight; 
      newWidth = (int)Math.Round(newHeight * aspectRatio); 
     } 
     Bitmap newImage = new Bitmap(newWidth, newHeight); 
     using (Graphics g = Graphics.FromImage(newImage)) 
     { 
      //--Quality Settings Adjust to fit your application 
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; 
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 
      g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height); 

      return newImage; 
     } 
    } 

這是ASPX頁面的代碼片段。

<asp:GridView ID="SearchResults" runat="Server" AutoGenerateColumns="false" EnableViewState="false" 
AllowPaging="true" PageSize="10" ondatabound="SearchResults_DataBound" > 
<RowStyle CssClass="EvenRow" /> 
<AlternatingRowStyle CssClass="OddRow" /> 
<Columns> 
    <asp:TemplateField meta:resourceKey="ProductThumbField"> 
     <ItemStyle CssClass="ProductNameColumn" /> 
     <HeaderStyle CssClass="ProductNameColumn" /> 
     <ItemTemplate> 
      <asp:image id="ProductThumbImg" runat="server" imageurl='<%# GetProductThumb(Container.DataItem) %>' Width ="60px" Height = "60px" /> 
     </ItemTemplate> 
    </asp:TemplateField> 

回答

0

我發現我不能分配位圖,因爲位圖從System.Drawing中,而ASP產生的原因是:圖像是從的System.Web ...

相關問題