简体   繁体   中英

retrieve blob from mysql and display using repeater in asp.net c# and linq datasource

I have stored manually images in the field categoryImage in Longblob datatype in my category table in mysql database . I have added a devart linq-mysql model to retrieve the data from database .

I have added a linq datasource to a repeater control and want to retrieve blob datatype image from the database directly . when i write this code
<%# Eval("CategoryImage" %>

I get a System.Byte[] as an output . somebody gave me a suggestion that i should convert the byte array to an image using the method

    public System.Drawing.Image byteArrayToImage (System.Byte[] ByteInArray)
    {
        MemoryStream ms = new MemoryStream(ByteInArray);
        System.Drawing.Image returnimage = System.Drawing.Image.FromStream(ms);
        return returnimage;

    }              

when i wrote this code <%# byteArrayToImage(Eval("CategoryImage") %> It gives me an error that byteArrayToImage(btye[]) has some invalid arguments. can anyone help me on this problem.

thanks in advance..

Firstly, your tag seems not well formed:

<%# byteArrayToImage(Eval("CategoryImage") %>

should be:

<%# byteArrayToImage(Eval("CategoryImage")) %>

Also, the Eval method returns object , the byteArrayToImage method parameter is of type System.Byte[] , so you would have to cast it to the proper type.

Additionally, it would be better if you made it clear to what element you are applying the byteArrayToImage method returned value. It may not work correctly with the an instance of System.Drawing.Image .

If you are trying to create an <asp:Image> control based on the System.Drawing.Image , it just won't work as that control only supports loading an image through an URL.

An alternative to that would be creating a custom HTTP handler for your images or creating a file with the image content and giving the URL to the <asp:Image> control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM