void CVideoPane::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here // Do not call CDialog::OnPaint() for painting messages // 绘制标题栏 if (NULL != m_pImgCaption) { // 设置坐标 CRect rect; GetClientRect(rect); Gdiplus::Rect gRect(rect.left, rect.top, rect.right, CAPTION_HEIGHT); Gdiplus::ImageAttributes ImgAtt; ImgAtt.SetWrapMode(Gdiplus::WrapModeTileFlipXY); Gdiplus::Graphics graphics(dc); graphics.DrawImage(m_pImgCaption, gRect, 0, 0, (INT)m_pImgCaption->GetWidth(), (INT)m_pImgCaption->GetHeight(), Gdiplus::UnitPixel, &ImgAtt); } // 标题栏文本 CRect rect; GetClientRect(rect); rect.left += BTN_CLOSE_SPACE; rect.bottom = CAPTION_HEIGHT; dc.SetBkMode(CAPTION_FONT_BKCLR); dc.SetTextColor(CAPTION_FONT_COLOR); CFont font; font.CreateFont(CAPTION_FONT_SIZE, 0, 0, 0, FW_NORMAL, FALSE,FALSE,FALSE,0,0,0,0,0,CAPTION_FONT_NAME); dc.SelectObject(font); dc.DrawText(m_strCaptionText, -1, &rect, DT_SINGLELINE | DT_VCENTER); }
// ************************************************************* // 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; }
bool CInstallDesktopTipWnd::LoadBkgImage() { HRSRC hRes = FindResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_PNG_NEW), _T("png")); if(hRes == NULL) { return false; } DWORD dwResSize = SizeofResource(AfxGetInstanceHandle(), hRes); if(dwResSize <= 0) { return false; } HGLOBAL hResData = LoadResource(AfxGetInstanceHandle(), hRes); if(hResData == NULL) { return false; } LPVOID lpResourceData = LockResource(hResData); HGLOBAL hResourceBuffer = GlobalAlloc(GMEM_MOVEABLE, dwResSize); LPVOID lpResourceBuffer = GlobalLock(hResourceBuffer); CopyMemory(lpResourceBuffer, lpResourceData, dwResSize); IStream* piStream = NULL; if(CreateStreamOnHGlobal(hResourceBuffer, FALSE, &piStream) == S_OK) { m_Image = Image::FromStream(piStream); piStream->Release(); } GlobalUnlock(hResourceBuffer); GlobalFree(hResourceBuffer); FreeResource(hResData); ATLASSERT(m_Image); m_ImageWidth = m_Image->GetWidth(); m_ImageHeight = m_Image->GetHeight(); m_ImageMemBitmap = CreateDefaultDIBSection(m_ImageWidth, m_ImageHeight); m_ImageMemDC = ::CreateCompatibleDC(NULL); ::SelectObject(m_ImageMemDC, m_ImageMemBitmap); Gdiplus::Graphics memGraphics(m_ImageMemDC); memGraphics.SetCompositingMode(CompositingModeSourceOver); memGraphics.SetCompositingQuality(CompositingQualityHighSpeed); BitBlt(m_ImageMemDC, 0, 0, m_ImageWidth, m_ImageHeight, NULL, 0, 0, BLACKNESS); Gdiplus::ImageAttributes imAtt; imAtt.SetWrapMode(Gdiplus::WrapModeTileFlipXY); memGraphics.DrawImage(m_Image, Gdiplus::Rect(0, 0, m_ImageWidth, m_ImageHeight), 0, 0, m_ImageWidth, m_ImageHeight, Gdiplus::UnitPixel, &imAtt, NULL, NULL); return true; }
void CVideoStatic::SetBackGroudImage(const CPaintDC& dc) { // ¼ÓÔر³¾°logoͼƬ if (NULL == m_pImage) { m_pImage = Gdiplus::Image::FromFile(m_strLogoPath); if (NULL == m_pImage) { LOG_RUN_ERROR("m_pImage is null."); return; } else { if (Gdiplus::Ok != m_pImage->GetLastStatus()) { std::string strPath = eLTE_Tool::UnicodeToANSI(m_strLogoPath.GetString()).c_str(); LOG_RUN_ERROR("Gdiplus::Image::FromFile failed. (%s)", strPath.c_str()); m_pImage = NULL; return; } } } // ¾ÓÖÐλÖüÆËã CRect rect; GetClientRect(&rect); int iWidth = (int)m_pImage->GetWidth(); int iHeight = (int)m_pImage->GetHeight(); if ((double)rect.right/rect.bottom > (double)iWidth/iHeight) { iHeight = rect.bottom / 3; iWidth = iHeight * (int)m_pImage->GetWidth() / (int)m_pImage->GetHeight(); } else { iWidth = rect.right / 3; iHeight = iWidth * (int)m_pImage->GetHeight() / (int)m_pImage->GetWidth(); } int iLeft = (rect.right - iWidth) / 2; int iTop = (rect.bottom - iHeight) / 2; // »æÖÆͼƬ Gdiplus::Graphics graphics(dc); Gdiplus::Rect gRect(iLeft, iTop, iWidth, iHeight); Gdiplus::ImageAttributes ImgAtt; ImgAtt.SetWrapMode(Gdiplus::WrapModeTileFlipXY); graphics.DrawImage(m_pImage, gRect, 0, 0, (INT)m_pImage->GetWidth(), (INT)m_pImage->GetHeight(), Gdiplus::UnitPixel, &ImgAtt); }
BOOL CBSObject::DrawPNG(HDC hDC, const RECT &rectDst, Gdiplus::Image* pImage, const RECT* prectImagePortion /*= NULL*/) { BOOL bRet = FALSE; if (pImage > 0) { Gdiplus::Graphics graphics(hDC); Gdiplus::RectF cRectF(static_cast<Gdiplus::REAL>(rectDst.left), static_cast<Gdiplus::REAL>(rectDst.top), static_cast<Gdiplus::REAL>(rectDst.right - rectDst.left), static_cast<Gdiplus::REAL>(rectDst.bottom - rectDst.top)); Gdiplus::ImageAttributes clImgAtt; clImgAtt.SetWrapMode(Gdiplus::WrapModeTileFlipXY); if (NULL == prectImagePortion) { graphics.DrawImage(pImage, cRectF, static_cast<Gdiplus::REAL>(0), static_cast<Gdiplus::REAL>(0), static_cast<Gdiplus::REAL>(pImage->GetWidth()), static_cast<Gdiplus::REAL>(pImage->GetHeight()), Gdiplus::UnitPixel, &clImgAtt); // } else { graphics.DrawImage(pImage, cRectF, static_cast<Gdiplus::REAL>(prectImagePortion->left), static_cast<Gdiplus::REAL>(prectImagePortion->top), static_cast<Gdiplus::REAL>(prectImagePortion->right - prectImagePortion->left), static_cast<Gdiplus::REAL>(prectImagePortion->bottom - prectImagePortion->top), Gdiplus::Unit::UnitPixel, NULL, //&clImgAtt NULL, NULL); } bRet = TRUE; } return bRet; }
// ************************************************************* // 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; }
Gdiplus::Bitmap* CSkinUnitODL::CreateImageData( INT nGroutX, INT nGroutY, INT nSrcWidth, INT nSrcHeight, Gdiplus::Color clBackground, CString strPicPath ) { //定义目标图片:代表宽度、高度,水泥槽宽度 Gdiplus::Bitmap *img=new Gdiplus::Bitmap(nSrcWidth + nGroutX, nSrcHeight + nGroutY); //计算点的最大最小位置 Gdiplus::Bitmap bmp(strPicPath); Gdiplus::Graphics buffer(img);//Gaphics对象引用内存画布 buffer.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);//SmoothingModeAntiAlias); buffer.ResetTransform(); buffer.Clear(clBackground); { Gdiplus::ImageAttributes attImage; attImage.SetWrapMode(Gdiplus::WrapMode::WrapModeTile); Gdiplus::Rect rtDraw(nGroutX/2, nGroutY/2, nSrcWidth, nSrcHeight); //图片原始尺寸在bmp内,画到 x,y点, 需要原始图片的位置是 srcX, srcY, 目标图形区域大小是 srcWidth, srcHeight buffer.DrawImage(&bmp, rtDraw, 0, 0, bmp.GetWidth(), bmp.GetHeight(), Gdiplus::Unit::UnitPixel, &attImage); } return img; }
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 CanvasGdiplus::TileImageInt(const Bitmap& bitmap, int src_x, int src_y, int dest_x, int dest_y, int w, int h) { if(!IntersectsClipRectInt(dest_x, dest_y, w, h)) { return; } Gdiplus::Graphics* current = GetCurrentGraphics(); if(!bitmap.IsNull()) { Gdiplus::Bitmap* native_bitmap = bitmap.GetNativeBitmap(); Gdiplus::ImageAttributes ia; ia.SetWrapMode(Gdiplus::WrapModeTile); Gdiplus::Rect rc_dest(dest_x, dest_y, w, h); current->DrawImage(native_bitmap, rc_dest, src_x, src_y, native_bitmap->GetWidth()-src_x, native_bitmap->GetHeight()-src_y, Gdiplus::UnitPixel, &ia); } }
HBITMAP g_create_hbitmap_from_image(Gdiplus::Bitmap & bm, t_size & cx, t_size & cy, COLORREF cr_back, bool b_reflection) { HDC dc=NULL, dcc=NULL; dc = GetDC(NULL); dcc = CreateCompatibleDC(dc); //cy = bm.GetHeight(); if (b_reflection) cy = cx;//(cy*11 -7) / 14; t_size ocx=cx,ocy=cy; t_size cx_source = bm.GetWidth(), cy_source = bm.GetHeight(); double ar_source = (double)cx_source / (double)cy_source; double ar_dest = (double)ocx / (double)ocy; //unsigned cx = RECT_CX(rc), cy = RECT_CY(rc); if (ar_dest < ar_source) cy = (unsigned)floor((double)ocx / ar_source); else if (ar_dest > ar_source) cx = (unsigned)floor((double)ocy * ar_source); //cy = (unsigned)floor((double)ocx / ar_source); if ( (ocx - cx) % 2) cx++; t_size reflect_cy = b_reflection ? (cy*3)/11 : 0; HBITMAP bitmap = CreateCompatibleBitmap(dc, cx, cy + reflect_cy); HBITMAP bm_old = SelectBitmap(dcc, bitmap); unsigned err = 0; Gdiplus::Graphics graphics(dcc); err = graphics.GetLastStatus(); graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHighQuality); graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic); Gdiplus::SolidBrush br(Gdiplus::Color(LOBYTE(LOWORD(cr_back)),HIBYTE(LOWORD(cr_back)),LOBYTE(HIWORD(cr_back)) )); graphics.FillRectangle(&br, 0, 0, cx, cy + reflect_cy); //if (cx_source>=2 && cy_source>=2) { { #if 1 Gdiplus::ImageAttributes imageAttributes; imageAttributes.SetWrapMode(Gdiplus::WrapModeTileFlipXY); Gdiplus::Rect destRect(0, 0, cx, cy); graphics.DrawImage(&bm, destRect, 0, 0, cx_source, cy_source, Gdiplus::UnitPixel, &imageAttributes); #else if (cx_source == cx && cy_source == cy) { Gdiplus::Rect destRect(0, 0, cx, cy); graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor); graphics.DrawImage(&bm, destRect, 0, 0, cx_source, cy_source, Gdiplus::UnitPixel); graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic); } else { Gdiplus::Rect destRect(-1, -1, cx+2, cy+2); //Gdiplus::Rect destRect(0, 0, cx, cy); graphics.DrawImage(&bm, destRect, 0, 0, cx_source, cy_source, Gdiplus::UnitPixel); } #endif } { Gdiplus::Bitmap scaled(bitmap, NULL); t_size i; if (reflect_cy) { Gdiplus::Rect rectref(0, cy, cx, reflect_cy); Gdiplus::Color cr_end(255,LOBYTE(LOWORD(cr_back)),HIBYTE(LOWORD(cr_back)),LOBYTE(HIWORD(cr_back))); Gdiplus::Color cr_start(148,LOBYTE(LOWORD(cr_back)),HIBYTE(LOWORD(cr_back)),LOBYTE(HIWORD(cr_back))); //Gdiplus::Color cr_middle(100,255,255,255); Gdiplus::Rect destRect(0, cy, cx, reflect_cy); graphics.DrawImage(&scaled, destRect, 0, cy, cx, 0-reflect_cy, Gdiplus::UnitPixel); Gdiplus::LinearGradientBrush lgb(rectref, cr_start, cr_end, Gdiplus::LinearGradientModeVertical); graphics.FillRectangle(&lgb, rectref); //graphics.FillRectangle(&Gdiplus::SolidBrush(cr_middle), rectref); /*for (i=0; i<reflect_cy; i++) { Gdiplus::ImageAttributes attrib; Gdiplus::ColorMatrix mtrx = {0}; mtrx.m[0][0] = 1; mtrx.m[1][1] = 1; mtrx.m[2][2] = 1; mtrx.m[3][3] = float(0.42) - (float(0.42)*(float(i)/float(reflect_cy))); mtrx.m[4][4] = 1; attrib.SetColorMatrix (&mtrx, Gdiplus::ColorMatrixFlagsDefault, Gdiplus::ColorAdjustTypeDefault); //Gdiplus::Rect sourceRect(0, cy-1-i, cx, cy-i); Gdiplus::Rect destRect(0, cy+i, cx, 1); graphics.DrawImage(&scaled, destRect, 0, cy-i-1, cx, 1, Gdiplus::UnitPixel, &attrib); }*/ } } err = graphics.GetLastStatus(); } //m_bitmap = pfc::rcnew_t<Gdiplus::CachedBitmap>(&bm, &_graphics); //err = m_bitmap->GetLastStatus(); SelectBitmap(dcc, bm_old); DeleteDC(dcc); ReleaseDC(NULL, dc); return bitmap; }
BOOL CBSObject::DrawPNG(HDC hDC, const RECT &rectDst, const RECT* prectImagePortion, UINT Res, HINSTANCE hInst) { BOOL bRet = FALSE; Gdiplus::Graphics graphics(hDC); HRSRC hResource = ::FindResource(hInst, MAKEINTRESOURCE(Res), TEXT("PNG")); if (hResource > 0) { DWORD imageSize = ::SizeofResource(hInst, hResource); if (imageSize > 0) { HGLOBAL hHandle = ::LoadResource(hInst,hResource); if (NULL != hHandle) { void* pResourceData = ::LockResource(hHandle); if (pResourceData != NULL) { HGLOBAL hBuffer; hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, imageSize); if (hBuffer) { void* pBuffer = ::GlobalLock(hBuffer); if (pBuffer) { memcpy_s(pBuffer, imageSize, pResourceData, imageSize); //For stm0117 IStream* pStream = NULL; if (::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream) == S_OK) { Gdiplus::Image cImage(pStream); Gdiplus::RectF cRectF(static_cast<Gdiplus::REAL>(rectDst.left), static_cast<Gdiplus::REAL>(rectDst.top), static_cast<Gdiplus::REAL>(rectDst.right - rectDst.left), static_cast<Gdiplus::REAL>(rectDst.bottom - rectDst.top)); Gdiplus::ImageAttributes clImgAtt; clImgAtt.SetWrapMode(Gdiplus::WrapModeTileFlipXY); if (NULL == prectImagePortion) { graphics.DrawImage(&cImage, cRectF, static_cast<Gdiplus::REAL>(0), static_cast<Gdiplus::REAL>(0), static_cast<Gdiplus::REAL>(cImage.GetWidth()), static_cast<Gdiplus::REAL>(cImage.GetHeight()), Gdiplus::UnitPixel, &clImgAtt); // } else { graphics.DrawImage(&cImage, cRectF, static_cast<Gdiplus::REAL>(prectImagePortion->left), static_cast<Gdiplus::REAL>(prectImagePortion->top), static_cast<Gdiplus::REAL>(prectImagePortion->right - prectImagePortion->left), static_cast<Gdiplus::REAL>(prectImagePortion->bottom - prectImagePortion->top), Gdiplus::Unit::UnitPixel, NULL, NULL, NULL); } pStream->Release(); bRet = TRUE; } ::GlobalUnlock(hBuffer); } ::GlobalFree(hBuffer); hBuffer = NULL; } } } } } return bRet; }
HBITMAP g_load_png_gdiplus_throw(HDC dc, const char * fn, unsigned target_cx, unsigned target_cy) { //FIXME m_gdiplus_initialised = (Gdiplus::Ok == Gdiplus::GdiplusStartup(&m_gdiplus_instance, &Gdiplus::GdiplusStartupInput(), NULL)); pfc::string8 canPath; HBITMAP bm = 0; abort_callback_dummy p_abort; file::ptr p_file; filesystem::g_get_canonical_path(fn, canPath); filesystem::g_open_read(p_file, canPath, p_abort); t_size fsize = pfc::downcast_guarded<t_size>(p_file->get_size_ex(p_abort)); pfc::array_staticsize_t<t_uint8> buffer(fsize); p_file->read(buffer.get_ptr(), fsize, p_abort); p_file.release(); IStream *pStream = NULL; HGLOBAL gd = GlobalAlloc(GMEM_FIXED | GMEM_MOVEABLE, buffer.get_size()); if (gd == NULL) throw exception_win32(GetLastError()); void * p_data = GlobalLock(gd); if (p_data == NULL) { GlobalFree(gd); throw exception_win32(GetLastError()); } memcpy(p_data, buffer.get_ptr(), buffer.get_size()); GlobalUnlock(gd); HRESULT hr = CreateStreamOnHGlobal(gd, TRUE, &pStream); if (FAILED(hr)) { GlobalFree(gd); throw exception_win32(hr); } Gdiplus::Bitmap pImage(pStream); CheckGdiplusStatus() << pImage.GetLastStatus(); { Gdiplus::BitmapData bitmapData; //Gdiplus::Bitmap * ppImage = &pImage; if (target_cx != pfc_infinite || target_cy != pfc_infinite) { Gdiplus::Bitmap pBitmapResized(target_cx == pfc_infinite ? pImage.GetWidth() : target_cx, target_cy == pfc_infinite ? pImage.GetHeight() : target_cy, PixelFormat32bppARGB); CheckGdiplusStatus() << pBitmapResized.GetLastStatus(); Gdiplus::Graphics pGraphics(&pBitmapResized); CheckGdiplusStatus() << pGraphics.GetLastStatus(); CheckGdiplusStatus() << pGraphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHighQuality); CheckGdiplusStatus() << pGraphics.SetInterpolationMode(Gdiplus::InterpolationModeBicubic); Gdiplus::ImageAttributes imageAttributes; CheckGdiplusStatus() << imageAttributes.SetWrapMode(Gdiplus::WrapModeTileFlipXY); CheckGdiplusStatus() << pGraphics.DrawImage(&pImage, Gdiplus::Rect(0, 0, pBitmapResized.GetWidth(), pBitmapResized.GetHeight()), 0, 0, pImage.GetWidth(), pImage.GetHeight(), Gdiplus::UnitPixel, &imageAttributes); CheckGdiplusStatus() << pBitmapResized.LockBits(&Gdiplus::Rect(0, 0, pBitmapResized.GetWidth(), pBitmapResized.GetHeight()), Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &bitmapData); bm = g_CreateHbitmapFromGdiplusBitmapData32bpp(bitmapData); CheckGdiplusStatus() << pBitmapResized.UnlockBits(&bitmapData); } else { CheckGdiplusStatus() << pImage.LockBits(&Gdiplus::Rect(0, 0, pImage.GetWidth(), pImage.GetHeight()), Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &bitmapData); //assert bitmapData.Stride == bitmapData.Width bm = g_CreateHbitmapFromGdiplusBitmapData32bpp(bitmapData); CheckGdiplusStatus() << pImage.UnlockBits(&bitmapData); } } return bm; }