我正在尝试将水印添加到图像并以最佳质量保存,但是保存 JPEG 水印时质量很差(但主图像的质量很好)。
string wtrSrc = @"D:\watermark.png";
string imgSrc = @"D:\image.jpg";
string imgOutJPG = @"D:\result.jpg";
string imgOutPNG = @"D:\result.png";
// create new image
Bitmap imgOutput = new Bitmap(imgSrc);
Graphics outputGraphics = Graphics.FromImage(imgOutput);
// image quality
outputGraphics.CompositingQuality = CompositingQuality.HighQuality;
outputGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
outputGraphics.SmoothingMode = SmoothingMode.HighQuality;
outputGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
// create watermark image
System.Drawing.Image wtrmark = System.Drawing.Image.FromFile(wtrSrc);
// add waternark
float wtrmarkX = 10;
float wtrmarkY = 10;
outputGraphics.DrawImage(wtrmark, wtrmarkX, wtrmarkY);
//set jpeg quality
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
// save result
imgOutput.Save(imgOutJPG, GetEncoder(ImageFormat.Jpeg), myEncoderParameters);
imgOutput.Save(imgOutPNG, ImageFormat.Png);
// clean
wtrmark.Dispose();
imgOutput.Dispose();
outputGraphics.Dispose();
……
// ImageCodecInfo
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
PNG保存效果很好,但我需要保存JPEG。这是PNG和JPEG之间的区别 http://i.stack.imgur.com/CqwgZ.jpg
我按照这篇文章http://www.codeproject.com/Articles/2927/Creating-a-Watermarked-Photograph-with-GDI-for-NET得到了相同的结果。
有没有办法让 JPEG 上的水印看起来不错?