0

私は書店のウェブサイトを作っています。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 プロジェクトの objectdatasource で datalist を使用しています。objectdatasource の Book および BookLayer クラスを作成しました。画像以外のすべての情報をデータリストに表示できます。画像はBookクラスのSystem.Drawing.Imageですが、画像はデータリストテンプレートアイテムのSystem.ui.WebControls.Imageであるためです。フォーマットが異なります。どうやってやるの ?その方法は間違っていますか?Plsは私に何かアドバイスをください。

4

1 に答える 1

0

はい、ハンドラーを使用します。テンプレートフィールドを追加する

 <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();
            }
于 2012-12-31T12:01:03.020 に答える