我所看到的關於adding watermark on images with phpASP.NET:添加「水印」到圖像上飛
我願做同樣的重大問題和答案,這一次與ASP.NET
因此,這裏有幾個問題。
- 我該如何做到這一點與ASP?
- 這個過程是否會對服務器造成巨大的負擔?
- 我可以使用圖像的水印,而不是簡單的文字?
我所看到的關於adding watermark on images with phpASP.NET:添加「水印」到圖像上飛
我願做同樣的重大問題和答案,這一次與ASP.NET
因此,這裏有幾個問題。
下面是來自codeproject的另一個示例http://www.codeproject.com/KB/web-image/ASPImaging1.aspx,您可以對圖像執行多種操作,包括從圖像添加水印。
我覺得這個過程就是取得cpu的功率ether是在php上,以太網在asp.net上。所以這種作品必須有一個圖像緩存模式。
這裏是一些基本的代碼。在這段代碼中,你必須改變水印的位置和圖像的大小。水印可以是透明的png圖像。
public void MakePhoto(...parametres...)
{
Bitmap outputImage = null;
Graphics g = null;
try
{
// the final image
outputImage = new Bitmap(OutWidth, OutHeight, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(outputImage);
g.CompositingMode = CompositingMode.SourceCopy;
Rectangle destRect = new Rectangle(0, 0, OutWidth, OutHeight);
// the photo
using (var BasicPhoto = new Bitmap(cBasicPhotoFileOnDisk))
{
g.DrawImage(BasicPhoto, destRect, 0, 0, BasicPhoto.Width, BasicPhoto.Height, GraphicsUnit.Pixel);
}
g.CompositingMode = CompositingMode.SourceOver;
// the watermark
using (var WaterMark = new Bitmap(cWaterMarkPhotoOnDisk))
{
Rectangle destWaterRect = new Rectangle(0, 0, OutWidth, OutHeight);
g.DrawImage(WaterMark, destWaterRect, 0, 0, OutWidth, OutHeight, GraphicsUnit.Pixel);
}
outputImage.Save(TheFileNameTosaveIt, ImageFormat.Jpeg);
}
catch (Exception x)
{
Debug.Assert(false);
... log your error, and send an error image....
}
finally
{
if (outputImage != null)
outputImage.Dispose();
if (g != null)
g.Dispose();
}
}
如果您希望自定義句柄,上面的代碼是站立的,但您只更改保存行。就像是。
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "image/jpeg";
// add you cache here
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(200));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 200, 0));
context.Response.BufferOutput = false;
..... the above code....
outputImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
..... the above code....
context.Response.End();
}
是的,你可以通過使用GDI+,在圖像使用DrawString(),然後將其保存或恢復它作爲響應做到這一點。
您需要使用HTTPModule
,如ASP.NET Watermarker Module文章中所述。
在post I made中,有一個使用WPF而不是舊的,棄用的GDI +爲圖像上的文本加水印的示例。
正如你在文章中看到的那樣,文本是通過使用DrawingContext的DrawText方法添加的,真的很容易使用DrawImage來代替,它接受一個BitmapImage。
的東西,如:
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.CacheOption = BitmapCacheOption.OnLoad;
logo.UriSource = new Uri(your_physical_logopath);
logo.EndInit();
Rect rect = new Rect(0, 0, (double)logo.PixelWidth, (double)logo.PixelHeight);
dc.DrawImage(logo, rect);
隨着rect.X和rect.Y,你執行的DrawImage(前),你可以修改的DrawingContext內的標誌圖像的相對位置。
您提供的鏈接已損壞。 – 2015-11-02 11:34:59
[我的開源imageresizing.net(http://imageresizing.net)項目允許您定義多個圖片和文字層(或他們的羣體)作爲水印使用。完全XML配置,請參閱[文檔頁面](http://imageresizing.net/plugins/watermark)。您可以通過將圖片添加到圖片網址來引用水印,如image.jpg?watermark = logo。您可以使用「Pipeline.Rewrite」事件處理程序輕鬆實施水印;您想要應用的任何規則都可用。而且,它兼容ASP.NET和ASP,因爲它具有URL API。 – 2011-12-09 07:43:03