Question : Draw a line in CImage object

I have used CImage class to create a 8-bits mask object. Now I am going to draw lines on this mask using a method called "DrawTrack (int xStart, int yStart, int xEnd, int yEnd)". My problem is, After I use LineTo method to draw the line, I cannot see this line is drawn in the mask picture. Anyone know the reason?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
BOOL CMapBuilder::DrawTrack(int xStart, int yStart, int xEnd, int yEnd)
{
	BOOL ret = FALSE;
	HDC hDC = NULL;
	hDC = m_TrackMask.GetDC();
	CDC* memoryDC = CDC::FromHandle(hDC);
	
	memoryDC->SetDCPenColor((COLORREF)255);
	memoryDC->MoveTo(xStart, yStart);
	ret  = memoryDC->LineTo(xEnd, yEnd);
	m_TrackMask.ReleaseDC();
	//COLORREF color = m_TrackMask.GetPixel(0, 0); // For testing
	return ret;
}

Answer : Draw a line in CImage object

And I have fixed your code.

You need to select a pen in the DC.

pDC->SelectObject(GetStockObject(DC_PEN));

This line pDC->SetDCPenColor(RGB(255, 0, 0)); just set a color for a pen selected in DC.

1:
2:
3:
4:
5:
6:
7:
8:
9:
void CCImage_DCDlg::AddLine()
{
	HDC hDC = m_Image.GetDC();
	CDC* pDC = CDC::FromHandle(hDC);
	pDC->SetDCPenColor(RGB(255, 0, 0));
	pDC->SelectObject(GetStockObject(DC_PEN));
	pDC->MoveTo(0, 0);
	pDC->LineTo(200, 200);
}
Random Solutions  
 
programming4us programming4us