Model [.CS]
[HttpPost]
public int WebUplaodFile()
{
var upfile = Request.Files["myFile"]; ;
if (upfile != null)
{
new ImgService().ImgMarkImg(Server.MapPath("~/Image"), upfile);
}
return 1;
}
Service [.CS]
public void ImgMarkImg(string spath, HttpPostedFileBase myfile)
{
//浮水印檔案路徑
string MarkImgPath = spath + "/imgmark.png";
//暫存檔路徑
string tmpPath = spath + "/Temp/tmp" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";
myfile.SaveAs(tmpPath);
//作為浮水印的圖檔
System.Drawing.Image watermarkImage = System.Drawing.Image.FromFile(MarkImgPath);
//加浮水印
System.Drawing.Image image = System.Drawing.Image.FromFile(tmpPath);
ImageFormat thisFormat = image.RawFormat;
//縮圖
int fixWidth = 0;
int fixHeight = 0;
int maxPx = 700; //可依浮水印大小作上限依據
if (image.Width > maxPx || image.Height > maxPx)
{
if (image.Width >= image.Height)
{
fixWidth = maxPx;
fixHeight = Convert.ToInt32((Convert.ToDouble(fixWidth) / Convert.ToDouble(image.Width)) * Convert.ToDouble(image.Height));
}
else
{
fixHeight = maxPx;
fixWidth = Convert.ToInt32((Convert.ToDouble(fixHeight) / Convert.ToDouble(image.Height)) * Convert.ToDouble(image.Width));
}
}
else
{
fixHeight = image.Height;
fixWidth = image.Width;
}
//匯出縮圖
Bitmap imageOutput = new Bitmap(image, fixWidth, fixHeight);
//取得畫布
Graphics gra = Graphics.FromImage(imageOutput);
//重新繪製縮圖並加浮水印
gra.DrawImage(watermarkImage, new Rectangle((imageOutput.Width - watermarkImage.Width) / 2, (imageOutput.Height - watermarkImage.Height) / 2, imageOutput.Width, image.Height), 0, 0, imageOutput.Width, imageOutput.Height, GraphicsUnit.Pixel);
imageOutput.Save(spath + "/Img/" + DateTime.Now.ToString("yyyyMMdd") + ".jpg", thisFormat);
imageOutput.Dispose();
image.Dispose();
gra.Dispose();
//刪除暫存
File.Delete(tmpPath);
}
[HttpPost]
public int WebUplaodFile()
{
var upfile = Request.Files["myFile"]; ;
if (upfile != null)
{
new ImgService().ImgMarkImg(Server.MapPath("~/Image"), upfile);
}
return 1;
}
Service [.CS]
public void ImgMarkImg(string spath, HttpPostedFileBase myfile)
{
//浮水印檔案路徑
string MarkImgPath = spath + "/imgmark.png";
//暫存檔路徑
string tmpPath = spath + "/Temp/tmp" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";
myfile.SaveAs(tmpPath);
//作為浮水印的圖檔
System.Drawing.Image watermarkImage = System.Drawing.Image.FromFile(MarkImgPath);
//加浮水印
System.Drawing.Image image = System.Drawing.Image.FromFile(tmpPath);
ImageFormat thisFormat = image.RawFormat;
//縮圖
int fixWidth = 0;
int fixHeight = 0;
int maxPx = 700; //可依浮水印大小作上限依據
if (image.Width > maxPx || image.Height > maxPx)
{
if (image.Width >= image.Height)
{
fixWidth = maxPx;
fixHeight = Convert.ToInt32((Convert.ToDouble(fixWidth) / Convert.ToDouble(image.Width)) * Convert.ToDouble(image.Height));
}
else
{
fixHeight = maxPx;
fixWidth = Convert.ToInt32((Convert.ToDouble(fixHeight) / Convert.ToDouble(image.Height)) * Convert.ToDouble(image.Width));
}
}
else
{
fixHeight = image.Height;
fixWidth = image.Width;
}
//匯出縮圖
Bitmap imageOutput = new Bitmap(image, fixWidth, fixHeight);
//取得畫布
Graphics gra = Graphics.FromImage(imageOutput);
//重新繪製縮圖並加浮水印
gra.DrawImage(watermarkImage, new Rectangle((imageOutput.Width - watermarkImage.Width) / 2, (imageOutput.Height - watermarkImage.Height) / 2, imageOutput.Width, image.Height), 0, 0, imageOutput.Width, imageOutput.Height, GraphicsUnit.Pixel);
imageOutput.Save(spath + "/Img/" + DateTime.Now.ToString("yyyyMMdd") + ".jpg", thisFormat);
imageOutput.Dispose();
image.Dispose();
gra.Dispose();
//刪除暫存
File.Delete(tmpPath);
}
留言
張貼留言