0

我正在尝试通过 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.
4

2 回答 2

1

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

int stride = width/8;

但这对我来说效果很好:

int stride = pixelWidth * (pixelFormat.BitsPerPixel / 8); 
于 2015-12-29T20:59:33.087 回答
0

@E-Bat/@Clemens 的答案/评论是正确的,尽管有一些东西让你到达那里:

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

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

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

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

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

在此处输入图像描述

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

于 2015-12-30T12:22:02.207 回答