VOID Example_DrawImage6(HDC hdc){ Graphics graphics(hdc); // Create an Image object. Image image(L"pattern.png"); // Draw the original source image. graphics.DrawImage(&image, 10, 10); // Define the portion of the image to draw. REAL srcX = 70.0f; REAL srcY = 20.0f; REAL srcWidth = 100.0f; REAL srcHeight = 100.0f; // Create a RectF object that specifies the destination of the image. RectF destRect(200.0f, 10.0f, <REAL>image.GetWidth(), <REAL>image.GetHeight()); // Create an ImageAttributes object that specifies a recoloring from red to blue. ImageAttributes remapAttributes; ColorMap redToBlue; redToBlue.oldColor = Color(255, 255, 0, 0); redToBlue.newColor = Color(255, 0, 0, 255); remapAttributes.SetRemapTable(1, &redToBlue); // Draw the resized image. graphics.DrawImage( &image, destRect, srcX, srcY, srcWidth, srcHeight, UnitPixel, &remapAttributes, NULL, NULL);}
SDK上的例子
并不确定是在dlg上
我现在的代码是这样的:
都是放到 dll 工程里面的.我现在给外部提供的是一个接口,通过这个接口把图片显示到控件上去.加载BMP文件到内存
通过得到的BMP图像文件路径目录,加载BMP图像文件到内存中。
BOOL CAutoLoadImage::LoadImageFile(const char *pPathname)
{
CFile file;
if( !file.Open( pPathname, CFile::modeRead) )
{
return FALSE;
}
DWORD m_nFileLen;
m_nFileLen = file.GetLength();
m_pBMPBuffer = new char[m_nFileLen + 1];
if(!m_pBMPBuffer)
{
return FALSE;
}
if(file.ReadHuge(m_pBMPBuffer,m_nFileLen) != m_nFileLen)
{
return FALSE;
}
return TRUE;
}将内存中的BMP文件内容转换成位图句柄。
HBITMAP CAutoLoadImage::BufferToHBITMAP()
{
HBITMAP hShowBMP;
LPSTR hDIB,lpBuffer = m_pBMPBuffer;
LPVOID lpDIBBits;
BITMAPFILEHEADER bmfHeader;
DWORD bmfHeaderLen;
bmfHeaderLen = sizeof(bmfHeader);
strncpy((LPSTR)&bmfHeader,(LPSTR)lpBuffer,bmfHeaderLen); if (bmfHeader.bfType != (*(WORD*)"BM")) return NULL;
hDIB = lpBuffer + bmfHeaderLen;
BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; lpDIBBits=(lpBuffer)+((BITMAPFILEHEADER *)lpBuffer)->bfOffBits;
CClientDC dc(this);
hShowBMP = CreateDIBitmap(dc.m_hDC,&bmiHeader,CBM_INIT,lpDIBBits,&bmInfo,DIB_RGB_COLORS);
return hShowBMP;
}在屏幕上显示BMP图像
void CAutoLoadImage::DrawImage(int x, int y, CDC *pDC)
{
HBITMAP OldBitmap;
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
OldBitmap=(HBITMAP)MemDC.SelectObject(m_hBitmap); CRect rect;
GetClientRect(&rect);
pDC->BitBlt(x,y,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(OldBitmap);
}再怎么也无法把图片显示出来,不知道为什么你可以给我举个例子吗?
checkabc#gmail.com#->@