3

I need to implement an image processing function in VB without resorting to the inefficient GetPixel/SetPixel methods.

I prefer the LockBits/UnlockBits mechanism. But all examples I have found seem to require to copy the bitmap buffer to an intermediate byte array. I find this pretty inefficient and I would prefer to access the RGB bytes in the bitmap buffer directly.

Is there a simple way to do that ?

  • It is faster and easier to do this in C# (see [this post](http://stackoverflow.com/q/24701703/1070452)) because C# allows `unsafe` blocks and pointers which is much more cumbersome in VB. – Ňɏssa Pøngjǣrdenlarp Jun 11 '16 at 20:19
  • @Plutonix: using VB isn't my choice. I usually do that in native C++, when not in explicit SSE/AVX vectorization. –  Jun 11 '16 at 21:37
  • `LockBits/UnlockBits` is extremely slow as well. This is because it performs a lock on the operation, change what you need and then unlock it finally, this proves to be inefficient. The approach I know of is creating a writeable bitmap and then performing somethings there, but its only for Win8 and up I believe... I think I might have an alternative approach, but it's unmanaged... – Trevor Jun 12 '16 at 03:37
  • Lock/Unlock is indeed performing a whole image conversion, in and out, which explains the delay. The solution needs to be pure VB. –  Jun 12 '16 at 07:49
  • @Plutonix: It doesn't matter if you implement the code in vb.net or c#. The results will be the same. In [that case](http://stackoverflow.com/q/24701703/1070452), the sample code was written in c# because that's what the OP requested. A vb.net implementation will be just as fast. – xfx Jul 01 '16 at 10:47
  • @xfx: I don't think so. C# allows unsafe accesses, which are the fastest and do not require copies. I don't think this is doable in VB. The sample code that you show is what I need to avoid. –  Jul 01 '16 at 10:55
  • 1
    @YvesDaoust: I've implemented that sample in vb.net and it works just fine. Notice that the way the bitmap is constructed is through an (unsafe) handle which is mapped to a bits array, that's the purpose of the `BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned)` code. This allows reading/writing the bitmap without locking/unlocking it and it's extremely fast. You could do away with lock/unlock without having to copy the whole bitmap buffer (I've also done that), but the technique explained by @SaxxonPike is by far the fastest. – xfx Jul 01 '16 at 11:08

0 Answers0