【问题标题】:WPF JpegBitmapDecoder creating different height and widthWPF JpegBitmapDecoder 创建不同的高度和宽度
【发布时间】:2011-12-12 07:08:19
【问题描述】:

我正在尝试读取 Jpeg 图像并将其绑定到矩形的填充属性,代码如下:

Dim filePath as string = "PathToJpeg.jpg"     
Dim imageStreamSource As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim decoder As New JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Dim bitmapSource As BitmapSource = decoder.Frames(0)

当我用较小的图像阅读此内容时,这可以正常工作。但是当我将它指向一个 3840 x 3024 的图形时,bitmmapSource.Height 的读数为 924,bitmapSource.Width 的读数为 1174

据我所知,1024x768 几乎是上限

我在这里遗漏了什么明显的东西吗?

【问题讨论】:

  • 我相信您的 sorce jpeg 是从扫描仪 (~300DPI) 收到的?在这种情况下,结果是可以预期的,因为 BitmapSource.Width 属性以与设备无关的单位(每单位 1/96 英寸)获取位图的宽度。请尝试 BitmapSource.PixelWidth/BitmapSource.DpiX 属性...
  • @Saint Domino:关于您的赏金评论,您是说您认为将 JPEG 直接加载到位图中是一种黑客行为,而使用 JpegBitmapDecoder 不是一种黑客行为?我可能会认为相反。
  • 是的。我的最终目标是从文件系统加载 jpeg 并将其用作 WPF 矩形的填充,保留原始高度和宽度。为了做到这一点,我不应该不得不求助于 GDI+(并添加 system.drawing 程序集)。我可以使用 System.Drawing.Bitmap...但是对于这种情况,我不应该这样做。这就是为什么我认为这是一个 hack。

标签: .net wpf image image-processing jpeg


【解决方案1】:

我真的不知道您为什么要明确使用 JpegBitmapDecoder。我的 C# 代码非常简单:

BitmapSource bitmapSource = new BitmapImage(new Uri(@"PathToJpeg.jpg"));

BitmapSource 类提供两个宽度和高度属性:

Width:以与设备无关的单位(每单位 1/96 英寸)获取位图的宽度。 (From MSDN)

PixelWidth:获取位图的宽度,以像素为单位。(From MSDN)

我用 111 MPixel 的图像对此进行了测试,效果很好。

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多