Is there anyway to display dynamically generated Bitmap on a asp image control?

后端 未结 1 1474
长情又很酷
长情又很酷 2021-02-20 01:54

In my code I create a bitmap dynamically, using c# and ASP.NET. Than I need to display it on the asp image control. There are anyway to do it whithout using handlers?

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-20 02:21

    Using a ashx handler is better cause it works on all browsers and you can cache the output images on the client.

    However if you must do it, images can be displayed inline directly using the tag as follows:

    
    

    ASPX:

    
    

    CS:

    MemoryStream ms = new MemoryStream();
    bitmap.Save(ms, ImageFormat.Gif);
    var base64Data = Convert.ToBase64String(ms.ToArray());
    imgCtrl.Src = "data:image/gif;base64," + base64Data;
    

    Yes, you could write the bitmap directly, but compressed formats(JPEG, GIF) are better for the web.

    Note: Inline images don't work on older browsers. Some versions of IE had limitations of max 32KB size.

    0 讨论(0)
提交回复
热议问题