Image control displays a picture that exists as a byte[] byte array

Source: Internet
Author: User

There was such a problem in the work. At first, it was easy to get a picture of the byte[], you can convert it into an in-memory picture object (such as System.Drawing.Image), and then assign to the page image control. Try to find that it is not done at all. Search the network and sort out two ways to implement it.

Sample code here!

First, introduce a new page. Aspx,image control URL points to this page

This page imagesource.aspx only one thing: Writes the byte[representing the picture] to the HTTP output stream.

1 response.binarywrite (imgbytes);

The effect of this page is to display only the picture itself.

Causes the ImageUrl property of the image control to point to the page.

1 string. Concat ("imagesource.aspx?name=""lighthouse");

Feature implementation. This kind of method is relatively simple, but the disadvantage is to introduce a seemingly redundant page imagesource.aspx. To avoid this, there is a second approach.

Second, the introduction of HttpHandler, specifically to handle the picture loading request

The image control still points to a page URL through the ImageUrl property, but the page does not really exist. The request was referred to handler for processing.

1 < httphandlers > 2     <  verb= "*"  path= "imagevirtualsource.aspx"  type= " Imagebytesshowsample.imagevirtualsourcehandler "/>3</  httphandlers>

The work done in Imagevirtualsourcehandler is essentially the same as in the previous imagesource.aspx, mainly the output of the picture byte[] to the HTTP stream.

1  Public voidProcessRequest (HttpContext context)2 {3     varImgname = context. request.querystring["name"];4     varLightimg = Image.FromFile (string. Concat ("e:\\cspro\\imagebytesshowsample\\imagebytesshowsample\\", Imgname,". jpg"));5 6byte[] Imgbytes =imagetobytes (lightimg);7 8 context. Response.BinaryWrite (imgbytes);9}

Special attention:

If run with integrated environment (vs Debug Run),<system.web>

1 <system.webserver>2   <handlers>3     <Addname= "Virtualimagehandler"verb="*"Path= "Imagevirtualsource.aspx"type= "Imagebytesshowsample.imagevirtualsourcehandler"/>4   </handlers>5 </system.webserver>

Image control displays a picture that exists as a byte[] byte array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.