OrE*_*lse 5 asp.net watermark image image-processing
我已经看到了关于使用 php 在图像上添加水印的很好的问题和答案
我也想做同样的事情,这次是使用 ASP.NET
所以这里有几个问题。
这是来自 codeproject 的另一个示例http://www.codeproject.com/KB/web-image/ASPImaging1.aspx,您可以对图像进行许多思考,包括从图像添加水印。
我认为这个过程需要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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想制作自定义句柄,则上面的代码是有效的,但您只需更改保存行。就像是。
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13646 次 |
| 最近记录: |