图像尺寸应用简单的水印后大幅提高 [英] Image size is drastically increasing after applying a simple watermark

查看:241
本文介绍了图像尺寸应用简单的水印后大幅提高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组我对他们使用编程方式绘制一个简单的水印图像 System.Windows System.Windows.Media .Imaging 通过遵循的此处



大部分的图像是不超过 500KB ,但应用简单的水印,这是一个透明背景的文本后,图像更规模大幅提高。



例如,一个 440KB 图片正在成为 8.33MB 与下面的方法,并采用水印后。令人震惊的我

 私有静态BitmapFrame ApplyWatermark(BitmapFrame形象,串waterMarkText){
const int的X = 5;
变种Y = image.Height - 20;
变种targetVisual =新DrawingVisual();
VAR targetContext = targetVisual.RenderOpen();
VAR刷=(的SolidColorBrush)(新BrushConverter()ConvertFrom(#FFFFFF));
brush.Opacity = 0.5;
targetContext.DrawImage(图象,新的Rect(0,0,image.Width,image.Height));
targetContext.DrawRectangle(毛笔,钢笔新(),新的矩形(0,Y,image.Width,20));
targetContext.DrawText(新FormattedText(waterMarkText,CultureInfo.CurrentCulture,FlowDirection.LeftToRight,
新的字体(巴塘),13,Brushes.Black),新点(x,y));
targetContext.Close();
VAR的目标=新RenderTargetBitmap((INT)image.Width,(INT)image.Height,96,96,PixelFormats.Default);
target.Render(targetVisual);
VAR targetFrame = BitmapFrame.Create(目标);
返回targetFrame;
}



我注意到图象质量比原始图像相比提高。图像更平滑,颜色更浅。但是,你知道,我真的不希望这样。我想要的形象是因为它是,但包括水印。没有质量的增加,并在图像尺寸当然也不剧烈变化。



有没有办法,我缺少这里要告诉我的计划,以确保质量为相同的任何设置作为源图像?如何防止图像大小在我的 ApplyWatermark 方法更改后显著变化?



修改



1 这是我如何转换 BitmapFrame 。然后我用保存图像AmazonS3

 专用流EncodeBitmap(BitmapFrame图片){
的BitmapEncoder ENC =新BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(图片));
变种的MemoryStream =新的MemoryStream();
enc.Save(MemoryStream的);
返回MemoryStream的;
}



2 这就是我得到的 BitmapFrame

 私有静态BitmapFrame ReadBitmapFrame(流流){
VAR photoDecoder = BitmapDecoder.Create(
流,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.None);
返回photoDecoder.Frames [0];
}



3。这是我读文件从本地目录

 公共流FindFileInLocalImageDir(){
尝试{
VAR路径= @D: \Some\Path\Image.png;
的回报!File.Exists(路径)?空:File.Open(路径,FileMode.Open,FileAccess.Read,FileShare.Read);
}赶上(例外){
返回NULL;
}
}


解决方案

的我之所以问我的问题在前面的评论,是因为我发现有几个可行的不同的编码器。位图通常有显著较大的文件大小,由于它存储有关图像的信息量。



我没有测试此我自己,但你有没有试过?不同的编码器

  VAR包括PNGEncoder =新PngBitmapEncoder(); 
pngEncoder.Frames.Add(ApplyWatermark(NULL,NULL));

MemoryStream的STM = File.Create(图片);
pngEncoder.Save(STM);
返回STM;


I've a set of images that I'm programmatically drawing a simple watermark on them using System.Windows and System.Windows.Media.Imaging (yes, not with GDI+) by following a tutorial in here.

Most of the images are not more than 500Kb, but after applying a simple watermark, which is a text with a transparent background, the image size is drastically increasing.

For example, a 440Kb image is becoming 8.33MB after applying the watermark with the below method, and that is shocking me.

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;
}

I've noticed that the image quality is improved compared than the original image. The image is more smoother and colors are more lighter. But, you know I don't really want this. I want the image to be as it is, but include the watermark. No quality increases, and of course no drastic changes in image size.

Is there any settings that I'm missing in here to tell my program to keep the quality as same as source image? How can I prevent the significant change of the image size after the changes in my ApplyWatermark method?

Edit

1. This is how I convert BitmapFrame to Stream. Then I use that Stream to save the image to 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. This is how I get the BitmapFrame from Stream

private static BitmapFrame ReadBitmapFrame(Stream stream) {
    var photoDecoder = BitmapDecoder.Create(
        stream,
        BitmapCreateOptions.PreservePixelFormat,
        BitmapCacheOption.None);
    return photoDecoder.Frames[0];
}

3. This is how I read the file from local directory

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;
    }
}

解决方案

The reason I asked my question in the comments earlier, is because I noticed there were several different encoders available. A bitmap usually has a significantly larger file size, due to the amount of information it's storing about your image.

I haven't tested this myself, but have you tried a different encoder?

var pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(ApplyWatermark(null, null));

MemoryStream stm = File.Create(image);
pngEncoder.Save(stm);
return stm;

这篇关于图像尺寸应用简单的水印后大幅提高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆