【问题标题】:error of creating bitmap from Bitmapsource.create from C# Visual Studio 2013 WPF从 C# Visual Studio 2013 WPF 的 Bitmapsource.create 创建位图的错误
【发布时间】:2015-12-29 18:45:16
【问题描述】:

我正在尝试通过 C# Visual Studio 2013 WPF 中的以下函数为地图图块构建位图。

https://msdn.microsoft.com/en-us/library/ms616045%28v=vs.110%29.aspx

public static BitmapSource Create(int pixelWidth, int pixelHeight, double dpiX, double dpiY, PixelFormat pixelFormat, BitmapPalette palette, Array pixels, int stride);

 pixelWidth = tile.Image.Width;  //value is 524288
 pixelHeight = tile.Image.Height;   //value is 524288
 dpiX  = 96;
 dpiY  = 96;
 System.Windows.Media.PixelFormat pixelFormat = new System.Windows.Media.PixelFormat();
 pixelFormat = System.Windows.Media.PixelFormats.Pbgra32;
 pngBuffer is a Byte[] that has 778 integer elements
 stride = 1024

但是,我得到了错误:

 An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll

 Additional information: Value does not fall within the expected range 

我应该改变什么“价值”才能创建图像?

的帖子
http://stackoverflow.com/questions/28490203/throws-an-exception-when-cropping-an-image-if-window-is-maximized-wpf

http://stackoverflow.com/questions/24613246/system-argumentexception-occurred

帮不了我。

谢谢

更新 我做了以下更改:

pixelWidth = 256
pixelHeight = 256
stride =1024 because stride = pixelWidth * (bitsPerPixel/ 8)

我收到错误:

An unhandled exception of type 'System.ArgumentException' occurred in PresentationCore.dll

Additional information: Buffer size is not sufficient.

【问题讨论】:

  • 尺寸可能太大,因为这会产生 1Gb 的图像。使用 ImageMagick,您可以处理巨大的图像。
  • @Aybe,如果我将宽度和高度更改为 256,则会出现错误:PresentationCore.dll 中发生“System.ArgumentException”类型的未处理异常,附加信息:缓冲区大小不足。

标签: c# wpf visual-studio bitmap bitmapsource


【解决方案1】:

Stride 不是任意值,它代表一行像素或扫描线的宽度,应该计算出来。如果您在网上搜索,您会遇到很多公式,请注意您指向的链接:

int stride = width/8;

但这对我来说效果很好:

int stride = pixelWidth * (pixelFormat.BitsPerPixel / 8); 

【讨论】:

  • 我改变了步幅,得到了错误:PresentationCore.dll 中发生了“System.ArgumentException”类型的未处理异常附加信息:值不在预期范围内。
  • 还要确保你的缓冲区大小是stride * pixelHeight,即262144字节。
  • 顺便说一句,如果您希望为所有像素格式正确计算步幅,而不仅仅是每个像素具有整数字节数的格式,请使用stride = (pixelWidth * pixelFormat.BitsPerPixel + 7) / 8。
【解决方案2】:

@E-Bat/@Clemens 答案/评论是正确的,虽然有一些地方让你到达那里:

bytesPerPixel = (bitsPerPixel + 7) / 8
stride = width * bytesPerPixel
bufferSize = stride * height

从技术上讲,步幅是扫描线的长度,可以匹配您的图像宽度或更大,请参阅 DirectX 文档了解更多详细信息。

BitmapSource 知道其中的微妙之处并会采取相应的行动,即更大的缓冲区大小确实是一个有效值,但通常我们不会倾向于传递更大的内存块,除非进行低级编程。

stride 这个术语有点令人困惑,实际上它的起源来自 裸机平台,通常会在内存中处理纹理图集。

这是一个例子,PSX 中 VRAM 内容的图片:

让我们看一下黄色图标,访问此图标的正确步幅确实是 VRAM 宽度,因为您可以看到图标每行之间的长度等于 VRAM 宽度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多