我看到了有关 adding watermark on images with php 的精彩问题和答案
我也想用 ASP.NET 做同样的事情
这里有几个问题。
- 如何使用 ASP 做到这一点?
- 此过程是否会导致服务器严重重载?
- 我可以使用图像代替简单的文本作为水印吗?
最佳答案
这是另一个示例 http://www.codeproject.com/KB/web-image/ASPImaging1.aspx从 codeproject 可以看出,您可以对图像进行很多思考,包括从图像添加水印。
我认为这个过程需要CPU电源,以太在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();
}
关于ASP.NET:动态添加 'Watermark' 到图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5457691/