gpt4 book ai didi

bitmap - F# 从抛出异常的内存流创建位图

转载 作者:行者123 更新时间:2023-12-04 18:29:21 26 4
gpt4 key购买 nike

我得到了

System.ArgumentException: 'Parameter is not valid.' 

出于某种原因。

我得到它的一行:

let image = Bitmap.FromStream stream

这就是我的

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

知道我在这里做错了什么吗?提前致谢。

最佳答案

我不知道为什么你的代码不起作用(我测试了它并得到了相同的结果)。

另一个选项(对我有用)是使用 Marshal.UnsafeAddrOfPinnedArrayElement

let image = new Bitmap(width, height, bitmapData.Stride, bitmap.PixelFormat,Marshal.UnsafeAddrOfPinnedArrayElement(bi‌​tmapArray, 0))

编辑

感谢@ildjarn 的解释:

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

关于bitmap - F# 从抛出异常的内存流创建位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com