我使用下面的代码来调整图像的大小。现在,我需要使用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图像对象时,它应用水印。
您的代码不工作,因为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");
}
}