Does Bitmap.LockBits “pin” a bitmap into memory?

后端 未结 2 1476
长发绾君心
长发绾君心 2020-12-21 01:05

I\'m using locked bitmaps a lot recently, and I keep getting \"attempted to access invalid memory\" errors. This is mostly because the bitmap has been moved in memory. Some

2条回答
  •  时光说笑
    2020-12-21 01:29

    In your case an attempted to access invalid memory error is most probably caused by invalid memory allocation which you are doing in the unsafe part of code, e.g allocated array is smaller than number of pixels you are trying to put in that.

    There is also no need to think about pinning the objects unless your image data is less than 85000 Bytes as only objects less than 85K will be moved in memory.

    Another story would be if you pass the object to unmanaged code, for example in the c++ library for faster processing. In this case your exception is very possible if the passed image gets out of scope and will be garbage collected. In this case you can use GCHandle.Alloc(imageArray,GCHandleType.Pinned); and than call Free if you do not need it any longer.

提交回复
热议问题