以下是网学网为您推荐的ASP.net-基于VC.NET的GDI+编程之CImage,希望本篇文章对您学习有所帮助。
BOOL Draw( HDC hDestDC, int xDest, int yDest, int nDestWidth, int nDestHeight, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight ); BOOL Draw( HDC hDestDC, const RECT& rectDest, const RECT& rectSrc ); BOOL Draw( HDC hDestDC, int xDest, int yDest ); BOOL Draw( HDC hDestDC, const POINT& pointDest ); BOOL Draw( HDC hDestDC, int xDest, int yDest, int nDestWidth, int nDestHeight ); BOOL Draw( HDC hDestDC, const RECT& rectDest ); |
void CEx_ImageView::OnFileOpen() { CString strFilter; CSimpleArray<GUID> aguidFileTypes; HRESULT hResult; // 获取CImage支持的图像文件的过滤字符串 hResult = m_Image.GetExporterFilterString(strFilter,aguidFileTypes, _T( "All Image Files") ); if (FAILED(hResult)) { MessageBox("GetExporterFilter调用失败!"); return; } CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter); if(IDOK != dlg.DoModal()) return; m_Image.Destroy(); // 将外部图像文件装载到CImage对象中 hResult = m_Image.Load(dlg.GetFileName()); if (FAILED(hResult)) { MessageBox("调用图像文件失败!"); return; } // 设置主窗口标题栏内容 CString str; str.LoadString(AFX_IDS_APP_TITLE); AfxGetMainWnd()->SetWindowText(str + " - " +dlg.GetFileName()); Invalidate(); // 强制调用OnDraw } |
void CEx_ImageView::OnDraw(CDC* pDC) { CEx_ImageDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!m_Image.IsNull()) { m_Image.Draw(pDC->m_hDC,0,0); } } |
public: CImage m_Image; |
HRESULT Save( LPCTSTR pszFileName, REFGUID guidFileType= GUID_NULL); |
void CEx_ImageView::OnFileSaveAs() { if (m_Image.IsNull()) { MessageBox("你还没有打开一个要保存的图像文件!"); return; } CString strFilter; strFilter = "位图文件|*.bmp|JPEG 图像文件|*.jpg| \ GIF 图像文件|*.gif|PNG 图像文件|*.png||"; CFileDialog dlg(FALSE,NULL,NULL,NULL,strFilter); if ( IDOK != dlg.DoModal()) return; // 如果用户没有指定文件扩展名,则为其添加一个 CString strFileName; CString strExtension; strFileName = dlg.m_ofn.lpstrFile; if (dlg.m_ofn.nFileExtension == 0) { switch (dlg.m_ofn.nFilterIndex) { case 1: strExtension = "bmp"; break; case 2: strExtension = "jpg"; break; case 3: strExtension = "gif"; break; case 4: strExtension = "png"; break; default: break; } strFileName = strFileName + ''.'' + strExtension; } // 图像保存 HRESULT hResult = m_Image.Save(strFileName); if (FAILED(hResult)) MessageBox("保存图像文件失败!"); } |
void FilterImage(CImage* image, int nType) { if (image->IsNull()) return; int smoothGauss[9] = {1,2,1,2,4,2,1,2,1}; // 高斯模板 int sharpLaplacian[9] = {-1,-1,-1,-1,9,-1,-1,-1,-1}; // 拉普拉斯模板 int opTemp[9]; float aver; // 系数 if ( nType > 1) nType = 0; switch( nType ){ case 0: // 高斯模板 aver = (float)(1.0/16.0); memcpy( opTemp, smoothGauss, 9*sizeof(int)); break; case 1: // 拉普拉斯模板 aver = 1.0; memcpy( opTemp, sharpLaplacian, 9*sizeof(int)); break; } int i,j; int nWidth = image->GetWidth(); int nHeight = image->GetHeight(); for (i = 1; i < nWidth-1; i++){ for (j = 1; j < nHeight-1; j++){ int rr = 0, gg = 0, bb = 0; int index = 0; for (int col = -1; col <= 1; col++){ for (int row = -1; row <= 1; row++){ COLORREF clr = image->GetPixel( i+row, j+col); rr += GetRValue(clr) * opTemp[index]; gg += GetGValue(clr) * opTemp[index]; bb += GetBValue(clr) * opTemp[index]; index++; } } rr = (int)(rr*aver); gg = (int)(gg*aver); bb = (int)(bb*aver); // 处理溢出点 if ( rr > 255 ) rr = 255; else if ( rr < 0 ) rr = -rr; if ( gg > 255 ) gg = 255; else if ( gg < 0 ) gg = -gg; if ( bb > 255 ) bb = 255; else if ( bb < 0 ) bb = -bb; // 错位重写以避免前一个像素被新的像素覆盖 image->SetPixel( i-1, j-1, RGB(rr,gg,bb)); } } } |
void CEx_ImageView::MakeBlackAndwhite(CImage* image) { if (image->IsNull()) return; if (!image->IsIndexed()) { // 直接修改像素颜色 COLORREF pixel; int maxY = image->GetHeight(), maxX = image->GetWidth(); byte r,g,b,avg; for (int x=0; x<maxX; x++) { for (int y=0; y<maxY; y++) { pixel = image->GetPixel(x,y); r = GetRValue(pixel); g = GetGValue(pixel); b = GetBValue(pixel); avg = (int)((r + g + b)/3); image->SetPixelRGB(x,y,avg,avg,avg); } } } else { // 获取并修改颜色表 int MaxColors = image->GetMaxColorTableEntries(); RGBQUAD* ColorTable; ColorTable = new RGBQUAD[MaxColors]; image->GetColorTable(0,MaxColors,ColorTable); for (int i=0; i<MaxColors; i++) { int avg = (ColorTable[i].rgbBlue + ColorTable[i].rgbGreen + ColorTable[i].rgbRed)/3; ColorTable[i].rgbBlue = avg; ColorTable[i].rgbGreen = avg; ColorTable[i].rgbRed = avg; } image->SetColorTable(0,MaxColors,ColorTable); delete(ColorTable); } } |