开发者

Quick query to retrieve binary image on Image Load?

Is there such a thing?!

So for eample, on my image load I have the following hooked up to a table where "Image" is a binary image set of data.

protected void Image1_Load1(object sender, EventArgs e)
    {
         myent logo = new myent();

        var query = (from p in 开发者_如何学JAVAlogo.tblLogoes
                    where p.Id == id && p.Id2 == id2
                    select p.Image).First();

        return query.  
    }

What do I need to return here to populate the image?

I have funny feeling, it's not going to be as simple as that!

Any pointers gratefully received.


I would create a Http Handler to do this. Some browser support writing out images binary in the mark-up, but this is non-standard so I would recommend against it.

If you can get bytes from your query do something like this:

Then create the handler.

public class IISHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpeg";
        myent logo = new myent();

        var query = (from p in logo.tblLogoes
                    where p.Id == id && p.Id2 == id2
                    select p.Image).First();
        byte[] bytes = query.bytes;
        context.Response.OutputStream.Write( bytes, 0, bytes.Length);
    }
}

Register the handle:

<system.web>
<httpHandlers>
    <!-- ImageHandler handlers -->
    <add verb="*" path="myimages/*.jpg" type="namespace.IISHandler, namespace" />
</httpHandlers>

...

On your webpage let the <img> tag point to the handler.

<img src="myimages/someimage.jpg">

Your code shows little about how you target images, id's, name's? Most likely you want to pass in some identifier to the handler, so you can serve up the right image.

Take a look at http://msdn.microsoft.com/en-us/library/ms972953.aspx for a more in-depth sample on serving dynamic content with http handlers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜