【问题标题】:How to set System.Drawing.Image to System.ui.WebControls.Image in datalist template item如何在 datalist 模板项中将 System.Drawing.Image 设置为 System.ui.WebControls.Image
【发布时间】:2012-12-31 10:38:50
【问题描述】:

我正在制作一个书店网站。我使用 Visual Studio 2010 和 MS SQL 数据库。我有关于书籍的图片。我在数据库中有一个 Book 表,并且该表中有一个图像列。

我将这些图像(以字节数组格式)保存在数据库中。

我在 Windows 窗体应用程序中对其进行了测试,一切正常。我的意思是我可以检索图像并将其保存到数据库中。当我从数据库中检索它们时,我将它们(以 System.Drawing.Image 格式)保存在 Book 类中。

    public Book
    {
          private int id;
          private System.Drawing.Image image;
          // name and other .. informations,  constructor, get and set methods;
    }

    public BookLayer
    {
           // after call this method i can get all informations from database
           public static List<Book> getBooks()
           {
            }
    }

我在 Asp.net 4 Web 项目中使用 datalist 和 objectdatasource。我为 objectdatasource 编写了 Book 和 BookLayer 类。我可以在数据列表中显示除图像之外的所有信息。 因为图像是 Book 类中的 System.Drawing.Image,但图像是 Datalist 模板项中的 System.ui.WebControls.Image。格式不同。我怎样才能做到这一点 ?那是错的吗?请给我任何建议。

【问题讨论】:

  • 您可以使用处理程序(.ashx)来显示图像
  • 已向before 提出了一个简短的回答,即您无法将 System.Drawing.Image 转换为 WebControls.Image,因为您尝试将图像资源转换为 HTML 控件。最好的办法是编写一个加载例程来从数据库中检索图像信息并在您的应用程序中使用它,如 here 所述

标签: c# asp.net datalist system.drawing


【解决方案1】:

是的,使用处理程序。您可以这样做添加模板字段

 <ItemTemplate>
<img border="2" width="150px" src="../images/loading.gif" onload="getpic(this,'<%# Eval("bkid")%>');"/>
</ItemTemplate>

使用以下java脚本 函数 getpic(img, pid) {

try {
img.onload = null;
img.src = '../Getimage.ashx?id=' + pid;
} catch (e) {
alert(e);
}
}
</script>

在你的 getimage.ashx 中

byte[] imageBytes = ;// ToDOget your byte
            Bitmap newBmp = ConvertToBitmap(imageBytes);
            if (newBmp != null)
            {
                newBmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                newBmp.Dispose();
            }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多