Hello! All time used the code given in article RSDN
"Gdi + .Graphic of new generation"
https://rsdn.ru/?article/gdi/gdiplus1.xml
Bitmap* BitmapFromResource(HINSTANCE hInstance, LPCTSTR szResName, LPCTSTR szResType) { HRSRC hrsrc = FindResource(hInstance, szResName, szResType); if(!hrsrc) return 0; // "ненастоящий" HGLOBAL - см. описание LoadResource HGLOBAL hgTemp = LoadResource(hInstance, hrsrc); DWORD sz = SizeofResource(hInstance, hrsrc); void* ptrRes = LockResource(hgTemp); HGLOBAL hgRes = GlobalAlloc(GMEM_MOVEABLE, sz); if(!hgRes) return 0; void* ptrMem = GlobalLock(hgRes); // Копируем растровые данные CopyMemory(ptrMem, ptrRes, sz); GlobalUnlock(hgRes); IStream *pStream; // TRUE означает освободить память при последнем Release HRESULT hr = CreateStreamOnHGlobal(hgRes, TRUE, &pStream); if(FAILED(hr)) { GlobalFree(hgRes); return 0; } // Используем загрузку из IStream Bitmap *image = Bitmap::FromStream(pStream); pStream->Release(); return image; } I draw in a standard
Graphics g(hdc); g.DrawImage(BitmapFromResource(hInstance,MAKEINTRESOURCE(101),"PNG"),500,500) The problem is performance. The images of Rus are not allowed slowly (small ones are still normal, but a 600 by 200 picture, for example, will be drawn for a second (plus or minus :))). Can someone know a more productive version of drawing PNG from resources, without using third-party libraries . I will be grateful.
Updated
I changed the function a bit. I added a two-dimensional static array where I saved the module, the name and the Bitmap* itself Bitmap* . First, there is a check: if the name and the module are the same, then we return the Bitmap* that was previously Bitmap* , and that's it. If not, we do what we wrote on the RSDN, add the data to the array.
As it turned out, it was not a matter of finding (after all, we now only find it once), but in the method itself of the Graphics::DrawImage . Still, this method draws slowly
How to be?
g.DrawImage(BitmapFromResource(hInstance,MAKEINTRESOURCE(101),"PNG"),500,500)new bitmap is loaded from the resources and immediately leaks. Call theBitmapFromResourceonly once and use the return value for all subsequent drawing calls, and at the end do not forget to delete it. - VTT