DirectBitmap的F#实现:保存为空白

时间:2018-01-12 21:02:33

标签: .net f#

我尝试在图像中设置像素,然后将其另存为PNG。我得到正确的bpp和大小的PNG文件,但它完全是黑色的。所以,我认为Save工作正常,但是设置像素丢失或Bits数组中的设置像素格式错误。有人会解释一下,这段代码有什么问题吗?

type WrBitmap =
    class
        val Width : int
        val Height : int
        val Bits : array<uint32>
        val BitsHandle : GCHandle
        val Bitmap : Bitmap
        val Length : int64
        val mutable Disposed : bool

        new (sizeAsFile : string) =
            let img : Image = Image.FromFile (sizeAsFile)
            new WrBitmap (img.Width, img.Height)

        new (width : int, height : int) =
            let bits = Array.zeroCreate (width * height)
            let bitsHandle = GCHandle.Alloc (bits, GCHandleType.Pinned)
            let bitmap = new Bitmap (width, height, width * 4, PixelFormat.Format32bppArgb, bitsHandle.AddrOfPinnedObject ())
            {
                Width = width
                Height = height
                Bits = bits
                BitsHandle = bitsHandle
                Bitmap = bitmap
                Length = width * height |> int64
                Disposed = false
            }

        member inline __.SetPixel (x : int, y : int, color : Color) =
            let i = x + (y * __.Width)
            let c = color.ToArgb ()
            __.Bits.[i] <- uint32 c

        member inline __.GetPixel (x : int, y : int) =
            let i = x + (y * __.Width)
            let c = __.Bits.[i]
            Color.FromArgb (int c)

        member __.Save (path : string) =
            __.Bitmap.Save (path)

        interface IDisposable with
            member __.Dispose () =
                if __.Disposed <> true then
                    __.Bitmap.Dispose ()
                    __.BitsHandle.Free ()
                    __.Disposed <- true

    end

1 个答案:

答案 0 :(得分:2)

我使用以下代码段测试了您的代码,它运行正常:

SELECT  [order_id],
      SUM(CASE WHEN prod_id = 1 THEN qty ELSE 0) AS p1_tot,
      SUM(CASE WHEN prod_id = 2 OR prod_id = 3 THEN qty ELSE 0) AS p2and3_tot,
      SUM(CASE WHEN prod_id = 4 OR prod_id = 6 THEN qty ELSE 0) AS p4and6_tot  
FROM [orders] 
    INNER JOIN [product_class] ON [orders].[prod_id] = [product_class].[id]
WHERE [product_class].[class] = 2
GROUP BY [order_id];

它创建10x10位图,该位图全部是透明的,中间有一个红色像素。所以,我怀疑你看到的问题是由其他原因引起的。