using System.Drawing.Imaging;
using System.Drawing;
// 兩種方式,長邊為主=750,短邊補齊空白邊
// 短邊為主=750,長邊(盡量)取750長度
// getSubDraw(路徑, 檔名)
protected void getSubDraw1(string url, string fileName)
{
int fixW = 0, fixH=0, maxPx=750;
double dbT = 0.0;
string uri = "", sName="";
uri = Server.MapPath("~/" + url);
System.Drawing.Image imageT = System.Drawing.Image.FromFile(uri);
ImageFormat thisFormat = imageT.RawFormat;
if (imageT.Width > imageT.Height)
{
fixW = maxPx;
fixH = Convert.ToInt32((Convert.ToDouble(fixW) / Convert.ToDouble(imageT.Width)) * Convert.ToDouble(imageT.Height));
}
else if (imageT.Width < imageT.Height)
{
fixH = maxPx;
fixW = Convert.ToInt32((Convert.ToDouble(fixH) / Convert.ToDouble(imageT.Height)) * Convert.ToDouble(imageT.Width));
}
else
{
fixH = maxPx;
fixW = maxPx;
}
Bitmap imageOutput = new Bitmap(imageT, fixW, fixH);
Bitmap imageBG = new Bitmap(750, 750);
Brush bb = new SolidBrush(Color.White);
//暫存圖層
Graphics g;
//將圖片加入畫布
g = Graphics.FromImage(imageBG);
g.FillRectangle(bb,0,0,750,750);
g.DrawImage(imageBG, 0, 0);
if (fixW > fixH)
{
dbT = ((fixW - fixH) / 2);
g.DrawImage(imageOutput, 0, (float)dbT);
}
else if (fixH > fixW)
{
dbT = ((fixH - fixW) / 2);
g.DrawImage(imageOutput, (float)dbT, 0);
}
else
{ g.DrawImage(imageOutput, 0, 0); }
sName = fileName.Replace(".jpg","s.jpg");
imageBG.Save(string.Concat(Server.MapPath("~/image/"), sName), thisFormat);
//將修改過的圖存於設定的位子
imageOutput.Dispose();
//釋放記憶體
imageT.Dispose();
//釋放掉圖檔
}
protected void getSubDraw2(string url, string fileName)
{
int fixW = 0, fixH = 0, maxPx = 750;
double dbT = 0.0;
string uri = "", sName = "";
uri = Server.MapPath("~/" + url);
System.Drawing.Image imageT = System.Drawing.Image.FromFile(uri);
ImageFormat thisFormat = imageT.RawFormat;
if (imageT.Width < imageT.Height)
{
fixW = maxPx;
fixH = Convert.ToInt32((Convert.ToDouble(fixW) / Convert.ToDouble(imageT.Width)) * Convert.ToDouble(imageT.Height));
}
else if (imageT.Width > imageT.Height)
{
fixH = maxPx;
fixW = Convert.ToInt32((Convert.ToDouble(fixH) / Convert.ToDouble(imageT.Height)) * Convert.ToDouble(imageT.Width));
}
else
{
//fixH = imageT.Height;
//fixW = imageT.Width;
fixH = maxPx;
fixW = maxPx;
}
Bitmap imageOutput = new Bitmap(imageT, fixW, fixH);
Bitmap imageBG = new Bitmap(750, 750);
Brush bb = new SolidBrush(Color.White);
//暫存圖層
Graphics g;
//將圖片加入畫布
g = Graphics.FromImage(imageBG);
g.FillRectangle(bb, 0, 0, 750, 750);
g.DrawImage(imageBG, 0, 0);
if (fixW > fixH)
{
dbT = ((fixW - fixH) / 2);
g.DrawImage(imageOutput, -(float)dbT, 0);
}
else if (fixH > fixW)
{
dbT = ((fixH - fixW) / 2);
g.DrawImage(imageOutput, 0, -(float)dbT);
}
else
{ g.DrawImage(imageOutput, 0, 0); }
sName = fileName.Replace(".jpg", "s.jpg");
//imageOutput.Save(string.Concat(Server.MapPath("~/img/draw/"), sName), thisFormat);
//imageBG.Save(string.Concat(Server.MapPath("~/img/draw/"), fileName.Replace(".jpg", "B.jpg")), thisFormat);
imageBG.Save(string.Concat(Server.MapPath("~/img/draw/"), sName), thisFormat);
//將修改過的圖存於設定的位子
imageOutput.Dispose();
//釋放記憶體
imageT.Dispose();
//釋放掉圖檔
}