我似乎在这里做错了。
我想要做的就是在gridview中显示缩略图。
ASPX页面有一个gridview和一个asp:image控件。 asp:image控件的图像来自一个网站。
一旦我获得imageUrl,我想使用ResizeImage函数下面的图像作为缩略图。最后,我想将缩略图分配给显示缩略图的gridview列,如下面的代码所示
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);
}
}
以下是调整图片大小的代码。(摘自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 :(得分:0)
我发现无法分配位图的原因是因为位图是从system.drawing创建的,而asp:image来自system.web ...