Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to save Gradient object as Cbitmap. The Function MakeDiagnolGradientBitmap() saves Diagnol Gradient. I Simply want to save Vertical Gradient.

void CButtonST::MakeDiagnolGradientBitmap()
{
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(&rect);

	int r1 = 245, g1 = 245, b1 = 245;
	int r2 = 230, g2 = 0, b2 = 0;

	int x1 = 0, y1 = 0;
	int x2 = 0, y2 = 0;

	CDC dc2;
	dc2.CreateCompatibleDC(&dc);

	if (m_bitmap.m_hObject)
		m_bitmap.DeleteObject();
	m_bitmap.CreateCompatibleBitmap(&dc, rect.Width(),
		rect.Height());

	CBitmap *oldbmap = dc2.SelectObject(&m_bitmap);
//-----------------------------------------------------------------------
	while (x1 < rect.Width() && y1 < rect.Height())
	{
		if (y1 < rect.Height() - 1)
			y1++;
		else
			x1++;

		if (x2 < rect.Width() - 1)
			x2++;
		else
			y2++;

		int r, g, b;
		int i = x1 + y1;
		r = r1 + (i * (r2 - r1) / (rect.Width() + rect.Height()));
		g = g1 + (i * (g2 - g1) / (rect.Width() + rect.Height()));
		b = b1 + (i * (b2 - b1) / (rect.Width() + rect.Height()));

		CPen p(PS_SOLID, 1, RGB(r, g, b));
		CPen *oldpen = dc2.SelectObject(&p);

		dc2.MoveTo(x1, y1);
		dc2.LineTo(x2, y2);

		dc2.SelectObject(oldpen);
	}
//------------------------------------------------------------------------
	dc2.SelectObject(oldbmap);

}


What I have tried:

void CButtonST::MakeVerticalGradientBitmap()
{
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(&rect);

	int r1 = 245, g1 = 245, b1 = 245;
	int r2 = 230, g2 = 0, b2 = 0;

	int x1 = 0, y1 = 0;
	int x2 = 0, y2 = 0;

	CDC dc2;
	dc2.CreateCompatibleDC(&dc);

	if (m_bitmap.m_hObject)
		m_bitmap.DeleteObject();
	m_bitmap.CreateCompatibleBitmap(&dc, rect.Width(),
		rect.Height());

	CBitmap *oldbmap = dc2.SelectObject(&m_bitmap);
//-----------------------------------------------------------------------
     for(int i=0;i<rect.Height();i++)
{ 
    int r,g,b;
    r = r1 + (i * (r2-r1) / rect.Height());
    g = g1 + (i * (g2-g1) / rect.Height());
    b = b1 + (i * (b2-b1) / rect.Height());
    //pDC->FillSolidRect(0,i,rect.Width(),1,RGB(r,g,b));


		dc2.SelectObject(oldpen);
	}
//------------------------------------------------------------------------
	dc2.SelectObject(oldbmap);

}
Posted
Updated 3-Sep-18 22:14pm
v2

1 solution

 
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