【问题标题】:How do you set pixels as transparent in a bitmap in Direct2D? [duplicate]如何在 Direct2D 的位图中将像素设置为透明? [复制]
【发布时间】:2021-03-02 00:45:21
【问题描述】:

我正在使用 Direct2D 创建一个图像编辑器,并希望像在 GIMP 中一样“删除像素”(当您按下删除键时),它将像素设置为透明。

每个图像都使用一个 ID2D1BitmapRenderTarget,我使用 FillRectangle、DrawLine 等方法以及一些画笔来设置像素颜色。

现在我想设置一些像素透明,基本上我想要一个等效的函数调用:

pBitmapRenderTarget->SetPixelAlpha(column, row, 0.f);

【问题讨论】:

  • @SimonMourier 我会尝试使用 ID2D1Bitmap::CopyFromMemory。谢谢。

标签: c direct2d


【解决方案1】:

我设法实现了我的目标。这是函数:

// 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://docs.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
    );
}

【讨论】:

    猜你喜欢
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2021-12-31
    • 2016-07-17
    • 2016-09-12
    相关资源
    最近更新 更多