处理具有不良元数据的图像-WPF中损坏的色彩配置文件

Creating a Twitter client is a really interesting exercise in application development because, amongst many reasons, it's taking input from effectively an infinite number of people and places. Never trust user input, right? Input to your application comes not only in the form of text, but also images. Writing a Twitter client is effectively writing a web browser that only browses one website. Getting a browser stable is hard.

创建Twitter客户端是应用程序开发中一个非常有趣的练习,因为在许多原因中,它实际上是从无数人和地方获取输入。 永远不要相信用户的输入,对不对? 输入到您的应用程序不仅以文本形式出现,而且以图像形式出现。 编写Twitter客户端实际上是在编写仅浏览一个网站的Web浏览器。 使浏览器稳定很难。

Long Zheng, Raphael Rivera and the MetroTwit team (MetroTwit is a lovely new Twitter client) have hit an extremely interesting crashing bug. The input comes in the form of a corrupted JPG image from the web.

龙征( Long Zheng)拉斐尔·里维拉( Raphael Rivera)MetroTwit团队(MetroTwit是一个可爱的新Twitter客户)遇到了一个非常有趣的崩溃错误。 输入来自网络的损坏的JPG图像。

Here's the bad image. Looks like a picture some folks speaking on a panel. However, even though this image looks fine, this specific binary version of it has a corrupted Color Profile.

这是不好的形象。 看起来像是一些人在面板上讲话的图片。 但是,即使此图像看起来不错,它的此特定二进制版本的Color Profile也已损坏

Sometimes folks don't realize that image formats contain lots of metadata that you can't see. Your JPGs may show what camera you used, what lens, what settings, possibly even the geo-coordinates of where you took the picture!

有时人们没有意识到图像格式包含许多看不见的元数据。 JPG可能会显示您使用的相机,镜头,设置,甚至照片拍摄地的地理坐标!

You can view all this extended information (EXIF) with a number of tools. A great free one is ExifTool by Phil Harvey at the command line, or a non-command line one like ExifPro. Windows Live Photo Gallery lets you view the data also.

您可以使用许多工具来查看所有这些扩展信息(EXIF)。 一个很棒的免费工具是命令行上的Phil Harvey的ExifTool ,或者像ExifPro这样的非命令行工具Windows Live Photo Gallery使您还可以查看数据。

Here's a snippet of some of the info in this pic:

这是此图片中一些信息的摘要:

Device Mfg Desc                 : IEC http://www.iec.ch Device Model Desc               : IEC 61966-2.1 Default RGB colour space - sRGB Viewing Cond Desc               : Reference Viewing Condition in IEC61966-2.1 Viewing Cond Illuminant         : 19.6445 20.3718 16.8089 Viewing Cond Surround           : 3.92889 4.07439 3.36179 Viewing Cond Illuminant Type    : D50 Make                            : Leica Camera AG Camera Model Name               : M8 Digital Camera Software                        : Aperture 3.0.2 Shutter Speed Value             : 1/256 Exposure Compensation           : 0 Max Aperture Value              : 1.0 Metering Mode                   : Center-weighted average Light Source                    : Flash Focal Length                    : 0.0 mm

设备制造说明:IEC http://www.iec.ch设备型号描述:IEC 61966-2.1默认RGB颜色空间-sRGB查看条件描述:IEC61966-2.1中的参考查看条件查看导体光源:19.6445 20.3718 16.8089查看导体环绕度: 3.92889 4.07439 3.36179观察条件光源类型:D50品牌:Leica Camera AG相机型号名称:M8数码相机软件:光圈3.0.2快门速度值:1/256曝光补偿:0最大光圈值:1.0测光模式:中央重点平均光源:闪光灯焦距:0.0毫米

You can extract the image profile (ICC Profile) from an image like this with exiftool:

您可以使用exiftool从这样的图像中提取图像配置文件(ICC配置文件):

exiftool -icc_profile -b foo.jpg > profile.icc

exiftool -icc_profile -b foo.jpg> profile.icc

If you're hardcore, you can get the Windows Imaging Component (WIC) Tools and run WICExplorer. WPF uses WIC to decode images. WICExplorer will report the error with this image as you load it.

如果您是中坚分子,则可以获取Windows Imaging Component(WIC)工具并运行WICExplorer。 WPF使用WIC解码图像。 WICExplorer将在加载该图像时报告错误。

在WPF中加载图像 (Loading Images in WPF)

When you're using WPF (Windows Presentation Foundation) to display an image on Windows, you might do something like this:

当您使用WPF(Windows Presentation Foundation)在Windows上显示图像时,可能会执行以下操作:

<Image Width="300" Height="300" ImageFailed="Image_ImageFailed">
<Image.Source>
<BitmapImage UriSource="http://hanselman.com/blog/images/JPGwithBadColorProfile.jpg"/>
</Image.Source>
</Image>

Except with this particular image, I'll get an exception the Color Profile (the image metadata) is corrupted. "ArgumentException: Value does not fall within the expected range." This is a corrupted file.

除了该特定图像外,我还会看到一个异常,即颜色配置文件(图像元数据)已损坏。 “ ArgumentException:值不在预期范围内。” 这是一个损坏的文件。

at System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorContextsDelegate getColorContexts)
at System.Windows.Media.Imaging.BitmapFrameDecode.get_ColorContexts()
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.OnDownloadCompleted(Object sender, EventArgs e)
at System.Windows.Media.UniqueEventHelper.InvokeEvents(Object sender, EventArgs args)
at System.Windows.Media.Imaging.LateBoundBitmapDecoder.DownloadCallback(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

在System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorContextsDelegate getColorContexts) 在System.Windows.Media.Imaging.BitmapFrameDecode.get_ColorContexts() 在System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() 在System.Windows.Media.Imaging.BitmapImage.OnDownloadCompleted(Object sender,EventArgs e) 在System.Windows.Media.UniqueEventHelper.InvokeEvents(对象发送者,EventArgs参数) 在System.Windows.Media.Imaging.LateBoundBitmapDecoder.DownloadCallback(Object arg) 在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs) 在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象args,Int32 numArgs,委托catchHandler)

If I get this exception, I can try to load the image again and ignore its color profile. Here's how I'd do that in XAML:

如果出现此异常,我可以尝试再次加载图像并忽略其颜色配置文件。 这是我在XAML中执行的操作:

<Image Width="300" Height="300" ImageFailed="Image_ImageFailed"  >
<Image.Source>
<BitmapImage CreateOptions="IgnoreColorProfile" UriSource="http://hanselman.com/blog/images/JPGwithBadColorProfile.jpg"/>
</Image.Source>
</Image>

If you're loading from code, you can ignore color profile information by adding the BitmapCreateOptions.IgnoreColorProfile flag to CreateOptions.

如果从代码加载,则可以通过将BitmapCreateOptions.IgnoreColorProfile标志添加到CreateOptions来忽略颜色配置文件信息。

As an aside, Andrew Eichacker has a nice post on how to read all the BitmapMetadata in WPF. There's lots in there!

顺便说一句 Andrew Eichacker撰写了一篇不错的文章,介绍如何读取WPF中的所有BitmapMetadata 里面有很多!

Here's loading the Bitmap into an image Control called "Foo."

将位图加载到名为“ Foo”的图像控件中。

var bi = new BitmapImage();
bi.BeginInit();
bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bi.UriSource = new Uri("http://hanselman.com/blog/images/JPGwithBadColorProfile.jpg");
bi.EndInit();

foo.Source = bi;

Knowing about possible corruption is important to be aware of, especially if you're loading arbitrary images from all over the place. If you don't care about color profiles, I'd just ignore them by default in your image loading code. If you are writing an image editor or you care about profiles, I'd catch the exception, let the user know, then load the image again without the profile.

了解可能的损坏非常重要,要特别注意,如果要从各处加载任意图像。 如果您不关心颜色配置文件,则默认情况下,在图像加载代码中我将忽略它们。 如果您正在编写图像编辑器或关心配置文件,我会捕获异常,让用户知道,然后在没有配置文件的情况下再次加载图像。

翻译自: https://www.hanselman.com/blog/dealing-with-images-with-bad-metadata-corrupted-color-profiles-in-wpf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值