demoshop

demo, trying to be the best_

之前demo介紹過如何將圖片縮圖的方式,現在我們再加上浮水印的功能,讓你可以應用在更多的地方。

★注意事項:此篇範例假設您已經製作好相關上傳方式,並且對於程式有一定程度,縮圖部份不做註解,如您還不熟悉可以至利用C#來做到圖檔縮圖的方式觀看。

▲直接就丟code吧 

System.Drawing.Image watermarkImage = System.Drawing.Image.FromFile(Server.MapPath("~/googledemo.gif"));
//作為浮水印的圖檔
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("~/000.jpg"));
ImageFormat thisFormat = image.RawFormat;
int fixWidth = 0;
int fixHeight = 0;
int maxPx = 1000;
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);
//宣告出一個GDI
gra.DrawImage(watermarkImage, new Rectangle(imageOutput.Width - watermarkImage.Width, imageOutput.Height - watermarkImage.Height, imageOutput.Width, imageOutput.Height), 0, 0, imageOutput.Width, imageOutput.Height, GraphicsUnit.Pixel);
//重新繪製被縮的圖並加浮水印上去
string fixSaveName = string.Concat("新圖的檔案名稱", ".jpg");
imageOutput.Save(string.Concat(Server.MapPath("~/"), fixSaveName), thisFormat);
imageOutput.Dispose();
image.Dispose();
gra.Dispose(); 

回應討論