using System;
using System.Drawing;
using YapiYardim.Common;
public partial class Tools_ImageResize : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string uri;
string URI;
short? newWidth = null;
short? newHeight = null;
short? maxWidth = null;
short? maxHeight = null;
if (Request.QueryString["width"] != null)
{
newWidth = Common.ToNullableInt16(Request.QueryString["width"]);
}
if (Request.QueryString["height"] != null)
{
newHeight = Common.ToNullableInt16(Request.QueryString["height"]);
}
if (Request.QueryString["maxwidth"] != null)
{
maxWidth = Common.ToNullableInt16(Request.QueryString["maxwidth"]);
}
if (Request.QueryString["maxheight"] != null)
{
maxHeight = Common.ToNullableInt16(Request.QueryString["maxheight"]);
}
uri = Convert.ToString(Request.QueryString["uri"]);
if (!uri.StartsWith("~/"))
{
uri = "~/" + uri;
}
Response.ContentType = "image/jpg";
URI = Server.MapPath(uri);
try // TODO: uri geçersizse
{
using (Bitmap img = new Bitmap(URI))
{
short resultWidth = (short)img.Width;
short resultHeight = (short)img.Height;
if (maxWidth > 0 && img.Width > maxWidth || maxHeight > 0 && img.Height > maxHeight)
{
decimal? widthRatio = null;
decimal? heightRatio = null;
decimal? ratio = null;
if (maxWidth > 0 && img.Width > maxWidth) // küçültme işlemi gerekliyse
widthRatio = (decimal)img.Width / maxWidth.Value;
if (maxHeight > 0 && img.Height > maxHeight)
heightRatio = (decimal)img.Height / maxHeight.Value;
if (maxHeight > 0 && img.Height / widthRatio > maxHeight) // maxheight aşılıyor
{
ratio = heightRatio;
}
else
{
ratio = widthRatio;
}
resultWidth = (short)(img.Width / ratio);
resultHeight = (short)(img.Height / ratio);
}
else
{
// küçültme işlemi gereksiz, imaj zaten yeteri kadar küçük
}
System.IO.Stream wClient = new System.Net.WebClient().OpenRead(URI);
System.Drawing.Bitmap DestImage = new System.Drawing.Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Graphics.FromImage(DestImage).DrawImage(System.Drawing.Image.FromStream(wClient), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
using (System.Drawing.Bitmap imgOutput = new System.Drawing.Bitmap(DestImage, resultWidth, resultHeight))
{
Graphics myresizer;
myresizer = Graphics.FromImage(imgOutput);
myresizer.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
myresizer.DrawImage(DestImage, 0, 0, resultWidth, resultHeight);
imgOutput.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
imgOutput.Dispose();
wClient.Close();
wClient.Dispose();
}
}
}
catch
{
}
}
}