I want to implement a requirement that the image control dynamically loads the picture under the path in the database, where the absolute path of the photo is stored (which can not be on the system's path).
See this topic, some people will say, this is not simple ah, set the URL directly to the absolute path is not ok ah. I can only say that, if you say so, then you can only say that you did not think, or even, did not understand the web development of the foreground code and background code in the end what is meant, but this practice, in their own time (not to IIS), Sogou browser can display pictures (only this one can show, So there is no point in this practice.
The image control is in the System.Web.UI.WebControls named control, so you cannot display the picture directly, as in WinForm, through byte[] (the individual is a little bit confused about this, and who knows what to explain). The method found is to take the picture and then write the read binary stream to a page, and then set the URL of image to this page. Here is my demo implementation code.
Foreground code:
Copy Code code as follows:
<title></title>
<script src= "Jquery-1.7.1.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
function Showp (obj) {
$ ("#Image1") [0].SRC = showpic.aspx? Url= "+ obj.id;
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<input id= "e:\\1.jpg" type= "button" value= "button" onclick= "SHOWP (this);"/>
<asp:image id= "Image1" runat= "Server"/>
</div>
</form>
</body>
The foreground code for showpic.aspx is empty, and the following is the background code for showpic.aspx:
Copy Code code as follows:
public partial class ShowPic:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
#region "Convert a photo to a binary array based on a photo path"
String strURL = request.querystring["URL"];
Read files in binary mode
FileStream afile = new FileStream (strURL, FileMode.OpenOrCreate, FileAccess.ReadWrite);
Create a binary data Flow reader, associating with open files
BinaryReader brmyfile = new BinaryReader (afile);
Reposition the file pointer to the beginning of the file
BrMyfile.BaseStream.Seek (0, Seekorigin.begin);
Get a byte array of photos
byte[] photo = Brmyfile.readbytes (Convert.ToInt32 (aFile.Length.ToString ()));
Close each object of the above new
Brmyfile.close ();
#endregion
Response.BinaryWrite (photo);
}
}
In the above I put the path of the photo to the button ID, and then the path as a parameter to invoke Showpic.aspx, in the Showpic code, the image as a two-dimensional array of the form of the page, if you open the page directly, will display garbled.
The URL of image is then set to this page, and a picture is displayed. I implemented a dynamic load through the button ID picture, to the specific application, there are a little bit of reference to the problem of JavaScript, this is still need to do an example, through JavaScript debugging tools, to understand the DOM structure, and then operate.