C#mvc displaying a picture using a binary stream

Source: Internet
Author: User

The best way to display pictures is of course img SRC directly points to the address, simple and practical. But sometimes the way to display pictures using picture streams is also used. The implementation is also relatively simple (in C # MVC, Java does not understand). Specific as follows:

1. Front desk cshtml part

Adds an IMG tag whose address points back to the background of the method that returns the picture stream.

<span id= "__kindeditor_bookmark_ start_14__ "></span>

@Url. Action method has three parameters, method name, control name, method parameter (parameter type not written)

2. Background Control Section

<summary>///Local picture///</summary>///<returns></returns>public fileresult ShowImage (string ID) {    string _path = string. Concat (System.AppDomain.CurrentDomain.BaseDirectory, "content\\images\\longmao.png");    FileStream fs = new FileStream (_path, FileMode.Open);    byte[] Bydata = new Byte[fs. Length];    Fs. Read (bydata, 0, bydata.length);    Fs. Close ();    Return File (Bydata, "image/jpg");}

The return value type is fileresult, not our common actionresult, and the return is also return File (). About the different return types of MVC, you can learn about the Internet.

In particular, this kind of image display method is not recommended, the use of relatively more troublesome.

C#mvc displaying a picture using a binary stream

Related Article

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.