这里是另一个来自 codeproject 的示例 http://www.codeproject.com/KB/web-image/ASPImaging1.aspx,您可以对图像做很多思考,包括从图像中添加水印。
我认为这个过程是在 php 上使用 cpu 电源 ether 在 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();
}