jingwood / d2dlib

A .NET library for hardware-accelerated, high performance, immediate mode rendering via Direct2D.
MIT License
234 stars 40 forks source link

Direct pixel access to D2DBitmap #110

Open mystery123sk opened 1 year ago

mystery123sk commented 1 year ago

Hi, is there a way to directly change byte array of D2DBitmap as it's possible in C++ D2D? Something like this in C++:

// Assuming you have a valid ID2D1Bitmap* named bitmap
D2D1_BITMAP_PROPERTIES bitmapProperties;
bitmap->GetDpi(nullptr, nullptr);
bitmap->GetPixelFormat(&bitmapProperties.pixelFormat);

UINT32 width, height;
bitmap->GetSize(&width, &height);

UINT32 pixelSize = bitmapProperties.pixelFormat.GetPixelSize();
UINT32 stride = width * pixelSize;

D2D1_MAPPED_RECT mappedRect;
HRESULT hr = bitmap->Map(D2D1_MAP_OPTIONS_READ | D2D1_MAP_OPTIONS_WRITE, &mappedRect);
if (SUCCEEDED(hr))
{
    BYTE* pixelData = mappedRect.bits;
    // Now you can access and modify the pixel data here

    // Make sure to unmap the bitmap when you're done editing
    bitmap->Unmap();
}

It could help us improve the performance, since every time we call CreateBitmapFromMemory, a new bitmap is created in memory.