Asp.net reads images from the database

Source: Internet
Author: User

/**/ ///   <Summary>
/// Processed to obtain the thumbnail
///   </Summary>
///   <Param name = "Ms"> </param>
///   <Param name = "height"> </param>
///   <Returns> </returns>
Private   Byte [] Lessonpicautowidth (memorystream MS, Int Height)
{
Byte [] Chagedbyte;
System. Drawing. Image originalimage = System. Drawing. image. fromstream (MS, True );
System. Drawing. Bitmap originalpic, newpic;
Originalpic =   New System. Drawing. Bitmap (originalimage );

Int Width = Height * Originalpic. Width / Originalpic. height;

If (Width >   280 )
{
Width= 280;
}

Newpic =   New Bitmap (originalpic, width, height );
Memorystream newms =   New Memorystream ();
Newpic. Save (newms, system. Drawing. imaging. imageformat. JPEG );
Chagedbyte = Newms. getbuffer ();
Originalpic. Dispose ();
Newpic. Dispose ();
Newms. Close ();

Return Chagedbyte;
}

/**//// <Summary>
///Show Image
/// </Summary>
/// <Param name = "employeeid"> </param>
Public   Void Getphoto ( String Employeeid, Int Height)
{
Bitmap BMP;
Byte [] Photobyte;
String Cnstr;
String SQL;
Cnstr =   " Data Source = localhost; database = northwind; uid = sa; Pwd = 123 " ;
SQL =   " Select photo from employees where emplyeeid = "   + Employeeid;

Using (Sqlconnection CN =   New Sqlconnection (cnstr ))
{
Using (Sqlcommand cmd = CN. createcommand ())
{
CN. open ();
Cmd. commandtext = SQL;
Using (Sqldatareader Reader = Cmd. executereader (commandbehavior. sequentialaccess ))
{
Reader. Read ();
Photobyte=(Byte[]) Reader. getvalue (0);
}
}
}
If (Photobyte =   Null   | Photobyte. Length =   0 ) Return ;

Memorystream tempstream= NewMemorystream (photobyte );
Photobyte = Lessonpicautowidth (tempstream, height );
BMP =   New Bitmap (tempstream );

Response. contenttype =   " Image/GIF " ;
BMP. Save (response. outputstream, system. Drawing. imaging. imageformat. GIF );
Response. End ();
}

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.