Click here to Skip to main content
15,878,602 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone, I'm developping a C++ application and I must display the result as an image. So that, I must draw dynamicly a png image and put into it other components like images.
I'm searching for a solution with GDI+ but I'm not understanding very well how to proceed.
Can anyone tell me how to do ?
Many thanks in advance for your precious help.
Best regards.
Posted
Updated 5-Apr-10 3:56am
v2

Dear Mr. Mourad,

Well, you're not very specific here. Are you using CLI (managed) code? If you're using the managed framework, create a reference to a System::Drawing::Graphics object (use the Control.CreateGraphics() method), and use Graphics.DrawImage(Image image, Point point) or one of the many overloads.
If you're using native C++, create a Graphics::Graphics object using one of the Constructors, then call the DrawImage method.

If you need more help, clarify your question a bit or add a minimum example, and I'll help more.
 
Share this answer
 
Dear Sir,

I stand by my original response, use the Graphics::Graphics class, and the draw methods (DrawString, DrawImage, et cetera) in the gdiplus.h file. Let me know if you have problems...see this[^] page, too. Get past all the crap about the library version stuff and how they're exported. Read starting one paragraph before
A Basic GDI+ Application
Good luck.
Aaron
ProCure
 
Share this answer
 
Thanks for your intrest to my problem.
I've changed the work method and I've used this function
void CWWizard::CreateBMPFile(HWND hwnd, LPTSTR pszFile, std::vector<CString> bmpListBmp, HDC hDC) //PBITMAPINFO pbi, HBITMAP hBMP
 { 
    HANDLE hf;                 // file handle  
    BITMAPFILEHEADER hdr;       // bitmap file-header  
    PBITMAPINFOHEADER pbih;     // bitmap info-header  
    LPBYTE lpBits;              // memory pointer  
    DWORD dwTotal;              // total count of bytes  
    DWORD cb;                   // incremental count of bytes  
    BYTE *hp;                   // byte pointer  
    DWORD dwTmp; 

	if((int)bmpListBmp.size() > 0)
	{
		// si liste contient des elements --> Create the .PNG file.  
		hf = CreateFile(pszFile, 
                   GENERIC_READ | GENERIC_WRITE, 
                   (DWORD) 0, 
                    NULL, 
                   CREATE_ALWAYS, 
                   FILE_ATTRIBUTE_NORMAL, 
                   (HANDLE) NULL); 

		if (hf == INVALID_HANDLE_VALUE) 
			errhandler("CreateFile", hwnd); 

		hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"  

		for(int nIdImg = 0; nIdImg < (int)bmpListBmp.size(); nIdImg++)
		{
			//recuperer le hBITMAP et le hederindo de chq image
			HBITMAP hBMP = GetBmpUnifie(bmpListBmp[nIdImg], m_BrosseFondWizard);
			PBITMAPINFO pbi = CreateBitmapInfoStruct(this->m_hWnd, hBMP);

			pbih = (PBITMAPINFOHEADER) pbi; 
			lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

			if (!lpBits) 
				errhandler("GlobalAlloc", hwnd); 

			// Retrieve the color table (RGBQUAD array) and the bits  
			// (array of palette indices) from the DIB.  
			if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS)) 
			{
				errhandler("GetDIBits", hwnd); 
			}

			// Compute the size of the entire file.  
			hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
						 pbih->biSize + pbih->biClrUsed 
						 * sizeof(RGBQUAD) + pbih->biSizeImage); 

			// Compute the offset to the array of color indices.  
			hdr.bfOffBits += (DWORD) sizeof(BITMAPFILEHEADER) + 
						pbih->biSize + pbih->biClrUsed 
						* sizeof (RGBQUAD); 
		
			hdr.bfReserved1 = 0; 
			hdr.bfReserved2 = 0; 

			// Copy the BITMAPFILEHEADER into the .BMP file.  
			if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp,  NULL)) 
			{
				errhandler("WriteFile", hwnd); 
			}

		
			//recuperer le hBITMAP et le hederindo de chq image
			HBITMAP hBMP = GetBmpUnifie(bmpListBmp[nIdImg], m_BrosseFondWizard);
			PBITMAPINFO pbi = CreateBitmapInfoStruct(this->m_hWnd, hBMP);

			pbih = (PBITMAPINFOHEADER) pbi; 
			lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

			// Copy the BITMAPINFOHEADER and RGBQUAD array into the file.  
			if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
						  + pbih->biClrUsed * sizeof (RGBQUAD), 
						  (LPDWORD) &dwTmp, ( NULL)))
				errhandler("WriteFile", hwnd); 

			// Copy the array of color indices into the .BMP file.  
			dwTotal = cb = pbih->biSizeImage; 
			hp = lpBits; 
			if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) 
				   errhandler("WriteFile", hwnd); 
		}//fin for
		 
		// Close the .BMP file.  
		if (!CloseHandle(hf)) 
           errhandler("CloseHandle", hwnd); 

		// Free memory.  
		GlobalFree((HGLOBAL)lpBits);
	}  
}

The problem is that only image file displayed. Normaly there's 3 !.
I know I missing something but I don't know where.
Can you help tell me where I'm wrong.
Many thanks again for your help.
Best Regards.
Mourad.
 
Share this answer
 
For information, I've tried this function
void CWWizard::SaveFile(HDC hdc)
{

	HDC hCompatibleDC = ::CreateCompatibleDC(hdc);

	Gdiplus::Graphics graphics(hCompatibleDC);

	// Create an Image object based on a PNG file. 
	Gdiplus::Image image3(L"D://mehdy//carre.png"); 

	// Create an Image object based on a PNG file. 
	Gdiplus::Image image2(L"D://mehdy//guitare.png"); 

	// Draw the image. 
	graphics.DrawImage(&image3, 0, 0); 

	// Draw the image. 
	graphics.DrawImage(&image2, 0,10); 

	HBITMAP hBmp = HDCtoHBITMAP(hCompatibleDC,350,350);
 
	// *Save* the altered image. 
	CLSID pngClsid; 
	GetEncoderClsid(L"image/png",&pngClsid); 
	//image3.Save(L"D://mehdy//Mosaic2.png",&pngClsid, NULL); 

	 Gdiplus::Bitmap image ((HBITMAP)hBmp, (HPALETTE)0);

	//Gdiplus::Bitmap image = hOldSrcBmp;
	 image.Save(L"D://mehdy//Sortie.png",&pngClsid, NULL);

	 RedrawWindow();

	
}

But, the problem is that I must use a virtual HDC not a HDC of the dialog.
I hope this can clarify the problem.
Thanks again.
Best regards.
Mourad
 
Share this answer
 
Many thanks for replying.
First, I'm using native C++.
What I want to do is displying the final results of my program in an image.
This image will diplay every result as a bitmap (png image).
Sothat, I've to to draw a global image (witch contains all results) and attach to this image some other images who represent evenryone a single result).
I know it is not simple to understand by I hope this can clarify the problem.
Many thanks for helping.
Best regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900