i'm just doing a simple job: Converting a bitmap into an array, then using that array, recreating the bitmap with BitmapSource.Create method.
However, i'm getting the error: "Value does not fall within the expected range". Here's my code.
Dim width As Integer = bitmapImage.PixelWidth
Dim height As Integer = bitmapImage.PixelHeight
Dim bytesPerPixel As Integer = bitmapImage.Format.BitsPerPixel / 8
Dim stride As Integer = width * bytesPerPixel
Dim pixelBuffer = New Byte(height * stride - 1) {}
bitmapImage.CopyPixels(pixelBuffer, stride, 0)
Dim bmpSource As BitmapSource = BitmapSource.Create(width, height, 96, 96, PixelFormats.Bgr32, Nothing, pixelBuffer, width)
Image2.Source = bmpSource
Any help regarding that will be appreciated, thank you.
Dim pixelBuffer = New Byte(height * stride - 1) {}
allocates one byte too little.
As an example, a 4x4 pixel image with 4 bytes per pixel will allocate 4*4*4-1=63 bytes, but 64 are required.
Also, you're using BGR32 (4 byte pixels) here so you're safe, but the stride may in other pixel formats need to be rounded up to the next 4 byte boundary.
BitmapSource.Create also takes stride
as last parameter, not width
.