How to correctly display pictures in a database

Source: Internet
Author: User
Tags httpcontext
Data | database | Display file 1:showimage.aspx.cs


Namespace Imageresizing {

public class MainDisplay:System.Web.UI.Page {

public void Page_Load (System.Object sender, System.EventArgs e) {

try {

System.Int32 _imgid = System.Convert.ToInt32 (request.querystring["Imgid"));

System.Int32 _height = System.Convert.ToInt32 (request.querystring["height"));

System.Int32 _width = System.Convert.ToInt32 (request.querystring["width");

System.Data.SqlClient.SqlConnection Con = new System.Data.SqlClient.SqlConnection ("server=localhost;database= Northwind;trusted_connection=true ");

System.String SQLCMD = "SELECT * from Images WHERE imageID = @ImageID";

System.Data.SqlClient.SqlCommand sqlcmdobj = new System.Data.SqlClient.SqlCommand (SQLCMD, Con);

SQLCMDOBJ.PARAMETERS.ADD ("@ImageID", System.Data.SqlDbType.Int). Value = _imgid;

Con.open ();

System.Data.SqlClient.SqlDataReader Sqlreader = Sqlcmdobj.executereader ();

Sqlreader.read ();

System.Web.HttpContext.Current.Response.ContentType = "Image/pjpeg";

System.Drawing.Image _image = System.Drawing.Image.FromStream (New System.IO.MemoryStream ((byte[)) sqlreader["Image" ] ) );

System.Drawing.Image _newimage = _image. Getthumbnailimage (_width, _height, NULL, New System.IntPtr ());

_newimage. Save (System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

catch (System.Exception Ex) {

System.Web.HttpContext.Current.Trace.Write (Ex.Message.ToString ());

}

}

}

}


File 2: Display the picture, pass the QueryString
<body>


</body>



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.