Tutorial :Using GDI+ Bitmap



Question:

I am using GDI+ Bitmap class to convert an IStream to HBITMAP. I have included the gliplus lib file in the Linker inputs and also have the dll in the build path. But using the statement

Bitmap bm(lpStream,FALSE);  

gives me an error C2065: 'Bitmap' : undeclared identifier

Can someone please tell me what I am doing wrong here.

Thanks.

Edit
I have already included the appropriate headers in my implementation (gdiplus.h) And I can view the definition of Bitmap by selecting the "Go to definition" option in the context menu.


Solution:1

You also need to include the relevant header file. At a guess, it would probably have a name like "Bitmap.h" or "gdi+.h".

There is some more detail on the Bitmap class here. The correct header file is "gdiplus.h". In short:

#include "gdiplus.h"    Constructor Information  
Stock Implementation  gdiplus.dll  Header    Declared in Gdiplusheaders.h, include gdiplus.h  Import library    gdiplus.lib  Minimum availability  GDI+ 1.0  Minimum operating systems  Windows 98/Me, Windows XP, Windows 2000,  

Windows NT 4.0 SP6

In the table in MSDN, where it says "Header", this tells you the name of the header file you need to include. The "Import Library" you have already covered. Had you missed that, you would have gotten a link error.

EDIT: In this article on getting started with GDI+, it looks like there is a namespace "Gdiplus" that you need to specify. Either use "using namespace Gdiplus" or specify the namespace explicitly.

#include <windows.h>  #include <gdiplus.h>  using namespace Gdiplus;    VOID OnPaint(HDC hdc)  {     Graphics graphics(hdc);     Pen      pen(Color(255, 0, 0, 255));     graphics.DrawLine(&pen, 0, 0, 200, 100);  }  


Solution:2

In addition to the namespace issue, using Gdiplus also requires that the library is initialized before it is used:

ULONG_PTR gdiplusToken;  GdiplusStartupInput startupInput;  GdiplusStartup(&gdiplusToken, &startupInput, 0);  

You will need to hold on to the token until you are done using Gdiplus, then release it:

GdiplusShutdown(gdiplusToken);  

If the library is not initialized, Gdiplus operations will fail with the error GdiplusNotInitialized.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »