JpegBitmapEn codeR叶在文件末尾灰色条纹? [英] JpegBitmapEncoder leaves gray streak at end of file?

查看:151
本文介绍了JpegBitmapEn codeR叶在文件末尾灰色条纹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试做一些简单的图像处理的WPF成像组件工作。我粗略下面就的MS文章如何恩code&放;德code JPEG图像

I'm working with the WPF imaging components in an attempt to do some simple image manipulation. I'm roughly following the MS article on How to Encode & Decode a JPEG Image.

我的code是简单的:

My code is simple:

BitmapImage bi = new BitmapImage();

bi.BeginInit();
bi.UriSource = new Uri("D:\\Temp\\StackOverflowCapture.png");
//bi.DecodePixelWidth = 1024;
//bi.DecodePixelHeight = 768;
bi.EndInit();

var jpg                  = new JpegBitmapEncoder();
    jpg.QualityLevel     = 90;
    //jpg.FlipHorizontal = false;
    //jpg.FlipVertical   = false;
    jpg.Frames.Add(BitmapFrame.Create(bi));

var fs = new FileStream("D:\\Temp\\StackOverflow_Result.jpg", FileMode.Create);

jpg.Save( fs );
jpg = null;

在code段创建一个奇怪的神器,其中最后几个行是纯灰色的块。这几乎就像如果EN codeR刚跑出像素写的。灰色的量2倍或3x视我为输入(各项决议,JPEG,PNG)或选择启用(强制在德code分辨率)使用的图像上的因素而变化。最终的效果是一样的,但是,你可以从下面的图片中看到的。

The code snippet creates an odd artifact, where the last few "lines" are a block of solid gray. It's almost as if the encoder just ran out of pixels to write. The amount of gray varies by a factor of 2x or 3x depending on the image I use as input (various resolutions, jpeg, png) or the options I enable (forcing resolution on decode). The net effect is the same, however, as you can see from the images below.

我的源图像是:

输出图像(注意在底部的灰色带):

Output image ( note the gray band at the bottom):

这是怎么回事?我如何得到一个干净的转换?

What's going on here? How do I get a clean convert?

推荐答案

这肯定是两耳之间的短路。对于其他有类似弱点的缘故,不过,我回答我的问题。

This was definitely a short circuit between the ears. For the sake of others with similar frailties, however, I'm answering my own question.

您需要关闭/刷新流。另外,这将是很好的做法,在创建 FS A 使用块。

You need to close/flush the stream. Alternately, it would be good practice to create fs in a using block.

修改code是这样的:

Modified code is thus:

BitmapImage bi = new BitmapImage();

bi.BeginInit();
bi.UriSource = new Uri("D:\\Temp\\StackOverflow_Source.png");
bi.EndInit();

var jpg                  = new JpegBitmapEncoder();
    jpg.QualityLevel     = 90;
    jpg.Frames.Add(BitmapFrame.Create(bi));

var fs = new FileStream("D:\\Temp\\StackOverflow_Result.jpg", FileMode.Create);

jpg.Save( fs );
fs.close();       <--- duh, flush the stream to disk
jpg = null;

这篇关于JpegBitmapEn codeR叶在文件末尾灰色条纹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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