Reputation: 53
I'm getting
System.ArgumentException: 'Parameter is not valid.'
for some reason.
A line where I'm getting it:
let image = Bitmap.FromStream stream
That's what I have
let ChangeBitmapColors (bitmap : Bitmap) =
Console.WriteLine bitmap.Size
let width = bitmap.Width
let height = bitmap.Height
let rectangle = new Rectangle(0, 0, width, height)
let bitmapData = bitmap.LockBits(rectangle, Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat)
let length = bitmapData.Stride * height
let bitmapArray : byte[] = Array.zeroCreate length
Marshal.Copy(bitmapData.Scan0, bitmapArray, 0, length)
bitmap.UnlockBits bitmapData
//todo something with bitmapArray
use stream = new MemoryStream(bitmapArray)
let image = Bitmap.FromStream stream
stream.Dispose |> ignore
image
Any ideas what am I doing wrong here? Thanks in advance.
Upvotes: 3
Views: 315
Reputation: 2220
I don't know why your code not working (I tested it and got the same result).
Another option (that works for me) it's use Marshal.UnsafeAddrOfPinnedArrayElement
let image = new Bitmap(width, height, bitmapData.Stride, bitmap.PixelFormat,Marshal.UnsafeAddrOfPinnedArrayElement(bitmapArray, 0))
Edit
Thanks @ildjarn for the explanation:
The stream didn't work because it was just raw pixel data without any image header, so there was no way to know simple things like the image dimensions and palette type. The stream approach is possible, but you need to save the image header to the stream, too, not just the scanlines
Upvotes: 3