Can I pass a locked .NET BitmapData.Scan0 to unmanaged C++?

82 views Asked by At

I wanted to know whether sending a locked Bitmap's BitmapData.Scan0 field to unmanaged C++ could be an issue. Here's how I do it:

From C#.NET:

public class BmpTest{
    Bitmap bmp = new Bitmap();
    BitmapData bmpData = new BitmapData();


    public BmpTest()
    {
        fooFillBitmapWithData(bmp);
    }

    public IntPtr getDataPtr()
    {
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
        return bmpData.Scan0;
    }

    public void unlockData()
    {
        bmp.UnlockBits(bmpData);
    }
}

Then, using a C++/CLI Wrapper (not included in this sample):

// This is unmanaged, native c++
int main()
{
    unsigned short * dataPtr = BmpTestCliWrapper.getDataPtr();
    // do something like ... update openGL texture with data 
    BmpTestCliWrapper.unlockData();
}

I am not using GCHandle, as BitmapData will be locked to memory according to this source. I only need to read the Bitmap so that I can update an OpenGL texture. I cannot access the images directly from C++. I probably could from C++/CLI but I don't see a clear advantage.

0

There are 0 answers