我使用以下代码调整图像大小。现在,我需要使用Magick.NET在此图像上应用水印。
var response = client.GetObject(request).ResponseStream;
MagickImage image = new MagickImage(response);
MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
size.IgnoreAspectRatio = maintainAspectRatio;
image.Resize(size);
Bitmap watermarkObj = (Bitmap)Bitmap.FromFile("G:/Images/watermark.png");
Graphics imageGraphics = Graphics.FromImage(image.ToBitmap());
Point point = new Point(image.Width - 118, image.Height - 29);
imageGraphics.DrawImage(watermarkObj, point);
image.write("G:/Images/ProcessedImage.JPG");
工作代码:
MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
size.IgnoreAspectRatio = maintainAspectRatio;
image.Resize(size);
Bitmap watermarkObj = (Bitmap)Bitmap.FromFile("G:/Images/watermark.png");
Bitmap objImg = new Bitmap("G:/Images/OriginalImage.jpg");
Graphics imageGraphics = Graphics.FromImage(objImg);
Point point = new Point(image.Width - 118, image.Height - 29);
imageGraphics.DrawImage(watermarkObj, point);
objImg.save("G:/Images/ProcessedImage.JPG");
那么,任何人都可以帮我使用imagemagick吗?原因是当我在图形中传递imageObject时,它不会应用水印,因为当我通过.net图像对象时,它会应用水印。
答案 0 :(得分:1)
您的代码无效,因为image.ToBitmap()
会创建新的位图。当您致电image.write("G:/Images/ProcessedImage.JPG");
时,您正在保存image
实例的未修改版本。您应该执行以下操作。
using (MagickImage image = new MagickImage(response))
{
MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
size.IgnoreAspectRatiomaintainAspectRatio;
image.Resize(size);
using (Bitmap watermarkObj = Bitmap)Bitmap.FromFile("G:/Images/watermark.png"))
{
using (Bitmap imageObj = image.ToBitmap())
{
using (Graphics imageGraphics = Graphics.FromImage(imageObj))
{
Point point = new Point(image.Width - 118, image.Height - 29);
imageGraphics.DrawImage(watermarkObj, point);
imageObj.Save("G:/Images/ProcessedImage.JPG");
}
}
}
}
另请注意,我添加了using
语句。在使用IDisposable
类时,您应该真的使用它。
您也可以在不使用System.Drawing
的情况下执行此操作。我在Magick.NET的文档中为此创建了一个新示例:https://magick.codeplex.com/wikipage?title=Watermark&referringTitle=Documentation
您可以在以下情况下使用以下代码:
using (MagickImage image = new MagickImage(response))
{
MagickGeometry size = new MagickGeometry(imgWidth, imgHeight);
size.IgnoreAspectRatiomaintainAspectRatio;
image.Resize(size);
using (MagickImage watermark = new MagickImage("G:/Images/watermark.png"))
{
image.Composite(watermark, image.Width - 118, image.Height - 29, CompositeOperator.Over);
image.Write("G:/Images/ProcessedImage.JPG");
}
}