【问题标题】:Bitmap creation in Win32 using Gdiplus使用 Gdiplus 在 Win32 中创建位图
【发布时间】:2020-03-13 19:10:40
【问题描述】:

我正在尝试使用 Win32 和 Gdiplus 创建位图。我不想加载我只想使用 Setpixel 创建自己的图像的文件。我使用了这个构造函数:

void Bitmap(INT width, INT height, INT stride, PixelFormat format, BYTE *scan0);

但是,当我尝试在我的位图上使用 Setpixel 时,即使 GetHeight 返回 0,它也无济于事。

m_pixelMap = new BYTE[m_renderSpaceWidth*m_renderSpaceHeight * 3];
Gdiplus::Bitmap img(m_renderSpaceWidth, m_renderSpaceHeight, 24 * m_renderSpaceWidth, PixelFormat24bppRGB, m_pixelMap);
std::cout << "H: " << img.GetHeight() << std::endl;

我做错了什么?

【问题讨论】:

  • stride 是指从一行到下一行的字节数,而不是位数。此外,位图行必须是 DWORD 对齐的;根据 24 bpp 位图的宽度,您可能需要为每行填充字节以满足此要求。
  • 我将 24 位更改为 3 个字节,但仍然一无所获。关于 DWORD 对齐,我稍后会处理,但现在我只想获得我的图像高度。
  • 好的,我明白了。我是 Gdiplus 的新手,我不知道我必须初始化它。

标签: c++ winapi graphics bitmap gdi+


【解决方案1】:

所以我不知道我必须先初始化 Gdiplus,这就是我所缺少的。我稍微改变了位图的创建。现在一切正常。

m_stride =  ALIGN_CLUSPROP(3 * m_renderSpaceWidth);
m_padding = m_stride - (m_renderSpaceWidth * 3);
m_horizontalScanLine = (m_renderSpaceWidth * 3) + m_padding;

m_pixelMap = new BYTE[m_stride * m_renderSpaceHeight];
m_bitmap = new Gdiplus::Bitmap(m_renderSpaceWidth, m_renderSpaceHeight, m_stride, PixelFormat24bppRGB, m_pixelMap);

【讨论】:

    猜你喜欢
    • 2019-01-04
    • 1970-01-01
    • 2023-03-23
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    相关资源
    最近更新 更多