protected void Page_Load(object sender, EventArgs e)
{
byte[] byteImage = null;
string ID = Request.QueryString["id"];
string imageFile = "\\\\server\\c$\\inetpub\\wwwroot\\web-app\\images\\" + ID+ ".jpg";
string server = Request.ServerVariables["server_name"];
var serverNameList = db.Servers.Select(x => x.ServerName).ToList();
connection.Open();
SqlCommand sqlCommand = new SqlCommand("sp", connection);
sqlCommand.Parameters.Add("@ID", SqlDbType.VarChar);
sqlCommand.Parameters["@ID"].Value = ID;
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = sqlCommand.ExecuteReader();
while (dr.Read())
{
byteImage = (byte[])dr["BLOB"];
}
if (byteImage != null)
{
using (MemoryStream ms = new MemoryStream(byteImage))
{
//saving to jpg image
Image img = new Bitmap(ms);
img.Save(imageFile, ImageFormat.Jpeg);
Response.Clear();
Response.CacheControl = "public";
Response.Cache.SetExpires(DateTime.Now.AddDays(1.0));
Response.ContentType = "Image/jpeg";
if (serverNameList.Contains(server)) // if user is coming from a specific server, resize the image then send back
{
byte[] resizedImage = GetResizedImage(imageFile, 300, 400);
Response.OutputStream.Write(resizedImage, 0, resizedImage.Length);
}
else // skip resize and show image as jpg
{
Response.BinaryWrite(byteImage);
}
}
}
else // image is null
{
imageFile = HttpRuntime.AppDomainAppPath + "PERSON.GIF";
}
}
private byte[] GetResizedImage(string path, int width, int height)
{
Bitmap bitmap1 = new Bitmap(path);
double height1 = bitmap1.Height;
double width1 = bitmap1.Width;
double num = 1.0;
if (width > 0)
num = width / width1;
else if (height > 0)
num = height / height1;
MemoryStream memoryStream = new MemoryStream();
Bitmap bitmap2 = new Bitmap((int)(width1 * num), (int)(height1 * num));
bitmap2.SetResolution(150f, 150f);
Graphics graphics = Graphics.FromImage(bitmap2);
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.Clear(Color.White);
graphics.DrawImage(bitmap1, new Rectangle(0, 0, (int)(num * width1), (int)(num * height1)), new Rectangle(0, 0, (int)width1, (int)height1), GraphicsUnit.Pixel);
bitmap2.Save(memoryStream, ImageFormat.Jpeg);
return memoryStream.ToArray();
}
Became Hot Network Question