Original article address: Workshop. Put it in your own space for reference.
Store images in databases and display images using the image control
Access the database and display it on the pageThe functions of the image control. I thought it was not complicated, but encountered some problems in reality.
First, let's take a look at saving the image:
Images are stored in binary mode in the database. Therefore, you need to convert images into binary stream files and store them in the database,CodeAs follows:
Private VoidSave ()
{
MyimageIMG =New Myimage();
IMG. IMG = system. Drawing.Image. Fromstream (This. Fileupload1.postedfile. inputstream );
System. Io.MemorystreamMS =New Memorystream();
IMG. IMG. Save (MS, IMG. IMG. rawformat );
Sqlparameter[] Paras =New Sqlparameter[1];
Paras [0] =New Sqlparameter("@ IMG",Sqldbtype. Image, 2147483647 );
Paras [0]. value = Ms. toarray ();
SQL. dboption ("Insert into t_img (IMG) values (@ IMG )", Paras );
}
Let's take a look at reading files:
SqldataadapterAdapter =New Sqldataadapter("Select * From t_img where imgid ="+ Request. querystring ["Imgid"]. Tostring (), SQL. sqlconn );
DatasetDS =New Dataset();
Adapter. Fill (DS );
If(Ds. Tables [0]. Rows. Count> 0)
{
IMG. imgid =Convert. Toint32 (Ds. Tables [0]. Rows [0] ["Imgid"]);
Byte[] Bytes = (Byte[]) Ds. Tables [0]. Rows [0] ["IMG"];
Response. binarywrite (bytes );
}
FinallyThe image control displays:
I read a lot of materials online and use them.Response. binarywrite (bytes), but this can only overwrite the page, so that only images are displayed on the page. However, in actual projects, we may want to display images at a specific position, or in system. web. UI. webcontrols. the image control. At first, I had a misunderstanding that I wanted to put the system. Drawing. image object into the webcontrols. Image control. In fact, it cannot be converted directly. Many people on the Internet propose to create a page to display only images, and then pass the address of this page to image1.imageurl. This is obviously successful, but I am not satisfied with it. There is a redundant page, which can be solved using httphandler.
InAdd an httphandler to the Web. config file.
<System. Web>
httphandlers
Add verb = " * " path =" imgpage1.aspx " type =" imgpagehandler " />
</Httphandlers>
</System. Web>
InApp_codeImgpagehandlerType, and make it inherit fromIhttphandler, And rewriteThe isreusable attribute and processrequest method are as follows:
Public BoolIsreusable
{
Get{Return False;}
}
Public VoidProcessrequest (HttpcontextContext)
{
MyimageIMG =New Myimage();
Try
{
SQL. dbconnect ();
SqldataadapterAdapter =New Sqldataadapter("Select * From t_img where imgid ="+ Context. Request. querystring ["Imgid"]. Tostring (), SQL. sqlconn );
DatasetDS =New Dataset();
Adapter. Fill (DS );
If(Ds. Tables [0]. Rows. Count> 0)
{
IMG. imgid =Convert. Toint32 (Ds. Tables [0]. Rows [0] ["Imgid"]);
Byte[] Bytes = (Byte[]) Ds. Tables [0]. Rows [0] ["IMG"];
Context. response. binarywrite (bytes );
}
}
Catch(ExceptionEx)
{
ThrowEx;
}
Finally
{
SQL. dbclose ();
}
}
On the page where the image is to be displayed, give the virtual page addressImage1.imageurl
This. Image1.imageurl ="Imgpage1.aspx? Imgid ="+This. Textbox1.text;