using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using Microsoft.Data.SqlXml;
using System.IO;
public partial class GetImage : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
string employeeID = Request.QueryString[ "EmployeeID"];
if (employeeID == string.Empty || employeeID == null)
{
return;
}
byte[] imgByteArray = GetImage2(employeeID);
Response.ContentType = "image/jpeg";
int offset = 78;
//读取图片并作图片格式转换,原图是BMP图,在网页中显示使用JPG图片,同时使图片感官效果优化:
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
mstream.Write(imgByteArray, offset, imgByteArray.Length - offset);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(mstream);
Bitmap bmpTmp = new Bitmap(bmp.Width / 2, bmp.Height / 2); //尺寸缩小为50%
Graphics g = Graphics.FromImage(bmpTmp);
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle destRect = new Rectangle(0, 0, bmpTmp.Width, bmpTmp.Height);
Rectangle srcRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
g.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
bmpTmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
bmp.Dispose();
byte[] byteReturn = stream.ToArray();
mstream.Close();
stream.Close();
Response.OutputStream.Write(byteReturn, 0, byteReturn.Length);
Response.End();
}
private byte[] GetImage2( string employeeID)
{
string NorthwindConnstring;
NorthwindConnstring = "data source=(local);database=Nwind;uid=SA;pwd=password";
using (SqlConnection conn = new SqlConnection(NorthwindConnstring))
{
string sqlSelect = "SELECT Photo FROM Emp WHERE EmployeeID = " + employeeID;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sqlSelect;
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow))
{
if (dr.Read())
{
if (dr[0] != null)
{
byte[] byteImg = ( byte[])dr[0];
return byteImg;
}
}
}
}
return null;
}
}