0

I am creating an image editor with Direct2D and would like to "delete pixels" like in GIMP (when you press the delete key) which sets the pixels as transparent.

Each image uses a ID2D1BitmapRenderTarget and I use methods like FillRectangle, DrawLine... with some brushes in order to set the pixel colors.

Now I would like to set some pixels transparent, basically I would like an equivalent of this function call:

pBitmapRenderTarget->SetPixelAlpha(column, row, 0.f);
Itsbananas
  • 569
  • 1
  • 4
  • 12

1 Answers1

1

I managed to achieve my goal. Here is the function:

// Each image in my program is managed by a Document class.
// I implemented a method for this class.
void Document::SetPixelTransparent(float column, float row)
{
    // The rectangle in the bitmap corresponding to the pixel.
    D2D1_RECT_U dstRect = D2D1::RectU(
        (UINT)column,
        (UINT)row,
        (UINT)(column + 1),
        (UINT)(row + 1)
    );

    // We will write this color in the pixel.
    BYTE pixelColor[4] = {
        0x0,// Blue
        0x0,// Green
        0x0,// Red
        0x0// Alpha <== I only care about the value in this channel
    };

    ID2D1Bitmap *bitmap;
    m_pBitmapRenderTarget->GetBitmap(&bitmap);

    // https://learn.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-id2d1bitmap-copyfrommemory
    // The next Direct2D function needs to know how big in bytes a row of pixels is.
    const UINT NUM_BYTES_PER_PIXEL = 4;// I use 4 bytes for the pixel color in my images
    const UINT MEMORY_PADDING = 0;// I have no idea if 0 is fine
    const UINT32 PITCH = bitmap->GetSize().width * NUM_BYTES_PER_PIXEL + MEMORY_PADDING;

    HRESULT hr = bitmap->CopyFromMemory(
        &dstRect,
        pixelColor,
        PITCH
    );
}
Itsbananas
  • 569
  • 1
  • 4
  • 12