Click here to Skip to main content
15,881,559 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Create new bitmap from oldbitmap Pin
Code-o-mat16-Sep-09 3:33
Code-o-mat16-Sep-09 3:33 
GeneralRe: Create new bitmap from oldbitmap Pin
Game-point16-Sep-09 3:54
Game-point16-Sep-09 3:54 
GeneralRe: Create new bitmap from oldbitmap Pin
Code-o-mat16-Sep-09 4:16
Code-o-mat16-Sep-09 4:16 
GeneralRe: Create new bitmap from oldbitmap Pin
Game-point16-Sep-09 4:22
Game-point16-Sep-09 4:22 
GeneralRe: Create new bitmap from oldbitmap Pin
Code-o-mat16-Sep-09 5:01
Code-o-mat16-Sep-09 5:01 
AnswerRe: Create new bitmap from oldbitmap Pin
Nuri Ismail16-Sep-09 5:02
Nuri Ismail16-Sep-09 5:02 
GeneralRe: Create new bitmap from oldbitmap Pin
Game-point16-Sep-09 20:46
Game-point16-Sep-09 20:46 
GeneralRe: Create new bitmap from oldbitmap [modified] Pin
Nuri Ismail16-Sep-09 22:04
Nuri Ismail16-Sep-09 22:04 
rajugis wrote:
when i gave this rect portion ..the saving images contains ..full black


I think this problem could occur when your original image is indexed. You can read about indexed colors here[^].

If we want to copy indexed images we have to copy their color tables too. We can easily do this with CImage class. Smile | :)
First we have to check if the image is indexed and if it is than copy its color table, just like that:

// Check for color table after the creation of the new image
if(imageOriginal.IsIndexed())
{
	// If there is a color table for the original image
	// copy this table for the new image too.
	const int nMaxColors = imageOriginal.GetMaxColorTableEntries();
	RGBQUAD* pColorTable = new RGBQUAD[nMaxColors];
	imageOriginal.GetColorTable(0, nMaxColors, pColorTable);
	imageNew.SetColorTable(0, nMaxColors, pColorTable);
	delete [] pColorTable;
}


This code must be inserted after the creation of the new image. I've made a test application and it works fine for me for both indexed and on-indexed TIFFs. Here is the complete method:

void CTestCImageAppDlg::OnBnClickedButtonTest()
{
	// First load the original image
	CImage imageOriginal;
	HRESULT hrLoad = imageOriginal.Load(_T("D:\\testImg1.tif"));
	if(FAILED(hrLoad))
	{
		CString msg;
		msg.Format(_T("Error on load. Code:%08x"), hrLoad);
		AfxMessageBox(msg);
		return;
	}
	
	// Here is the rect that we will copy from the original image (in this case 1/4 of it)
	CRect rectCopy(0, 0, imageOriginal.GetWidth() / 2, imageOriginal.GetHeight() / 2);

	// Create the new image
	CImage imageNew;
	imageNew.Create(rectCopy.Width(), rectCopy.Height(), imageOriginal.GetBPP());
	
	// Check the original image for color table 
        // after the creation of the new image
	if(imageOriginal.IsIndexed())
	{
		// If there is a color table for the original image
		// copy this table for the new image too.
		const int nMaxColors = imageOriginal.GetMaxColorTableEntries();
		RGBQUAD* pColorTable = new RGBQUAD[nMaxColors];
		imageOriginal.GetColorTable(0, nMaxColors, pColorTable);
		imageNew.SetColorTable(0, nMaxColors, pColorTable);
		delete [] pColorTable;
	}

        CRect rect(rectCopy);
        rect.OffsetRect(-rectCopy.left, -rectCopy.top);
	
        // Now we can blit the original image on the new image
	imageOriginal.BitBlt(imageNew.GetDC(), rect, rectCopy.TopLeft());

	// It is good to release the DC first
	imageNew.ReleaseDC();


	// Lets save the new image
	HRESULT hrSave = imageNew.Save(_T("D:\\testImg2.tif"), Gdiplus::ImageFormatTIFF);
	if(FAILED(hrSave))
	{
		CString msg;
		msg.Format(_T("Error on save. Code:%08x"),hrSave);
		AfxMessageBox(msg);
		return;
	}
}


I hope this helps you. Smile | :)


Regards
Nuri Ismail

modified on Thursday, September 17, 2009 4:12 AM

GeneralRe: Create new bitmap from oldbitmap Pin
Game-point16-Sep-09 22:36
Game-point16-Sep-09 22:36 
GeneralRe: Create new bitmap from oldbitmap Pin
Nuri Ismail16-Sep-09 22:50
Nuri Ismail16-Sep-09 22:50 
QuestionHow to put MFC View full screen on second monitor? Pin
Jeff Archer16-Sep-09 2:36
Jeff Archer16-Sep-09 2:36 
GeneralRe: How to put MFC View full screen on second monitor? Pin
Iain Clarke, Warrior Programmer16-Sep-09 3:20
Iain Clarke, Warrior Programmer16-Sep-09 3:20 
QuestionMFC app does not show full screen Pin
m_mun16-Sep-09 2:20
m_mun16-Sep-09 2:20 
QuestionRe: MFC app does not show full screen Pin
David Crow16-Sep-09 2:24
David Crow16-Sep-09 2:24 
AnswerRe: MFC app does not show full screen Pin
m_mun16-Sep-09 3:00
m_mun16-Sep-09 3:00 
QuestionNeed help on TAPI3.0 with some sample programs Pin
John50216-Sep-09 1:53
John50216-Sep-09 1:53 
Questionfiles after compilation Pin
hrishiS16-Sep-09 1:35
hrishiS16-Sep-09 1:35 
AnswerRe: files after compilation Pin
Stephen Hewitt16-Sep-09 1:49
Stephen Hewitt16-Sep-09 1:49 
GeneralRe: files after compilation Pin
hrishiS16-Sep-09 2:22
hrishiS16-Sep-09 2:22 
GeneralRe: files after compilation Pin
David Crow16-Sep-09 2:27
David Crow16-Sep-09 2:27 
AnswerRe: files after compilation Pin
Rozis16-Sep-09 13:10
Rozis16-Sep-09 13:10 
Questiontop down and bottom up apporach Pin
hrishiS16-Sep-09 1:30
hrishiS16-Sep-09 1:30 
AnswerRe: top down and bottom up apporach Pin
Nuri Ismail16-Sep-09 1:48
Nuri Ismail16-Sep-09 1:48 
QuestionIn System Tray Icon Ballon not displayed in Windows XP. Pin
Le@rner16-Sep-09 0:41
Le@rner16-Sep-09 0:41 
AnswerRe: In System Tray Icon Ballon not displayed in Windows XP. Pin
Iain Clarke, Warrior Programmer16-Sep-09 1:19
Iain Clarke, Warrior Programmer16-Sep-09 1:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.