// ************************************************************* // GetImageAttributes() // ************************************************************* Gdiplus::ImageAttributes* ImageHelper::GetImageAttributes(IImage* img) { if (!img) return NULL; Gdiplus::ImageAttributes* attr = new Gdiplus::ImageAttributes(); attr->SetWrapMode(Gdiplus::WrapModeTileFlipXY); VARIANT_BOOL useTransparency; img->get_UseTransparencyColor(&useTransparency); OLE_COLOR clr1, clr2; img->get_TransparencyColor(&clr1); img->get_TransparencyColor2(&clr2); SetTransparency(*attr, useTransparency ? true : false, clr1, clr2); Gdiplus::ColorMatrix m = GetColorMatrix(img); attr->SetColorMatrix(&m); Gdiplus::REAL gamma; img->get_Gamma(&gamma); attr->SetGamma(gamma); return attr; }
void CanvasGdiplus::RestoreLayer() { DCHECK(layers_.size()); CanvasLayer* layer = layers_.top(); layers_.pop(); DCHECK(layer); Gdiplus::Graphics* current = GetCurrentGraphics(); Gdiplus::Bitmap* native_bitmap = layer->mem_bitmap.GetNativeBitmap(); Gdiplus::ImageAttributes ia; Gdiplus::ColorMatrix cm = { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, (static_cast<float>(layer->alpha))/255, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; ia.SetColorMatrix(&cm); Gdiplus::Rect destRect(layer->bounds.x(), layer->bounds.y(), layer->bounds.width(), layer->bounds.height()); current->DrawImage(native_bitmap, destRect, layer->bounds.x(), layer->bounds.y(), layer->bounds.width(), layer->bounds.height(), Gdiplus::UnitPixel, &ia); delete layer; }
void ImageAttributes::SetColorMatrix(const ColorMatrix* cm) { Gdiplus::ColorMatrix gdiMatrix; for(int a = 0; a < 5; a++) { for(int b = 0; b < 5; b++) { gdiMatrix.m[a][b] = cm->m[a][b]; } } Gdiplus::ImageAttributes* ia = reinterpret_cast<Gdiplus::ImageAttributes*>(_private); ia->SetColorMatrix(&gdiMatrix); }
void Splash::DrawPNG(PNG* lpPNG, ColorMatrix* cmMatrix) { // Check alpha if(lpPNG->fAlpha <= 0.0f) return; // Check cmMatrix for null if(cmMatrix == NULL) cmMatrix = &ClrMatrix; // Set alpha cmMatrix->m[3][3] = lpPNG->fAlpha; Gdiplus::ImageAttributes iaAttr; iaAttr.SetColorMatrix(cmMatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap); // Draw _oGrphInf.graphics->DrawImage(lpPNG->GetImage(), *lpPNG->frcDest, lpPNG->frcSrc->X, lpPNG->frcSrc->Y, lpPNG->frcSrc->Width, lpPNG->frcSrc->Height, Gdiplus::UnitPixel, &iaAttr); _oGrphInf.graphics->Flush(); }
// ************************************************************* // GetImageAttributes() // ************************************************************* Gdiplus::ImageAttributes* ImageHelper::GetImageAttributes(float alpha, bool useTransparency, OLE_COLOR clr1, OLE_COLOR clr2) { Gdiplus::ImageAttributes* attr = new Gdiplus::ImageAttributes(); attr->SetWrapMode(Gdiplus::WrapModeTileFlipXY); Gdiplus::ColorMatrix matrix; ZeroMemory((void*)&matrix.m, 25 * sizeof(REAL)); for (int i = 0; i < 5; i++) matrix.m[i][i] = 1.0f; matrix.m[3][3] = alpha; attr->SetColorMatrix(&matrix); SetTransparency(*attr, useTransparency, clr1, clr2); return attr; }
BOOL CDuiImgX::StretchBlt(HDC hdc,int x,int y,int nWid,int nHei,int xSrc,int ySrc,int nWidSrc,int nHeiSrc,BYTE byAlpha/*=0xFF*/) { if(IsEmpty()) return FALSE; Gdiplus::Graphics graphics(hdc); Gdiplus::ImageAttributes ImgAtt; ImgAtt.SetWrapMode(Gdiplus::WrapModeTileFlipXY); if(byAlpha!=0xFF) { Gdiplus::ColorMatrix ClrMatrix = { 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, float(byAlpha)/0xFF, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; ImgAtt.SetColorMatrix(&ClrMatrix); } return S_OK==graphics.DrawImage(m_pImg,Gdiplus::Rect(x,y,nWid,nHei),xSrc,ySrc,nWidSrc,nHeiSrc,Gdiplus::UnitPixel,&ImgAtt); }
//----------------------------------------------------------------------------- void GdiplusDrawContext::drawBitmap (CBitmap* cbitmap, const CRect& dest, const CPoint& offset, float alpha) { alpha *= currentState.globalAlpha; if (alpha == 0.f || pGraphics == 0) return; IPlatformBitmap* platformBitmap = cbitmap ? cbitmap->getPlatformBitmap () : 0; GdiplusBitmap* gpb = platformBitmap ? dynamic_cast<GdiplusBitmap*> (platformBitmap) : 0; Gdiplus::Bitmap* bitmap = gpb ? gpb->getBitmap () : 0; if (bitmap) { GdiplusDrawScope drawScope (pGraphics, currentState.clipRect, getCurrentTransform ()); Gdiplus::ImageAttributes imageAtt; if (alpha != 1.f) { // introducing the alpha blend matrix Gdiplus::ColorMatrix colorMatrix = { 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, alpha, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, }; // create the imageattribute modifier imageAtt.SetColorMatrix (&colorMatrix, Gdiplus::ColorMatrixFlagsDefault, Gdiplus::ColorAdjustTypeBitmap); #if 1 Gdiplus::Rect myDestRect( (INT)dest.left, (INT)dest.top, (INT)dest.getWidth (), (INT)dest.getHeight ()); pGraphics->DrawImage ( bitmap, myDestRect, (INT)offset.x, (INT)offset.y, (INT)dest.getWidth (), (INT)dest.getHeight (), Gdiplus::UnitPixel, &imageAtt); #else // create a temporary bitmap to prevent OutOfMemory errors Gdiplus::Bitmap myBitmapBuffer ((INT)dest.getWidth (), (INT)dest.getHeight (),PixelFormat32bppARGB); // create a graphics context for the temporary bitmap Gdiplus::Graphics* myGraphicsBuffer = Gdiplus::Graphics::FromImage (&myBitmapBuffer); // copy the rectangle we want to paint to the temporary bitmap Gdiplus::Rect myTransRect( 0, 0, (INT)dest.getWidth (), (INT)dest.getHeight ()); // transfer the bitmap (without modification by imageattr!) myGraphicsBuffer->DrawImage ( bitmap,myTransRect, (INT)offset.x, (INT)offset.y, (INT)dest.getWidth (), (INT)dest.getHeight (), Gdiplus::UnitPixel, 0); // now transfer the temporary to the real context at the advised location Gdiplus::Rect myDestRect ( (INT)dest.left, (INT)dest.top, (INT)dest.getWidth (), (INT)dest.getHeight ()); // transfer from temporary bitmap to real context (with imageattr) pGraphics->DrawImage ( &myBitmapBuffer, myDestRect, (INT)0, (INT)0, (INT)dest.getWidth (), (INT)dest.getHeight (), Gdiplus::UnitPixel, &imageAtt); // delete the temporary context of the temporary bitmap delete myGraphicsBuffer; #endif } else { Gdiplus::Rect myDestRect( (INT)dest.left, (INT)dest.top, (INT)dest.getWidth (), (INT)dest.getHeight ()); pGraphics->DrawImage ( bitmap, myDestRect, (INT)offset.x, (INT)offset.y, (INT)dest.getWidth (), (INT)dest.getHeight (), Gdiplus::UnitPixel, 0); } } }
HRESULT CImageImpl::Render(HDC hDC, LPCRECT pDstRect, LPCRECT pSrcRect, LPCRECT pBoundRect) { USE_CLASS_DATA(CLASS_DATA_MEMBER_LIST); UNREFERENCED_PARAMETER(pSrcRect); UNREFERENCED_PARAMETER(pBoundRect); if (m_pImage == NULL) return S_FALSE; BOOL bGray = m_pImage->IsGrayScale(); UINT uFrames = 0; if (S_OK == GetNumFrames(&uFrames) && uFrames > 1) { if (bGray) { //填充白色 HBRUSH hBrush = ::CreateSolidBrush(RGB(255, 255, 255)); ::FillRect(hDC, pDstRect, hBrush); ::DeleteObject(hBrush); try { //去alpha矩阵,未实现 static Gdiplus::ColorMatrix removeAlphaMatrix = { 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, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, }; //去色矩阵 static Gdiplus::ColorMatrix grayMatrix = { 0.3f, 0.3f, 0.3f, 0.0f, 0.0f, 0.59f, 0.59f, 0.59f, 0.0f, 0.0f, 0.11f, 0.11f, 0.11f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, }; //选择第一帧 GUID guidGif = Gdiplus::FrameDimensionTime; m_pBitmap->SelectActiveFrame(&guidGif, 0); CRect dstRect = *pDstRect; Gdiplus::Rect rcDraw(dstRect.left, dstRect.top, (dstRect.right - dstRect.left), (dstRect.bottom - dstRect.top)); Gdiplus::Graphics graphics(hDC); Gdiplus::ImageAttributes *pImageAttributes = new Gdiplus::ImageAttributes(); pImageAttributes->SetColorMatrix(&grayMatrix); Gdiplus::Status ret = graphics.DrawImage(m_pBitmap, rcDraw, 0, 0, m_pBitmap->GetWidth(), m_pBitmap->GetHeight(), Gdiplus::UnitPixel, pImageAttributes); delete pImageAttributes; if (ret == Gdiplus::Ok) return S_OK; } catch (...) { } } else { //填充白色 ::FillRect(hDC, pDstRect, (HBRUSH)::GetStockObject(WHITE_BRUSH)); if (S_OK == RenderFrame(hDC, 0, pDstRect)) return S_OK; } } if (! m_pImage->AlphaIsValid()) { if (! m_pImage->Stretch(hDC, *pDstRect)) return S_FALSE; } else { if (! m_pImage->Draw(hDC, *pDstRect)) return S_FALSE; } return S_OK; }