How to display image inside web form from Byte Array with C#

前端 未结 3 915
温柔的废话
温柔的废话 2020-12-16 04:41

This is my code, all it does is clear the web page and draw the image, so the rest of the form disappears! I need to show the image inside the form.

This is my User

3条回答
  •  既然无缘
    2020-12-16 05:21

    Response.Clear(); is deleting all the buffered content (i.e. everything in your page).

    Instead of a user control to display a dynamic image, consider an HTTP handler (.ashx), like this:

    ImageHandler.ashx:

    public class ImageHandler : System.Web.IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // Logic to get byte array here
            byte[] buffer = WhateverLogicNeeded;
            context.Response.ContentType = "image/jpeg";
            context.Response.OutputStream.Write(buffer, 0, buffer.Length);
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    }
    

    Then in your .aspx page:

    
    

    Note: If you want a specific image, then you can pass a query string (i.e. image ID) to the handler and have the byte array logic account for using the query string value.

提交回复
热议问题