【问题标题】:Value out of range with BitmapSource.CreateBitmapSource.Create 的值超出范围
【发布时间】:2016-07-11 16:34:26
【问题描述】:

在 WPF 中工作并尝试从头开始创建 BitmapSource。以下代码给出了一个参数异常“值不在预期范围内”。我不知道为什么。我还是 WPF 的新手,也许我没有正确使用像素格式?

int width = 12;
int height = 14;
byte[] colorData = new byte[width * height * 4];
for(int i = 0; i < colorData.Length; i++)
{
    colorData[i] = 155;  //create all pixels same shade of gray
}
BitmapSource bitmap = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgra32, null, colorData, width);

【问题讨论】:

    标签: c# wpf bytearray bitmapsource


    【解决方案1】:

    BitmapSource.Create 方法的最后一个参数是步幅,即每“扫描线”的字节数。

    如果是每像素四个字节的像素格式,则为4 * width

    var stride = 4 * width;
    var bitmap = BitmapSource.Create(
        width, height, 96, 96, PixelFormats.Bgra32, null, colorData, stride);
    

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 2022-11-26
      • 2011-08-02
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多