Getting BITMAP structure in DirectX.

Started by
9 comments, last by MiXen 9 years, 2 months ago

Hi,

I have a problem, I need to get Bitmap structure(https://msdn.microsoft.com/en-us/library/windows/desktop/dd183371%28v=vs.85%29.aspx) in DirectX, when I create a bitmap thought CreateBitmapRenderTarget. Espesially I need a value: bmWidthBytes, bmBitsPixel and bmBits. Anybody can help me, in what way I can do it?

Advertisement

I don't know exactly what you want. Do you have a BMP image and want to extract the width and height?


	std::vector<BYTE>Buffer = LoadBmpFile("myimage.bmp");

	BITMAPFILEHEADER* pFHeader = (BITMAPFILEHEADER*)Buffer.data();
	BITMAPINFO* pIHeader = (BITMAPINFO*)(Buffer.data() + sizeof(BITMAPFILEHEADER));

	if (pFHeader->bfType != 0x4d42)		//BM = 0x4d42
		throw std::exception("File type does not match header description in file");

	if (pFHeader->bfSize != Buffer.size())
		throw std::exception("File size does not match header description in file");

	BYTE* pImageData = Buffer.data() + pFHeader->bfOffBits;
	UINT Width = pIHeader->bmiHeader.biWidth;
	UINT Height = pIHeader->bmiHeader.biHeight;

You don't exacly understand me. I have that code:


IDWriteBitmapRenderTarget *pRenderTarget;
		hResult = pGDIInterop->CreateBitmapRenderTarget(
			NULL,
			m_renderTargetWidth,
			m_renderTargetHeight,
			&pRenderTarget
			);
		if (FAILED(hResult)) {
			m_lastError = L"Failed to create bitmap render target";
		}
		else {
			hResult = pRenderTarget->SetPixelsPerDip(1.0f);
			hResult = S_OK;

			HDC hDC = pRenderTarget->GetMemoryDC();
			if (hDC == NULL) {
				m_lastError = L"Failed to get render target DC";
				hResult = E_FAIL;
			}
			else {
				HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
				if (hBrush == NULL) {
					m_lastError = L"Failed to create brush";
					hResult = E_FAIL;
				}
				else {
					HBITMAP hBitmap = static_cast<HBITMAP>(GetCurrentObject(hDC, OBJ_BITMAP));
					if (hBitmap == NULL) {
						m_lastError = L"GetCurrentObject failed";
						hResult = E_FAIL;
					}
					else {
						DIBSECTION dib;
						int iResult = GetObject(hBitmap, sizeof(dib), &dib);
						if (iResult < sizeof(dib)) {
							m_lastError = L"GetObject failed";
							hResult = E_FAIL;
						}
						else {
							// Store render target resources and info
							m_pRenderTarget = pRenderTarget;

							m_hDC = hDC;
							m_hBlackBrush = hBrush;

							m_bmBits = dib.dsBm.bmBits;
							m_bmWidthBytes = static_cast<UINT>(dib.dsBm.bmWidthBytes);
							m_bmBytesPixel = static_cast<UINT>(dib.dsBm.bmBitsPixel) / 8;

							hResult = S_OK;
						}
					}

					if (FAILED(hResult))
						DeleteObject(hBrush);
				}
			}

			if (FAILED(hResult))
				pRenderTarget->Release();
		}

		pGDIInterop->Release();

The API of my project didn't allow me to use functions: CreateSolidBrush, GetCurrentObject and GetObject. With solid brush I don't have a problem, because in directX i had a functions, that allow me to do it, but with the two others, I havea problem, because I cannot use it, so I cannot get a BITMAP structure, so I don't have a value for bmBits, bmWidthBytes and bmBitsPixel. I must get this values, so is there another way to get it?

Why don’t you just make your own structures?


#pragma pack( push, 1 )
		/** The bitmap file header. */
		typedef struct LSI_BITMAPFILEHEADER {
			LSUINT16					ui16Header;
			LSUINT32					ui32Size;
			LSUINT16					ui16Reserved1;
			LSUINT16					ui16Reserved2;
			LSUINT32					ui32Offset;
		} * LPLSI_BITMAPFILEHEADER, * const LPCLSI_BITMAPFILEHEADER;

		/** The bitmap info header. */
		typedef struct LSI_BITMAPINFOHEADER {
			LSUINT32					ui32InfoSize;
			LSUINT32					ui32Width;
			LSUINT32					ui32Height;
			LSUINT16					ui16Planes;
			LSUINT16					ui16BitsPerPixel;
			LSUINT32					ui32Compression;
			LSUINT32					ui32ImageSize;
			LSUINT32					ui32PixelsPerMeterX;
			LSUINT32					ui32PixelsPerMeterY;
			LSUINT32					ui32ColorsInPalette;
			LSUINT32					ui32ImportantColors;
		} * LPLSI_BITMAPINFOHEADER, * const LPCLSI_BITMAPINFOHEADER;

		/** Bitmap color mask. */
		typedef struct LSI_BITMAPCOLORMASK {
			LSUINT32					ui32Red,
									ui32Green,
									ui32Blue,
									ui32Alpha;
		} * LPLSI_BITMAPCOLORMASK, * const LPCLSI_BITMAPCOLORMASK;

		/** Bitmap palette data. */
		typedef union LSI_BITMAPPALETTE {
			struct LSI_BM_COLOR {
				LSUINT8					ui8Red,
									ui8Green,
									ui8Blue,
									ui8Alpha;
			};
			LSUINT32					ui32Color;
		} * LPLSI_BITMAPPALETTE, * const LPCLSI_BITMAPPALETTE;
#pragma pack( pop )


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Ok, but how can I use it? I repeat that I cannot put informations to this structures,becuase my api doesn't support this functions. I'm looking for the new functions replace old one.

If Tispe had proposed a function named “LoadFileToMemory(),” rather than, “LoadBmpFile(),” it might have been more obvious to you.

Load the file to memory and then perform the same casts as he showed in his sample code.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Ok, I understand. But problem is in another place, because how can i convert IDWriteBitmapRenderTarget to something like that std::vector<BYTE>Buffer and then get some information about it?

IDWriteBitmapRenderTarget::GetMemoryDC

"An application can use the device context to draw using GDI functions. An application can obtain the bitmap handle (HBITMAP) by calling GetCurrentObject. An application that wants information about the underlying bitmap, including a pointer to the pixel data, can call GetObject to fill in a DIBSECTION structure. The bitmap is always a 32-bit top-down DIB."

Eee did you see my post with code? I use this function to get handle to bitmap, but I CANNOT USE THE GETCURRENTOBJECT FUNCTION BECAUSE OF MY API! I say it twice... I apreciate, that you want to help me, but please Read first my posts. So anybody can Tell me How can i get a bitmap handle (hbitmap) from idwritebitmaprendertarget without getcurrentobject function?

You can't.

This topic is closed to new replies.

Advertisement