1

I recently saw this error for the first time in previously working code, and don't follow why.

The exception is thrown on

BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgr24, null, data, stride);

Here, width is 1194, height is 824, and stride uses the equation ((width * 24 + 23) & ~23) / 8, from this SO answer, which results in 3584 (which makes sense). The size of data is 2 951 568, which appears to be correct (1194 * 824 * 3). These values are all taken from the debugger.

I've not been able to reproduce this, so can't identify the exact circumstances that caused it, but don't see how the exception makes sense from the arguments that were supplied.

Is it perhaps because I misunderstand what stride is supposed to be (and in the past, perhaps the resolution was always a multiple of the bit width)? The data array does not have padding.

Community
  • 1
  • 1
Kat
  • 4,645
  • 4
  • 29
  • 81
  • `& ~23` look suspicious. I doubt it does what you want (and there is no sample like that in post you've linked which only show (power of 2 -1) values which work with that kind of conversion) – Alexei Levenkov Jan 06 '16 at 21:55
  • Hmm, I'm thinking that the case I presented into the last paragraph (which I just edited in) is likely the case, since changing the code to use `width * 3` in place of `stride` seems to be working. Stride is not documented, so this is merely guessing that the old equation was for the assumption that `data` is aligned to 4 bytes (which it's not -- it's width * height * depth, no scanline alignment). – Kat Jan 06 '16 at 22:01
  • 2
    I think that's it. See [this question](http://stackoverflow.com/questions/8492563/bitmapsource-create-error-buffer-size-is-not-large-enough). – 500 - Internal Server Error Jan 06 '16 at 22:03
  • Just google on "stride of a bitmap", for example: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9bf9dea5-e21e-4361-a0a6-be331efde835/how-do-you-calculate-the-image-stride-for-a-bitmap?forum=csharpgeneral – Simon Mourier Jan 06 '16 at 22:03
  • I see. I believe what happened was that at some point of time (I recall having had other issues with this value in the past, hence my usage of that calculation for stride), I used a calculation for stride that assumes the data came from a `Bitmap`, but my data is from another source that does not have the "scanline aligned to 4 bytes" issue, and thus that's the wrong calculation for stride. In my defense, documentation defined the stride argument as "the stride of the image" :P. – Kat Jan 06 '16 at 22:07
  • I think @500-InternalServerError's link is a duplicate, now. Different exception, but the accepted answer there applies. – Kat Jan 06 '16 at 22:08

0 Answers0