VB.net update for SafeArray2D + CopyMemory trick

VB.net update for SafeArray2D + CopyMemory trick

Post by Jean Ghal » Sat, 16 Mar 2002 02:18:27


This question is intended for people who ever have a look at VB.net. Some
time ago, I wrote a little VB5 app which perform image processing on 16 bit
grayscale pictures. For updating display, I used a  code starting like this

Public Sub CalcArray16_DisplayDib(CalcDib() As Integer, dDib As

'Dim time_start As Single
'time_start = Timer

Dim DispDib() As Byte

    With DispSA
        .cbElements = 1
        .cDims = 2
        .Bounds(0).lLbound = 0
        .Bounds(0).cElements = dDib.Height
        .Bounds(1).lLbound = 0
        .Bounds(1).cElements = dDib.BytesperScanLine()
        .pvData = dDib.DIBSectionBitsPtr
    End With
    CopyMemory ByVal VarPtrArray(DispDib()), VarPtr(DispSA), 4


End sub

Most of you will propably see the famous SafeArray2D + CopyMemory trick. I
decided some days ago to update the code to vb.net. I use Visual Studio.Net
Beta 2. The System.Drawing.Bitmap Class seems to be a good base for updating
the code as it allows native support of 16 bit greyscale pixel format. After
using the Lockbits method, I'm returned a Bitmapdata class (available in
System.drawing.imaging) which allows me to obtain desired informations :
width, height, stride (ie BytesperScanLine), scan0 (ie DIBSectionBitsPtr).
varPtr function is not available anymore in VB.Net. I know how I can get a
pointer to a vb.net array (not safearray anymore). My question is : how can
I get with the known informations (height, stride, scan0) get an array back
with VB.Net? The question could be also : how can I get an array from a
SafeArray structure (in System.Runtime.InteropServices.UnmanagedType) in
Thanks by advance,
                            Jean Ghali