我看到了关于使用PHP在图片上添加水印的好问题和答案。
这次我想用ASP.NET实现同样的功能。
所以这里有几个问题。
- 我该如何使用ASP.NET实现这个功能?
- 这个过程对服务器会造成很大负担吗?
- 我能使用图片作为水印而不是简单的文本吗?
我看到了关于使用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();
}
}
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();
}
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中的logo图像的相对位置。
GroupDocs.Watermark for .NET是一个强大且易于使用的API,可用于向图像文件添加文本和图像水印。以下是您可以使用几行代码添加水印的方法:
using (ImageDocument doc = Document.Load<ImageDocument>("D:\\image.jpeg"))
{
// Add text watermark
TextWatermark watermark = new TextWatermark("Protected Document", new Font("Arial", 8));
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.VerticalAlignment = VerticalAlignment.Center;
doc.AddWatermark(watermark);
// Add image watermark
ImageWatermark imageWatermark = new ImageWatermark("D:\\watermark.png");
doc.AddWatermark(imageWatermark);
// Save document
doc.Save("D:\\output.jpeg");
}
声明: 我在GroupDocs担任开发者大使。
虽然这是一篇旧文章,但可能有人正在寻找ASP.Net Core版本的文本/图像水印。
我刚刚为此创建了一个工具,可以从nuget下载:
PM> Install-Package LazZiya.ImageResize -Version 2.0.0
以下是添加水印图像的方法:
var img = Image.FromFile("wwwroot\\imags\\my-image.jpg");
var watermark = Image.FromFile("wwwroot\\images\\watermark.png");
img.ImageWatermark(watermark,
TargetSpot.TopRight, //spot to place the watermark
10, //margin from border
40); //opacity of image watermark
img.SaveAs("wwwroot\\images\\new-image.jpg");
该工具具有更多功能,如调整大小、裁剪和添加文字水印等。
在此处查看更多样例here