// description: load image from file using gdi+ NaImage * NaImage::Load(const wchar_t * filename) { NaImage *pImage = new NaImage; HDC hDC = NaScreenModule::GetDesktopDC(); pImage->m_hMemoryDC = ::CreateCompatibleDC(hDC); // initialize gdi+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); // load image Gdiplus::Image* pGdiImage = new Gdiplus::Image(filename); // converting to bitmap Gdiplus::Bitmap* pGdiBitmap = static_cast<Gdiplus::Bitmap*>(pGdiImage); pGdiBitmap->GetHBITMAP(Gdiplus::Color(0, 0, 0), &pImage->m_hBitmap); pImage->m_rc.left = 0; pImage->m_rc.top = 0; pImage->m_rc.right = pGdiImage->GetWidth(); pImage->m_rc.bottom = pGdiImage->GetHeight(); // shutdown gdi+ delete pGdiImage; Gdiplus::GdiplusShutdown(gdiplusToken); return pImage; }
BOOL COBSButton::ChangeSubPic(STATE index, LPCTSTR lpszImage) { if (m_hWnd == NULL || lpszImage == NULL) { return FALSE; } Gdiplus::Image* pImage = Gdiplus::Image::FromFile(lpszImage); if ((pImage == NULL) || (pImage->GetLastStatus() != Gdiplus::Ok)) { if (pImage) { delete pImage; pImage = NULL; } return FALSE; } int nPos = pImage->GetWidth()*index;//¼ÆËã4ÕÅͼƬÐèÒª¶àÉÙÏñËØ Gdiplus::Graphics graph(m_hdcMemory); graph.SetSmoothingMode(Gdiplus::SmoothingModeNone); graph.DrawImage(pImage, nPos, 0, pImage->GetWidth(), pImage->GetHeight()); return TRUE; }
/** Blend 'A''B''C''D' sign onto the image. */ static void BlendSelSign( Gdiplus::Graphics *g, const Gdiplus::RectF &render_rect, int sel ) { CFileListManager &file_manager = GetInst( CFileListManager ); Gdiplus::Image *img = CreateImgFromBuffer( file_manager.GetSelSign( sel ) ); Gdiplus::PointF render_pos = GetRandPosInULRect( img->GetWidth(), img->GetHeight(), render_rect ); img_alpha( g, img, render_pos.X, render_pos.Y, 255 ); delete img; }
BOOL LoadImage(const HINSTANCE& inst, DWORD res, const TCHAR* name, IMGINFO* img_info, const HDC* mdc) { HRSRC src = FindResource(inst, MAKEINTRESOURCE(res), name); if (src == NULL) return FALSE; DWORD len = SizeofResource(inst, src); BYTE* byte = (BYTE*)LoadResource(inst, src); if (byte == NULL) return FALSE; HGLOBAL global = GlobalAlloc(GMEM_FIXED, len); BYTE* mem = (BYTE*)GlobalLock(global); CopyMemory(mem, byte, len); IStream* stream = NULL; Gdiplus::Image* image = NULL; HRESULT hr = CreateStreamOnHGlobal(mem, FALSE, &stream); if (SUCCEEDED(hr)) { image = Gdiplus::Image::FromStream(stream); img_info->hdc = CreateCompatibleDC(*mdc); HBITMAP hbitmap = CreateCompatibleBitmap(*mdc, image->GetWidth(), image->GetHeight()); SelectObject(img_info->hdc, hbitmap); Gdiplus::Graphics graphics(img_info->hdc); Gdiplus::Rect rect; rect.X = 0.0F; rect.Y = 0.0F; rect.Width = image->GetWidth(); rect.Height = image->GetHeight(); Gdiplus::Status s = graphics.DrawImage(image, rect); DeleteObject(hbitmap); img_info->image = image; } GlobalUnlock(mem); SAFE_RELEASE(stream); FreeResource(byte); if (SUCCEEDED(hr)) return TRUE; else return FALSE; }
// ôóíêöèÿ îòêðûòèÿ ôàéëà äëÿ ïðîñìîòðà ñ âõîäÿùèì ïàðàìåòðîì - èìåíåì îòêðûâàåìîãî ãðàôè÷åñêîãî ôàéëà void BrouseFile(AnsiString FN) { float ho=0; float wo; if (FN!=""){//åñëè èìÿ ôàéëà óêàçàíî òî âûïîëíèòü äåéñòâèÿ Form1->Image1->Align=alClient;//ðàçâîðà÷èâàåì îáëàñòü ïðîñìîòðà íà âñþ äîñòóïíþ ïëîùàäü Form1->Image1->Picture->Assign(0);//î÷èùàåì îáëàñòü ïðîñìîòðà WCHAR buff[10001]; Graphics::TBitmap *Buf = new Graphics::TBitmap();//ñîçäàåì îáúåêò êëàññà TBitmap Gdiplus::Image *image =new Gdiplus::Image (FN.WideChar(buff,10000)); //ñîçäàåì îáúåêò êëàññà Gdiplus::Image è çàãðóæàåì â íåãî èçîáðàæåíèå èç âûáðàííîãî ôàéëà Buf->Width=image->GetWidth();//óêàçûâàåì øèðèíó è âûñîòó èçîáðàæåíèÿ Buf->Height=image->GetHeight(); Gdiplus::Graphics graphics(Buf->Canvas->Handle);//ïåðåäàåì óêàçàòåëü íà îáúåêò Buf graphics.DrawImage(image, 0, 0, image->GetWidth(), image->GetHeight()); //ïðîðèñîâûâàåì âçÿòîå èç ôàéëà èçîáðàæåíèå â îáúåêòå Buf Form1->Image1->Picture->Assign(Buf);//âûâîäì ïðîðèñîâàííîå èçîáðàæåíèå â îáëàñòü ïðîñìîòðà Form1->ScrollBox1->Refresh();//îáíîâëÿåì ðàçìåðíîñòü îáëàñòè ïðîñìîòðà z_min=1;//èíèöèàëèçèðåì ïåðåìåííûå ìàñøàáèðîâàíèÿ z_max=1; Form1->StatusBar1->Panels->Items[1]->Text="Ôàéë - "+Form1->FileListBox1->Items->Strings[Form1->FileListBox1->ItemIndex];//âûâîäèì ñëóæåáíóþ èíôîðìàöèþ Form1->StatusBar1->Panels->Items[2]->Text="Ðàçìåð - "+IntToStr(image->GetWidth())+"x"+IntToStr(image->GetHeight()); // B->Assign(0);//î÷èùàåì ðåçåðâíûé îáúåêò êëàññà TBitmap B->Width=Form1->Image1->Picture->Bitmap->Width;//çàäàåì ðàçìåðû ðåçåðâíîãî îáúåêòà B->Height=Form1->Image1->Picture->Bitmap->Height; B->Canvas->Draw(0,0,Form1->Image1->Picture->Bitmap);//ïðîðèñîâûâàåì èçîáðàæåíèå â ðåçåðâíîì îáúåêòå èç îáëàñòè ïðîñìîòðà //åñëè êàðòèíêà èçíà÷àëüíî áîëüøå îáëàñòè ïðîñìîòðà //òî îíà áóäåò àâòîìàòè÷åñêè óìåíüøåíà //äàëåå îïðåäåëÿåì êîýô. óìåíüøåíèÿ if ((B->Height>Form1->Image1->Height)||(B->Width>Form1->Image1->Width)) //åñëè ïàðìåòðû êàðòèíêè ïðåâûøàþò ïàðàìåòðû îáëàñòè { ho=float(Form1->Image1->Height)/float(B->Height); //âû÷èñëÿåì ñîîòíîøåíèÿ âåëè÷èí wo=float(Form1->Image1->Width)/float(B->Width); if (ho>wo) //îïåðåäåëÿåì áîëüøóþ èç âåëè÷èí, ÷òîáû èçîáðàæåíèå ïîìåñòèëîñü â îáëàñòè ïðîñìîòðà {z_min=1+ho;} //óñòàíàâëèâàåì êîýô. else {z_min=1+wo;} } delete image;//î÷èùàåì ïàìÿòü delete Buf; } }
cgSize cgGdiplusRender::GetImageSize( cgID image ) { cgSize size; Gdiplus::Image* pkImage = m_kImageStorage.Find(image); if (pkImage) { size.w = pkImage->GetWidth(); size.h = pkImage->GetHeight(); } return size; }
/** generate some small images and render them on the backgroun. */ static void GenSmallImage( int img_count, Gdiplus::Graphics *g, const Gdiplus::RectF &render_rect, int sel ) { #define RAND_ALPHA ( 100 + random( 55 ) ) if( img_count < 1 ) { return; } CFileListManager &file_manager = GetInst( CFileListManager ); // render without alpha value Gdiplus::Image *img = CreateImgFromBuffer( file_manager.GetRandSmall() ); Gdiplus::PointF render_pos = GetRandPosInULRect( img->GetWidth(), img->GetHeight(), render_rect ); Gdiplus::RectF img_rect( render_pos.X, render_pos.Y, (float)img->GetWidth(), (float)img->GetHeight() ); img_alpha( g, img, render_pos.X, render_pos.Y, img_count == 1 ? 255 : RAND_ALPHA ); delete img; // render with random alpha value and random rotation degree g->SetClip( img_rect ); for( int i = 1; i < img_count; ++ i ) { img = CreateImgFromBuffer( file_manager.GetRandSmall() ); img_rotate( g, img, render_pos.X, render_pos.Y, img->GetWidth() / 2, img->GetHeight() / 2, random( 360 ), // [0-360) RAND_ALPHA ); // [100-155) delete img; } g->ResetClip(); // blend a 'X' sign onto the picture if it's not the answer if( img_count > 1 ) { //BlendXSign( g, img_rect ); } // blend 'A''B''C''D' sign. BlendSelSign( g, img_rect, sel ); }
void SetAspectRatio(Gdiplus::Image &image, RECT &rc) { double dWidth = (rc.right - rc.left); double dHeight = (rc.bottom - rc.top); double dAspectRatio = dWidth / dHeight; double dImageWidth = image.GetWidth(); double dImageHeight = image.GetHeight(); double dImageAspectRatio = dImageWidth / dImageHeight; if (dImageAspectRatio > dAspectRatio) { double nNewHeight = (dWidth / dImageWidth*dImageHeight); double nCenteringFactor = (dHeight - nNewHeight) / 2; SetRect(&rc, 0, (int)nCenteringFactor, (int)dWidth, (int)(nNewHeight + nCenteringFactor)); } else if (dImageAspectRatio < dAspectRatio) { double nNewWidth = (dHeight / dImageHeight*dImageWidth); double nCenteringFactor = (dWidth - nNewWidth) / 2; SetRect(&rc, (int)nCenteringFactor, 0, (int)(nNewWidth + nCenteringFactor), (int)(dHeight)); } }
BOOL COBSButton::Create(LPCTSTR lpszCaption, const RECT& rect, HWND hParentWnd, \ UINT nID, LPCTSTR lpszImage,int nSubPic) { if (m_hWnd) return FALSE; if (nSubPic>4||nSubPic<=0) { return FALSE; } m_hWnd = CreateWindow(TEXT("BUTTON"), lpszCaption, \ WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_OWNERDRAW, \ rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, \ hParentWnd, (HMENU)nID, NULL, NULL); if (NULL == m_hWnd) { goto CREATE_FAIL; } SendMessage(m_hWnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); m_OldProc = (WNDPROC)SetWindowLong(m_hWnd, GWLP_WNDPROC /*GWL_WNDPROC*/, (LONG)ButtonProc); SetWindowLong(m_hWnd, GWLP_USERDATA, (LONG)this); m_hFont = CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0, DEFAULT_CHARSET, \ OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, TEXT("Arial")); Gdiplus::Image* pImage = NULL; //¼ÓÔØͼƬ if (lpszImage != NULL) { pImage = Gdiplus::Image::FromFile(lpszImage); if ((pImage == NULL) || (pImage->GetLastStatus() != Gdiplus::Ok)) { if (pImage) { delete pImage; pImage = NULL; } return FALSE; } RECT windowRect; GetWindowRect(m_hWnd, &windowRect); SIZE sizeWindow; sizeWindow.cx = (pImage->GetWidth() / nSubPic)*4;//¼ÆËã4ÕÅͼƬÐèÒª¶àÉÙÏñËØ sizeWindow.cy = pImage->GetHeight(); HDC hDC = GetDC(m_hWnd); m_hdcMemory = CreateCompatibleDC(hDC); if (m_hdcMemory == NULL) return FALSE; m_hFourStateBitmap = CreateCompatibleBitmap(hDC, sizeWindow.cx, sizeWindow.cy); if (m_hFourStateBitmap == NULL) return FALSE; m_hOldBmp = (HBITMAP)::SelectObject(m_hdcMemory, m_hFourStateBitmap); if (m_hOldBmp == NULL) return FALSE; Gdiplus::Graphics graph(m_hdcMemory); graph.SetSmoothingMode(Gdiplus::SmoothingModeNone); graph.DrawImage(pImage, 0, 0, sizeWindow.cx, sizeWindow.cy); if (NULL == m_hFourStateBitmap) { goto CREATE_FAIL; } } if (pImage) { delete pImage; pImage = NULL; } return TRUE; CREATE_FAIL: if (m_hFourStateBitmap) { DeleteObject(m_hFourStateBitmap); } return FALSE; }
static Image *ReadEMFImage(const ImageInfo *image_info, ExceptionInfo *exception) { Gdiplus::Bitmap *bitmap; Gdiplus::BitmapData bitmap_data; Gdiplus::GdiplusStartupInput startup_input; Gdiplus::Graphics *graphics; Gdiplus::Image *source; Gdiplus::Rect rect; GeometryInfo geometry_info; Image *image; MagickStatusType flags; register Quantum *q; register ssize_t x; ssize_t y; ULONG_PTR token; unsigned char *p; wchar_t fileName[MagickPathExtent]; assert(image_info != (const ImageInfo *) NULL); assert(image_info->signature == MagickSignature); if (image_info->debug != MagickFalse) (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename); assert(exception != (ExceptionInfo *) NULL); image=AcquireImage(image_info,exception); if (Gdiplus::GdiplusStartup(&token,&startup_input,NULL) != Gdiplus::Status::Ok) ThrowReaderException(CoderError, "GdiplusStartupFailed"); MultiByteToWideChar(CP_UTF8,0,image->filename,-1,fileName,MagickPathExtent); source=Gdiplus::Image::FromFile(fileName); if (source == (Gdiplus::Image *) NULL) { Gdiplus::GdiplusShutdown(token); ThrowReaderException(FileOpenError,"UnableToOpenFile"); } image->resolution.x=source->GetHorizontalResolution(); image->resolution.y=source->GetVerticalResolution(); image->columns=(size_t) source->GetWidth(); image->rows=(size_t) source->GetHeight(); if (image_info->density != (char *) NULL) { flags=ParseGeometry(image_info->density,&geometry_info); image->resolution.x=geometry_info.rho; image->resolution.y=geometry_info.sigma; if ((flags & SigmaValue) == 0) image->resolution.y=image->resolution.x; if ((image->resolution.x > 0.0) && (image->resolution.y > 0.0)) { image->columns=(size_t) floor((Gdiplus::REAL) source->GetWidth() / source->GetHorizontalResolution() * image->resolution.x + 0.5); image->rows=(size_t)floor((Gdiplus::REAL) source->GetHeight() / source->GetVerticalResolution() * image->resolution.y + 0.5); } } bitmap=new Gdiplus::Bitmap((INT) image->columns,(INT) image->rows, PixelFormat32bppARGB); graphics=Gdiplus::Graphics::FromImage(bitmap); graphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic); graphics->SetSmoothingMode(Gdiplus::SmoothingModeHighQuality); graphics->SetTextRenderingHint(Gdiplus::TextRenderingHintClearTypeGridFit); graphics->Clear(Gdiplus::Color((BYTE) ScaleQuantumToChar( image->background_color.alpha),(BYTE) ScaleQuantumToChar( image->background_color.red),(BYTE) ScaleQuantumToChar( image->background_color.green),(BYTE) ScaleQuantumToChar( image->background_color.blue))); graphics->DrawImage(source,0,0,(INT) image->columns,(INT) image->rows); delete graphics; delete source; rect=Gdiplus::Rect(0,0,(INT) image->columns,(INT) image->rows); if (bitmap->LockBits(&rect,Gdiplus::ImageLockModeRead,PixelFormat32bppARGB, &bitmap_data) != Gdiplus::Ok) { delete bitmap; Gdiplus::GdiplusShutdown(token); ThrowReaderException(FileOpenError,"UnableToReadImageData"); } image->alpha_trait=BlendPixelTrait; for (y=0; y < (ssize_t) image->rows; y++) { p=(unsigned char *) bitmap_data.Scan0+(y*abs(bitmap_data.Stride)); if (bitmap_data.Stride < 0) q=GetAuthenticPixels(image,0,image->rows-y-1,image->columns,1,exception); else q=GetAuthenticPixels(image,0,y,image->columns,1,exception); if (q == (Quantum *) NULL) break; for (x=0; x < (ssize_t) image->columns; x++) { SetPixelBlue(image,ScaleCharToQuantum(*p++),q); SetPixelGreen(image,ScaleCharToQuantum(*p++),q); SetPixelRed(image,ScaleCharToQuantum(*p++),q); SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); q+=GetPixelChannels(image); } if (SyncAuthenticPixels(image,exception) == MagickFalse) break; } bitmap->UnlockBits(&bitmap_data); delete bitmap; Gdiplus::GdiplusShutdown(token); return(image); }
int DrawSdk::Image::WriteScaledImageFile(WCHAR* scaledImageName, float scaleX, float scaleY, const WCHAR* format) { int hr = S_OK; // // Starting the Gdiplus machine should be executed outside of this method // Gdiplus::GdiplusStartupInput gdiplusStartupInput; // ULONG_PTR gdiplusToken; // Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); Gdiplus::ImageCodecInfo *pImageCodecInfo = NULL; CLSID *pClsid = NULL; HDC hdc = CreateCompatibleDC(NULL); // Get the encoder CLSID depending on the image format (image/jpeg, image/gif, image/png) UINT num = 0; UINT size = 0; Gdiplus::GetImageEncodersSize(&num, &size); if (size == 0) hr = -1; // Failure if (SUCCEEDED(hr)) { pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size)); if (pImageCodecInfo == NULL) hr = -1; // Failure } if (SUCCEEDED(hr)) { Gdiplus::GetImageEncoders(num, size, pImageCodecInfo); for(int j = 0; j < num; ++j) { if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) { pClsid = &pImageCodecInfo[j].Clsid; break; // Success } } if (pClsid == NULL) hr = -1; // Failure } // Bugfix 5301: // If 'gdipImage_' is NULL then it was not kept in memory and therefore // a temporary Gdiplus::Image object is created for the rescaling. // This avoids having too much image data in the memory. Gdiplus::Image *tmpImage = NULL; if (SUCCEEDED(hr) && gdipImage_ == NULL) { // convert char-String in WCHAR-String int iStringLength = _tcslen(imageName_) + 1; WCHAR *wcFilename = (WCHAR *)malloc(iStringLength*sizeof(WCHAR)); #ifdef _UNICODE wcscpy(wcFilename, imageName_); #else MultiByteToWideChar( CP_ACP, 0, imageName, iStringLength, wcFilename, iStringLength); #endif tmpImage = new Gdiplus::Image(wcFilename, FALSE); if (wcFilename) free(wcFilename); } else { tmpImage = gdipImage_; } if (SUCCEEDED(hr) && tmpImage != NULL) { // Resize the image by scaling factors Gdiplus::REAL destWidth = scaleX * this->GetWidth(); Gdiplus::REAL destHeight = scaleY * this->GetHeight(); // If no correct size is defined in the object queue, use the real image size instead if ( (this->GetWidth() <= 0.0f) && (this->GetHeight() <= 0.0f) ) { destWidth = scaleX * tmpImage->GetWidth(); destHeight = scaleY * tmpImage->GetHeight(); } // At least we have to write one pixel if (destWidth < 1.0f) destWidth = 1.0f; if (destHeight < 1.0f) destHeight = 1.0f; Gdiplus::Bitmap scaledImage((int)(destWidth + 0.5f), (int)(destHeight + 0.5f)); scaledImage.SetResolution(tmpImage->GetHorizontalResolution(), tmpImage->GetVerticalResolution()); Gdiplus::Graphics grPhoto(&scaledImage); grPhoto.SetCompositingQuality(Gdiplus::CompositingQualityHighSpeed); grPhoto.SetCompositingMode(Gdiplus::CompositingModeSourceOver); grPhoto.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic); Gdiplus::Rect destRect(0, 0, (int)(destWidth + 0.5f), (int)(destHeight + 0.5f)); hr = grPhoto.DrawImage(tmpImage, destRect, 0, 0, tmpImage->GetWidth(),tmpImage->GetHeight(), Gdiplus::UnitPixel); // Write the scaled image to a file if (SUCCEEDED(hr)) hr = scaledImage.Save(scaledImageName, pClsid, NULL); } // Cleanup if (hdc) { ::DeleteDC(hdc); hdc = NULL; } if (pImageCodecInfo) { free(pImageCodecInfo); pImageCodecInfo = NULL; } if (tmpImage != NULL && tmpImage != gdipImage_) delete tmpImage; // // Stop the Gdiplus machine --> this should be executed outside of this method (see above) // Gdiplus::GdiplusShutdown(gdiplusToken); return hr; }
LRESULT BlobMgmt::ShowImage() { #ifndef VC6 HDC hdc; RECT rectwin; wyInt32 renderwidth, renderheight; PAINTSTRUCT ps; LPSTREAM stream = NULL; HGLOBAL glbmem; void *glbbuffer; wyWChar tempfilename[MAX_PATH+1] = {0}, path[MAX_PATH + 1] = {0}; wyString tempstr; HANDLE hfile = INVALID_HANDLE_VALUE; DWORD byteswritten = 0; if(!m_piub->m_data || m_piub->m_datasize == 0) { VERIFY(hdc = BeginPaint(m_hwndimage, &ps)); VERIFY(EndPaint(m_hwndimage, &ps)); return 0; } /* allocate global memory and copy image data in it*/ VERIFY(glbmem = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, m_piub->m_datasize)); if(!glbmem) return 0; /* lock the global memory and get a pointer */ glbbuffer = GlobalLock(glbmem); /* copy the memory to buffer */ CopyMemory(glbbuffer, m_piub->m_data, m_piub->m_datasize); /* unlock it */ VERIFY(GlobalUnlock(glbmem)== NO_ERROR); /* create the stream */ VERIFY(CreateStreamOnHGlobal(glbmem, FALSE, &stream)== S_OK); /* prepare window for painting */ VERIFY(hdc = BeginPaint(m_hwndimage, &ps)); /* clear the window */ PrepareScreen(ps.hdc); if(pGlobals->m_configdirpath.GetLength() || SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, (LPWSTR)path))) { if(pGlobals->m_configdirpath.GetLength()) { //wcscpy(path, pGlobals->m_configdirpath.GetAsWideChar()); wcsncpy(path, pGlobals->m_configdirpath.GetAsWideChar(), MAX_PATH); path[MAX_PATH] = '\0'; } else { wcscat(path, L"\\"); wcscat(path, L"SQLyog"); } VERIFY(GetTempFileName(path, L"img", 0, tempfilename)); hfile = CreateFile(tempfilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, NULL, NULL); VERIFY(hfile != INVALID_HANDLE_VALUE); VERIFY(WriteFile(hfile, m_piub->m_data, m_piub->m_datasize, &byteswritten, NULL)); VERIFY(CloseHandle(hfile)); } tempstr.SetAs(tempfilename); WCHAR *wpath = GetWideString(tempstr.GetString()); Gdiplus::Graphics graphics(hdc); Gdiplus::Image *image = new Gdiplus::Image(wpath); HeapFree(GetProcessHeap(), 0, wpath); /* in win95 image will be null so we exit */ if(!image) goto ExitPara; /* the binary data might not be image so image.getlastatus will not return Ok */ if(image->GetLastStatus()!= Gdiplus::Ok) { delete image; goto ExitPara; } /* get the window width and calculate the correct render stats */ VERIFY(GetClientRect(m_hwndimage, &rectwin)); renderheight =(((LONG)image->GetHeight())> rectwin.bottom)?(rectwin.bottom):(image->GetHeight()); renderwidth =(((LONG)image->GetWidth())> rectwin.right)?(rectwin.right):(image->GetWidth()); graphics.DrawImage(image, 0, 0, renderwidth, renderheight); delete image; EndPaint(m_hwndimage, &ps); ExitPara: /* free up stuff */ VERIFY(DeleteFile(tempfilename)); if(stream) stream->Release(); VERIFY(GlobalFree(glbmem)== NULL); #endif return 0; }
BOOL ReadImageProperty(const TCHAR *lpszImage, PROPID propId, TCHAR *szProperty, int cchMax) { Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR token; Gdiplus::Status status = GdiplusStartup(&token, &gdiplusStartupInput, NULL); if(status != Gdiplus::Ok) { return FALSE; } BOOL bSuccess = FALSE; /* This object needs to be deleted before GdiplusShutdown is called. If it were stack allocated, it would go out of scope _after_ the call to GdiplusShutdown. By allocating it on the heap, the lifetime can be directly controlled. */ Gdiplus::Image *image = new Gdiplus::Image(lpszImage, FALSE); if(image->GetLastStatus() == Gdiplus::Ok) { if(propId == PropertyTagImageWidth) { bSuccess = TRUE; StringCchPrintf(szProperty, cchMax, _T("%u pixels"), image->GetWidth()); } else if(propId == PropertyTagImageHeight) { bSuccess = TRUE; StringCchPrintf(szProperty, cchMax, _T("%u pixels"), image->GetHeight()); } else { UINT size = image->GetPropertyItemSize(propId); if(size != 0) { Gdiplus::PropertyItem *propertyItem = reinterpret_cast<Gdiplus::PropertyItem *>(malloc(size)); if(propertyItem != NULL) { status = image->GetPropertyItem(propId, size, propertyItem); if(status == Gdiplus::Ok) { if(propertyItem->type == PropertyTagTypeASCII) { int iRes = MultiByteToWideChar(CP_ACP, 0, reinterpret_cast<LPCSTR>(propertyItem->value), -1, szProperty, cchMax); if(iRes != 0) { bSuccess = TRUE; } } } free(propertyItem); } } } } delete image; Gdiplus::GdiplusShutdown(token); return bSuccess; }
void Graphics::DrawImage(Image* image, const Rect& rc, const ImageAttributes* attr) { Gdiplus::Graphics* g = reinterpret_cast<Gdiplus::Graphics*>(_private); Gdiplus::Image* gdiImage = reinterpret_cast<Gdiplus::Image*>(image->_private); if(attr!=0) { Gdiplus::ImageAttributes* ia = reinterpret_cast<Gdiplus::ImageAttributes*>(attr->_private); g->DrawImage(gdiImage, Gdiplus::Rect(rc.GetLeft(), rc.GetTop(), rc.GetWidth(), rc.GetHeight()), 0, 0, gdiImage->GetWidth(), gdiImage->GetHeight(), Gdiplus::UnitPixel, ia); } else { g->DrawImage(gdiImage, ToGDIRect<Rect, Gdiplus::Rect>(rc)); } }
int Image::GetHeight() { Gdiplus::Image* gdiImage = reinterpret_cast<Gdiplus::Image*>(_private); return gdiImage->GetHeight(); }
void CListViewCtrlEx::_DrawNormalItem( LPDRAWITEMSTRUCT lpdis, const TListItem *pItem ) { if (!pItem) return; int nItem = lpdis->itemID; CDCHandle dc; dc.Attach(lpdis->hDC); HFONT hOldFont = dc.SelectFont(m_fontDef); BOOL bSelect = FALSE ; if ((lpdis->itemAction | ODA_SELECT) && (lpdis->itemState & ODS_SELECTED)) { bSelect = TRUE ; } if ( bSelect ) dc.FillSolidRect( &lpdis->rcItem, RGB(185,219,255)); else dc.FillSolidRect( &lpdis->rcItem, pItem->clrBg); // draw check box if( pItem->dwFlags&(LISTITEM_CHECKBOX|LISTITEM_RADIOBOX) ) _DrawCheckBox(dc, lpdis->rcItem, _super::GetCheckState(nItem), pItem->dwFlags); COLORREF oldClr = dc.GetTextColor(); for(int i=0; i<pItem->subItems.size(); ++i) { CRect rcSubItem; DWORD nMarginWidth = 0; CRect rcBounds; GetSubItemRect(nItem, i, LVIR_LABEL, &rcSubItem); nMarginWidth = LEFT_MARGIN_TEXT_COLUMN+3; if(i==0) { if( pItem->dwFlags&(LISTITEM_CHECKBOX|LISTITEM_RADIOBOX) ) { nMarginWidth+=rcSubItem.left; } else { rcSubItem.left -= 19; nMarginWidth+=5; } } #define DT_FLAGS_DRAW_TEXT (DT_SINGLELINE|DT_LEFT|DT_NOPREFIX|DT_END_ELLIPSIS|DT_VCENTER) rcSubItem.left += LEFT_MARGIN_TEXT_COLUMN; rcSubItem.right -= 3; const TListSubItem &subItem = pItem->subItems[i]; if(subItem.type == SUBITEM_LINK) { dc.SelectFont(m_fontLink); dc.SetTextColor(COLOR_LIST_LINK); CRect rcProbeItem; dc.DrawText( subItem.str, -1, &rcProbeItem, DT_SINGLELINE|DT_LEFT|DT_NOPREFIX|DT_VCENTER|DT_CALCRECT); dc.DrawText( subItem.str, -1, &rcSubItem, DT_FLAGS_DRAW_TEXT); DWORD nMaxWidth = rcProbeItem.Width()+nMarginWidth; _SetColumnNeedWidth(i,nMaxWidth); } else { if (subItem.type == SUBITEM_ICON && subItem.nImg != NULL) { dc.DrawIconEx( rcSubItem.left, rcSubItem.top + 3, (HICON)subItem.nImg, 16 , 16, 0, 0, DI_NORMAL ); rcSubItem.left = rcSubItem.left + 18; } else if (subItem.type == SUBITEM_PNG) { Gdiplus::Image* pImg = BkPngPool::Get(subItem.nImg); if (pImg) { Gdiplus::Graphics graphics(dc); SIZE size = {0, 0}; if (pImg) { size.cx = pImg->GetWidth(); size.cy = pImg->GetHeight(); } graphics.DrawImage(pImg, Gdiplus::Rect(rcSubItem.left, rcSubItem.top + 5, size.cx, size.cy)); } rcSubItem.left = rcSubItem.left + 18; } else if(subItem.type==SUBITEM_COMBO) { CDC dcTmp; dcTmp.CreateCompatibleDC(dc); HBITMAP hBmpOld = dcTmp.SelectBitmap(m_bitmapCombo); dc.BitBlt(rcSubItem.right-20, rcSubItem.top + 3, 20, 20, dcTmp, 0, 0, SRCCOPY); dcTmp.SelectBitmap(hBmpOld); dcTmp.DeleteDC(); } UINT uFormat = DT_SINGLELINE|DT_LEFT|DT_NOPREFIX|DT_PATH_ELLIPSIS|DT_VCENTER; if (i == 3) { if (pItem->nLevel == enumLevelRisk) { rcSubItem.DeflateRect(2, 3); CPen penBorder; penBorder.CreatePen( PS_SOLID, 1, RGB(224, 0, 0) ); CBrush bshInterior; bshInterior.CreateSolidBrush( RGB(224, 0, 0) ); HPEN hOldPen = dc.SelectPen( penBorder ); HBRUSH hOldBrush = dc.SelectBrush( bshInterior ); dc.RoundRect( rcSubItem, CPoint( 3, 3 ) ); dc.SelectPen(hOldPen); dc.SelectBrush(hOldBrush); dc.SetTextColor( RGB(255, 255, 255) ); } else if (pItem->nLevel == enumLevelUnknown) { rcSubItem.DeflateRect(2, 3); CPen penBorder; penBorder.CreatePen( PS_SOLID, 1, RGB(250, 115, 5) ); CBrush bshInterior; bshInterior.CreateSolidBrush( RGB(250, 115, 5) ); HPEN hOldPen = dc.SelectPen( penBorder ); HBRUSH hOldBrush = dc.SelectBrush( bshInterior ); dc.RoundRect( rcSubItem, CPoint( 3, 3 ) ); dc.SelectPen(hOldPen); dc.SelectBrush(hOldBrush); dc.SetTextColor( RGB(255, 255, 255) ); } else dc.SetTextColor( subItem.clr ); uFormat = DT_SINGLELINE|DT_CENTER|DT_NOPREFIX|DT_PATH_ELLIPSIS|DT_VCENTER; } else dc.SetTextColor( subItem.clr ); dc.DrawText( subItem.str, -1, &rcSubItem, uFormat); if (subItem.type == SUBITEM_ICON || subItem.type == SUBITEM_PNG) rcSubItem.left = rcSubItem.left - 18; CRect rcProbeItem; dc.DrawText( subItem.str, -1, &rcProbeItem, DT_SINGLELINE|DT_LEFT|DT_NOPREFIX|DT_VCENTER|DT_CALCRECT); DWORD nMaxWidth = rcProbeItem.Width()+nMarginWidth; _SetColumnNeedWidth(i,nMaxWidth); } } CPen penx; penx.CreatePen(PS_SOLID,1,pItem->clrBtmGapLine); HPEN penOld = dc.SelectPen(penx); dc.MoveTo( lpdis->rcItem.left, lpdis->rcItem.bottom-1 ); CRect rcClient; GetClientRect(rcClient); dc.LineTo( lpdis->rcItem.left + rcClient.Width(), lpdis->rcItem.bottom-1); dc.SelectPen(penOld); dc.SelectFont(hOldFont); dc.SetTextColor(oldClr); dc.Detach(); }