0
votes

I'm trying to create a bitmap using Win32 and Gdiplus. I don't want to load in a file I just want to create my own image by using Setpixel. I used this constructor:

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

But when I try to use Setpixel on my bitmap it does nothing even GetHeight returns 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;

What am I doing wrong?

1
stride is meant to be the number of bytes from one row to the next, not the number of bits. Additionally, bitmap rows must be DWORD-aligned; depending on the width for a 24 bpp bitmap you may need padding bytes for each row to satisfy this requirement.Jonathan Potter
I changed 24 bits to 3 bytes and I still get nothing. About the DWORD alignment, I will manage that later but for now I just want to get my image height.Abbion Nebula
Ok, I got it. I'm new to Gdiplus and I didn't know that I have to initialize it.Abbion Nebula

1 Answers

1
votes

So I didn't know that I have to initialize Gdiplus first so that is what I was missing. And I changed the bitmap creation a bit. Now everything works.

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);