Home F# implementation of DirectBitmap: saved blank
Reply: 0

F# implementation of DirectBitmap: saved blank

user1985
1#
user1985 Published in April 27, 2018, 8:33 am

I'm trying to set pixels in image and then to save it as PNG. I get PNG file with correct bpp and size but it's totally black. So, I suppose Save works correctly, but either set pixels are lost or set pixels in Bits array have wrong format. Would somebody explain, what's wrong with this code?

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323024 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO