Binary Image Database

Source: Internet
Author: User
// Upload the image file to the image
System. Drawing. Image img = System. Drawing. image. fromfile (fileimg. postedfile. filename );

// Save to memory in JPEG format
MS for system. Io. memorystream =   New System. Io. memorystream ();
IMG. Save (MS, system. Drawing. imaging. imageformat. JPEG );

// Output Response. binarywrite (Binary)
Response. clearcontent ();
Response. contenttype =   " Image/JPEG " ;
Response. binarywrite (Ms. toarray ());

IMG. Dispose ();
Ms. Dispose ();
Ms. Flush ();
Bytes -------------------------------------------------------------------------------------
// Upload the image file to the image
System. Drawing. Image img = System. Drawing. image. fromfile (fileimg. postedfile. filename );

// Save to memory in JPEG format
MS for system. Io. memorystream =   New System. Io. memorystream ();
IMG. Save (MS, system. Drawing. imaging. imageformat. JPEG );

Using (Sqlconnection con =   New Sqlconnection ( " Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ database. MDF; Integrated Security = true; user instance = true " ))
{
Sqlcommand cmd =   New Sqlcommand ( " Spinsertimage " , Con );
Cmd. commandtype = Commandtype. storedprocedure;

Cmd. Parameters. Add ( " @ Imagebinary " , System. Data. sqldbtype. Image );
// Binary data of an image
Cmd. Parameters [ " @ Imagebinary " ]. Value = Ms. toarray ();

Con. open ();
Try
{
//Procedure
Cmd. executenonquery ();
Response. Write ("OK");
}
Catch
{
Response. Write ("Error");
}
}

IMG. Dispose ();
Ms. Dispose ();
Ms. Flush ();
------------------------------------------------------------------------------
Using (Sqlconnection con =   New Sqlconnection ( " Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ database. MDF; Integrated Security = true; user instance = true " ))
{
Sqlcommand cmd =   New Sqlcommand ( " Spselectimage " , Con );
Cmd. commandtype = Commandtype. storedprocedure;

Cmd. Parameters. Add ( " @ Imageid " , Sqldbtype. INT );
// Imageid selected in dropdownlist
Cmd. Parameters [ " @ Imageid " ]. Value = Int32.parse (ddlimage. selectedvalue );

Con. open ();
Sqldatareader SDR = Cmd. executereader ();

Response. clearcontent ();
Response. contenttype =   " Image/JPEG " ;
While (SDR. Read ())
{
//Read and display the binary data of the corresponding record
Response. binarywrite ((Byte[]) SDR [0]);
}

SDR. Close ();
SDR. Dispose ();
}

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.