SetPixel is too slow. Is there a faster way to draw to bitmap?

前端 未结 5 472
生来不讨喜
生来不讨喜 2020-11-30 06:08

I have a small paint program that I am working on. I am using SetPixel on a bitmap to do that drawing of lines. When the brush size gets large, like 25 pixels across there i

5条回答
  •  广开言路
    2020-11-30 06:45

    I usually use an array to represent the raw pixel data. And then copy between that array and the bitmap with unsafe code.

    Making the array of Color is a bad idea, since the Color struct is relatively large(12 bytes+). So you can either define your own 4 byte struct(that's the one I chose) or simply use an array of int or byte.

    You should also reuse your array, since GC on the LOH tends to be expensive.

    My code can be found at:

    https://github.com/CodesInChaos/ChaosUtil/blob/master/Chaos.Image/

    An alternative is writing all your code using pointers into the bitmap directly. That's a bit faster still, but can make the code uglier and more error prone.

提交回复
热议问题