我有一组图像,我正在按照此处System.Windows
的教程使用和System.Windows.Media.Imaging
(是的,不是使用 GDI+)以编程方式在它们上绘制一个简单的水印。
大多数图像不超过500Kb,但在应用简单的水印(即透明背景的文本)后,图像大小急剧增加。
例如,一张440Kb的图片在用下面的方法加水印后变成了8.33MB,这让我很震惊。
private static BitmapFrame ApplyWatermark(BitmapFrame image, string waterMarkText) {
const int x = 5;
var y = image.Height - 20;
var targetVisual = new DrawingVisual();
var targetContext = targetVisual.RenderOpen();
var brush = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFFFFF"));
brush.Opacity = 0.5;
targetContext.DrawImage(image, new Rect(0, 0, image.Width, image.Height));
targetContext.DrawRectangle(brush, new Pen(), new Rect(0, y, image.Width, 20));
targetContext.DrawText(new FormattedText(waterMarkText, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface("Batang"), 13, Brushes.Black), new Point(x, y));
targetContext.Close();
var target = new RenderTargetBitmap((int)image.Width, (int)image.Height, 96, 96, PixelFormats.Default);
target.Render(targetVisual);
var targetFrame = BitmapFrame.Create(target);
return targetFrame;
}
我注意到与原始图像相比,图像质量有所提高。图像更平滑,颜色更浅。但是,你知道我真的不想要这个。我希望图像保持原样,但要包含水印。质量没有提高,当然图像尺寸也没有剧烈变化。
我在这里是否缺少任何设置来告诉我的程序保持与源图像相同的质量?如何防止我的ApplyWatermark
方法更改后图像大小的显着变化?
编辑
1.这就是我转换BitmapFrame
为Stream
. 然后我用它将Stream
图像保存到 AmazonS3
private Stream EncodeBitmap(BitmapFrame image) {
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(image));
var memoryStream = new MemoryStream();
enc.Save(memoryStream);
return memoryStream;
}
2.这就是我得到的BitmapFrame
方式Stream
private static BitmapFrame ReadBitmapFrame(Stream stream) {
var photoDecoder = BitmapDecoder.Create(
stream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.None);
return photoDecoder.Frames[0];
}
3.这就是我从本地目录读取文件的方式
public Stream FindFileInLocalImageDir() {
try {
var path = @"D:\Some\Path\Image.png";
return !File.Exists(path) ? null : File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
} catch (Exception) {
return null;
}
}