CScrollView中,用HBitmap、HDC在内存中画图,可保存成BMP图片时,只有view的可视部分,其他部分一片白色,如何解决?

abcd_efg 2003-08-19 02:10:44
保存成BMP图片的三个函数如下,我直接在WriteWindowToDIB(),把HBitmap、HDC转化为要CBitmap、CDC。

HANDLE CMyView::DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;


ASSERT( bitmap.GetSafeHandle() );

// The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL;

// If a palette has not been supplied use defaul palette
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

// Get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm);

// Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;

// Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);

// We need a device context to get the DIB from
hDC = ::GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);

// Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}

lpbi = (LPBITMAPINFOHEADER)hDIB;

*lpbi = bi;

// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

bi = *lpbi;

// If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;

// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}

// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else{
GlobalFree(hDIB);

// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}

// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB;

// FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table

if( !bGotBits )
{
GlobalFree(hDIB);

SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return NULL;
}

SelectPalette(hDC,hPal,FALSE);
::ReleaseDC(NULL,hDC);
return hDIB;

}

BOOL CMyView::WriteDIB(LPTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;

if (!hDIB)
return FALSE;

CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE;

lpbi = (LPBITMAPINFOHEADER)hDIB;

int nColors = 1 << lpbi->biBitCount;

// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD));

// Write the file header
file.Write( &hdr, sizeof(hdr) );

// Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) );

return TRUE;

}

BOOL CMyView::WriteWindowToDIB(CWnd *pWnd)
{
CBitmap *bitmap;
// CWindowDC dc(pWnd);
CDC *memDC;
// CRect rect;

// memDC.CreateCompatibleDC(&dc);

// pWnd->GetWindowRect(rect);

// bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
memDC=CDC::FromHandle(m_hMemDC);
bitmap=CBitmap::FromHandle(m_hBitmap);

CBitmap* pOldBitmap = memDC->SelectObject(bitmap);
// memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);

// Create logical palette if device support a palette
CPalette pal;
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;

pLP->palNumEntries =
GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );

// Create the palette
pal.CreatePalette( pLP );

delete[] pLP;
}

memDC->SelectObject(pOldBitmap);

// Convert the bitmap to a DIB
HANDLE hDIB = DDBToDIB( *bitmap, BI_RGB, &pal );

if( hDIB == NULL )
return FALSE;

// Write it to file
WriteDIB( "c:\\test.bmp", hDIB );

// Free the memory allocated by DDBToDIB for the DIB
GlobalFree( hDIB );
return TRUE;
}
...全文
228 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcd_efg 2003-08-21
  • 打赏
  • 举报
回复
TO mct1025(macuntao) :你真是太谦虚了,代码很清晰,注释也很详细,谢谢你。
mct1025 2003-08-20
  • 打赏
  • 举报
回复
你的信箱damadama◎citiz.net
地址错误
abcd_efg 2003-08-20
  • 打赏
  • 举报
回复
TO mct1025(macuntao) :那太感谢了,我的信箱damadama◎citiz.net
mct1025 2003-08-20
  • 打赏
  • 举报
回复
已发送
abcd_efg 2003-08-20
  • 打赏
  • 举报
回复
怎么会呢,上海热线的信箱呀。那我只有用一个名称不怎么雅的信箱了,当时只是为了好玩,看看新浪有没有过滤这些不雅的词?
i_fuck_you@sina.com。

我今天总在看damadama◎citiz.net,没有来csdn,所以没看到你最新的留言,抱歉啊
mct1025 2003-08-19
  • 打赏
  • 举报
回复
我使用CScrollView,m_cx,m_cy 纪录滚动位置,m_size 纪录缩放比例

留一个邮箱,我发给你
abcd_efg 2003-08-19
  • 打赏
  • 举报
回复
TO: mct1025(macuntao) ,谢谢!能说得完整点吗,能不能把你的code贴出来我看看。

在OnPaint事件中
使用dc.StretchBlt(0,0,w,h,&m_memDC,int(m_cx/m_size),
int(m_cy/m_size),int(w/m_size),int(h/m_size),SRCCOPY);
其中m_size是?
mct1025 2003-08-19
  • 打赏
  • 举报
回复
我认为,你使用m_hMemDC=dc.Detach();
m_hBitmap=(HBITMAP)bitmap.Detach();
得到视的dc不对

我是这样做的
建一个m_memDC,m_pbmp用来存储图像数据
CDC m_memDC;
CBitmap *m_pbmp;

CDC *pDC = GetDC();
m_memDC.CreateCompatibleDC(pDC);
m_pbmp = new CBitmap;
m_pbmp->CreateCompatibleBitmap(pDC,m_width,m_height);
m_memDC.SelectObject(m_pbmp);
//加入你的写数据函数

在OnPaint事件中
使用dc.StretchBlt(0,0,w,h,&m_memDC,int(m_cx/m_size),
int(m_cy/m_size),int(w/m_size),int(h/m_size),SRCCOPY);
把m_memDC中的数据,画到视图dc

保存时调用m_memDC,m_pbmp
abcd_efg 2003-08-19
  • 打赏
  • 举报
回复
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
CDC dc;
dc.CreateCompatibleDC(NULL);
CBitmap bitmap;
// CDC *pDC=GetClientDC();
CClientDC pDC(this);
bitmap.CreateCompatibleBitmap(&pDC,3600,2800);
ReleaseDC(&pDC);

CBitmap *pOldBitmap=(CBitmap *) dc.SelectObject(&bitmap);
dc.PatBlt(0,0,3600,2800,WHITENESS);
dc.SelectObject(pOldBitmap);
m_hMemDC=dc.Detach();
m_hBitmap=(HBITMAP)bitmap.Detach();

m_Tip.Create(this);
return 0;
}


void CMyView::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
OnPrepareDC(&dc);
CRect rect;
GetClientRect(&rect);
CRect invalidRect;
dc.GetClipBox(&invalidRect);
CBitmap *pBmp=CBitmap::FromHandle(m_hBitmap);
CDC *pdc=CDC::FromHandle(m_hMemDC);
CBitmap* pOldBitmap=(CBitmap*)pdc->SelectObject(pBmp);
pdc->PatBlt(invalidRect.left,invalidRect.top,invalidRect.Width(),invalidRect.Height(),WHITENESS);
CRgn rgn;
rgn.CreateRectRgnIndirect(&invalidRect);
pdc->SelectClipRgn(&rgn);

OnDraw(pdc);

dc.BitBlt(invalidRect.left,invalidRect.top,invalidRect.Width(),invalidRect.Height(),pdc,invalidRect.left,invalidRect.top,SRCCOPY);pdc->SelectObject(pOldBitmap);

// Do not call CScrollView::OnPaint() for painting messages
}


void CMyView::OnDraw(CDC* pDC)
{

}
benz600 2003-08-19
  • 打赏
  • 举报
回复
直接从内存写,应该没有问题,我做过
mct1025 2003-08-19
  • 打赏
  • 举报
回复
能把设置m_hMemDC,m_hBitmap的代码贴出来吗?
abcd_efg 2003-08-19
  • 打赏
  • 举报
回复
void CMyView::SaveAsBmp(CString sPath)
{
WriteWindowToDIB(this);
CFileStatus rStatus;
if(CFile::GetStatus(sPath,rStatus)&&sPath!="c:\\test.bmp")
CFile::Remove(sPath);
CFile::Rename( "c:\\bdam1mp.bmp", sPath );

}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧