0

I am making a Bookstore website. I use Visual Studio 2010 and MS SQL Database. I have images about books. I have a Book table in database and there is an image column in this table.

I saved these images (in byte array format) in database.

I tested it in Windows Form Application and everything is ok. I mean i can retrieve and save images to/from database. when I retrieve them from database, i save them (in System.Drawing.Image format) in Book class.

    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()
           {
            }
    }

I use datalist with objectdatasource in Asp.net 4 Web Project. I wrote Book and BookLayer class for objectdatasource. I can show all informations in datalist except image. because image is System.Drawing.Image in Book class but image is System.ui.WebControls.Image which in datalist template item. Format is different. How can i do that ? is that way wrong ? Pls give me any advice.

Eyüp Alemdar
  • 233
  • 3
  • 8
  • 14
  • You can use handler (.ashx) to display image – Satinder singh Dec 31 '12 at 10:48
  • 1
    Question has been asked [before](http://stackoverflow.com/questions/1357826/converting-system-web-ui-webcontrols-image-to-system-drawing-image) with the short answer that you can't convert from System.Drawing.Image to WebControls.Image because you try to convert an image resource into an HTML control. Best would be to write a load routine to retrieve image information from database and use this in your application as stated [here](http://forums.asp.net/t/1767962.aspx/1) – SaschaM78 Dec 31 '12 at 12:01

1 Answers1

0

Yes use handler. You can do as Add a template field

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

use the following java script function getpic(img, pid) {

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

in your 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();
            }
Ratna
  • 2,289
  • 3
  • 26
  • 50
  • Thanks all of you especially SaschaM78, I followed your advices and I did it using handler as described on this site http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html – Eyüp Alemdar Dec 31 '12 at 12:50