Example #1
0
bool CGdiPlusImage::DrawImage(CDC* pDC)
{
    //´´½¨ÆÁÄ»
    ASSERT(pDC!=NULL); 
    if ( !pDC ) return false; 

    Bitmap bmp(m_rcDest.Width(), m_rcDest.Height()); 
    Graphics* pGraphics = Graphics::FromImage(&bmp); 
    if ( !pGraphics ) return false;

    GraphicsContainer Containter = pGraphics->BeginContainer();
    pGraphics->SetSmoothingMode(SmoothingModeHighQuality); 

    SolidBrush bgbrush(Color(238, 243, 250));
    pGraphics->FillRectangle(&bgbrush, 0, 0, m_rcDest.Width(), m_rcDest.Height());
    
    if ( m_pImage )
    {
        //»ñÈ¡ÊôÐÔ 
        INT nImageWidth  = m_pImage->GetWidth();
        INT nImageHeight = m_pImage->GetHeight(); 


        float fMin = 1.0F; 
        float fZoomRate = 1.0F; 
        bool bZoomOut = false; 
        int nXSrc = 0;
        int nYSrc = 0; 

        if ( nImageWidth >= m_rcDest.Width() || nImageHeight >= m_rcDest.Height() )
        { 
            float fXRate = 1.0F * m_rcDest.Width() / nImageWidth;
            float fYRate = 1.0F * m_rcDest.Height() / nImageHeight;
            fMin = min(fXRate, fYRate);   
            fZoomRate = max(nImageWidth * 1.0F / m_rcDest.Width(), nImageHeight * 1.0F / m_rcDest.Height()); 
            bZoomOut = true;
        } 


        int nDestWidth  = fMin * nImageWidth;
        int nDestHeight = fMin * nImageHeight;


        //¹¹ÔìλÖÃ
        RectF rcDrawRect;  
        rcDrawRect.X = m_rcDest.left + (m_rcDest.Width() - nDestWidth ) / 2;   
        rcDrawRect.Y = m_rcDest.top + (m_rcDest.Height() - nDestHeight ) / 2; 
        rcDrawRect.Width = nDestWidth;
        rcDrawRect.Height = nDestHeight; 

        Matrix matrix;

        CPoint pt(m_rcDest.Width() / 2, m_rcDest.Height() / 2);
        PointF ptBase((float)pt.x, (float)pt.y);  

        //¾ØÕó±ä»»Òª×¢Òâ˳Ðò£¬ÏÈƽÒÆÔÚËõ·ÅºóÐýת
        matrix.Translate((REAL)m_rcDest.left,(REAL)m_rcDest.top,MatrixOrderAppend);

        //Ëõ·Å
        if ( m_szZoom.cx != 1.0f || m_szZoom.cy != 1.0f )
        {
            ptBase.X += m_szZoom.cx;
            ptBase.Y += m_szZoom.cy;

            matrix.Translate(-ptBase.X,-ptBase.Y,MatrixOrderAppend);
            matrix.Scale(m_szZoom.cx,m_szZoom.cy,MatrixOrderAppend);
            matrix.Translate(ptBase.X,ptBase.Y,MatrixOrderAppend);
        }

        //Ðýת
        if (m_nRotateAngle)
        {
            matrix.RotateAt((REAL)m_nRotateAngle,ptBase,MatrixOrderAppend);
        }

        pGraphics->SetTransform(&matrix);

        //͸Ã÷¾ØÕó
        ColorMatrix Matrix=
        {
            1.0f,0.0f,0.0f,0.0f,0.0f, 
            0.0f,1.0f,0.0f,0.0f,0.0f, 
            0.0f,0.0f,1.0f,0.0f,0.0f,
            0.0f,0.0f,0.0f,m_cbAlphaDepth/255.0f,0.0f, 
            0.0f,0.0f,0.0f,0.0f,1.0f
        };

        //ÉèÖÃÊôÐÔ
        ImageAttributes Attributes; 
        Attributes.SetColorMatrix(&Matrix,ColorMatrixFlagsDefault,ColorAdjustTypeBitmap); 

        //»æ»­Í¼Ïñ
        pGraphics->DrawImage(m_pImage, rcDrawRect, nXSrc, nYSrc, (REAL)nImageWidth - nXSrc * 2, (REAL)nImageHeight - nYSrc * 2, UnitPixel,&Attributes);  
        pGraphics->ResetTransform();   
        pGraphics->EndContainer(Containter);

    } // if ( m_pImage )
   
    // ´ÓÄڴ濽±´ÖÁÉ豸
    Graphics graphicsreal(pDC->GetSafeHdc()); 
    graphicsreal.DrawImage(&bmp, 0, 0, m_rcDest.Width(), m_rcDest.Height()); 
    delete pGraphics;
    pGraphics = NULL;

    return true;
}
Example #2
0
//╩Ф╩╜м╪оЯ
bool CGdiPlusImage::DrawImage(CDC * pDC, INT nXPos, INT nYPos)
{
	//╪стьеп╤о
	ASSERT(m_pImage!=NULL);
	if (m_pImage==NULL) return false;

	//╢╢╫╗фад╩
	ASSERT(pDC!=NULL);
	Graphics graphics(pDC->GetSafeHdc());

	GraphicsContainer Containter = graphics.BeginContainer();
	graphics.SetSmoothingMode(SmoothingModeHighQuality);

	//╩Ях║йТпт
	INT nImageWidth=m_pImage->GetWidth();
	INT nImageHeight=m_pImage->GetHeight();

	//╧╧тЛн╩жц
	RectF rcDrawRect;
	rcDrawRect.X=0;//(REAL)nXPos;
	rcDrawRect.Y=0;//(REAL)nYPos;
	rcDrawRect.Width=(REAL)nImageWidth;
	rcDrawRect.Height=(REAL)nImageHeight;

	Matrix matrix;

	CPoint pt((nXPos+nImageWidth/2), (nYPos+nImageHeight/2));
	PointF ptBase((float)pt.x,(float)pt.y);

	//╬ьуС╠Д╩╩р╙в╒рБкЁпРё╛охф╫рфтзкУ╥е╨СпЩв╙
	matrix.Translate((REAL)nXPos,(REAL)nYPos,MatrixOrderAppend);
	
	//кУ╥е
	if ( m_szZoom.cx != 1.0f || m_szZoom.cy != 1.0f )
	{
		ptBase.X += m_szZoom.cx;
		ptBase.Y += m_szZoom.cy;

		matrix.Translate(-ptBase.X,-ptBase.Y,MatrixOrderAppend);
		matrix.Scale(m_szZoom.cx,m_szZoom.cy,MatrixOrderAppend);
		matrix.Translate(ptBase.X,ptBase.Y,MatrixOrderAppend);
	}

	//пЩв╙
	if (m_nRotateAngle)
	{
		matrix.RotateAt((REAL)m_nRotateAngle,ptBase,MatrixOrderAppend);
	}

	graphics.SetTransform(&matrix);

	//м╦цВ╬ьуС
	ColorMatrix Matrix=
	{
		1.0f,0.0f,0.0f,0.0f,0.0f, 
		0.0f,1.0f,0.0f,0.0f,0.0f, 
		0.0f,0.0f,1.0f,0.0f,0.0f,
		0.0f,0.0f,0.0f,m_cbAlphaDepth/255.0f,0.0f, 
		0.0f,0.0f,0.0f,0.0f,1.0f
	};

	//иХжцйТпт
	ImageAttributes Attributes; 
	Attributes.SetColorMatrix(&Matrix,ColorMatrixFlagsDefault,ColorAdjustTypeBitmap); 

	//╩Ф╩╜м╪оЯ
	graphics.DrawImage(m_pImage,rcDrawRect,0,0,(REAL)nImageWidth,(REAL)nImageHeight,UnitPixel,&Attributes);

	graphics.ResetTransform();

	graphics.EndContainer(Containter);

	return true;
}
Example #3
0
//»æ»­Í¼Ïñ
bool CGdiPlusImage::DrawImage(CDC * pDC, INT nXPos, INT nYPos)
{
    //¼ÓÔØÅжÏ
    //ASSERT(m_pImage!=NULL);
    if (m_pImage==NULL) return false;

    //´´½¨ÆÁÄ»
    ASSERT(pDC!=NULL);
    Graphics graphics(pDC->GetSafeHdc());

    GraphicsContainer Containter = graphics.BeginContainer();
    graphics.SetSmoothingMode(SmoothingModeHighQuality);

    //»ñÈ¡ÊôÐÔ
    INT nImageWidth=m_pImage->GetWidth();
    INT nImageHeight=m_pImage->GetHeight();

    //¹¹ÔìλÖÃ
    RectF rcDrawRect;
    rcDrawRect.X=0;//(REAL)nXPos;
    rcDrawRect.Y=0;//(REAL)nYPos;
    rcDrawRect.Width=(REAL)nImageWidth;
    rcDrawRect.Height=(REAL)nImageHeight;

    Matrix matrix;

    CPoint pt((nXPos+nImageWidth/2), (nYPos+nImageHeight/2));
    PointF ptBase((float)pt.x,(float)pt.y);

    //¾ØÕó±ä»»Òª×¢Òâ˳Ðò£¬ÏÈƽÒÆÔÚËõ·ÅºóÐýת
    matrix.Translate((REAL)nXPos,(REAL)nYPos,MatrixOrderAppend);

    //Ëõ·Å
    if ( m_szZoom.cx != 1.0f || m_szZoom.cy != 1.0f )
    {
        ptBase.X += m_szZoom.cx;
        ptBase.Y += m_szZoom.cy;

        matrix.Translate(-ptBase.X,-ptBase.Y,MatrixOrderAppend);
        matrix.Scale(m_szZoom.cx,m_szZoom.cy,MatrixOrderAppend);
        matrix.Translate(ptBase.X,ptBase.Y,MatrixOrderAppend);
    }

    //Ðýת
    if (m_nRotateAngle)
    {
        matrix.RotateAt((REAL)m_nRotateAngle,ptBase,MatrixOrderAppend);
    }

    graphics.SetTransform(&matrix);

    //͸Ã÷¾ØÕó
    ColorMatrix Matrix=
    {
        1.0f,0.0f,0.0f,0.0f,0.0f, 
        0.0f,1.0f,0.0f,0.0f,0.0f, 
        0.0f,0.0f,1.0f,0.0f,0.0f,
        0.0f,0.0f,0.0f,m_cbAlphaDepth/255.0f,0.0f, 
        0.0f,0.0f,0.0f,0.0f,1.0f
    };

    //ÉèÖÃÊôÐÔ
    ImageAttributes Attributes; 
    Attributes.SetColorMatrix(&Matrix,ColorMatrixFlagsDefault,ColorAdjustTypeBitmap); 

    //»æ»­Í¼Ïñ
    graphics.DrawImage(m_pImage,rcDrawRect,0,0,(REAL)nImageWidth,(REAL)nImageHeight,UnitPixel,&Attributes);

    graphics.ResetTransform();

    graphics.EndContainer(Containter);

    return true;
}