Memory is not freed after converting BitmapImage

前端 未结 2 817
春和景丽
春和景丽 2021-02-11 01:05

I got a problem with the following c# (test-)code:

    public static void TestBitmap2ByteArray(BitmapImage bitmap)
    {
        JpegBitmapEncoder encoder = new          


        
2条回答
  •  抹茶落季
    2021-02-11 01:49

    You are accessing the bitmap data by saving it to a stream. This causes overhead. A much better way to do it is by using LockBits. This will give you direct access to the bytes in the image and you can easily access it both as *byte[] and *UInt32[] (note: requires unsafe{}).

    If you still need to copy the bytes to a byte array then Marshal.Copy can be used, but if your intent is to make modifications directly to the image you are free to do so. The LockBits link has a sample showing the use of Marshal.Copy.

    If you need some samples of how to process picture I have released some sample code on image processing for the Kinect.

提交回复
热议问题