怎么保存图片??

yangxingkuan 2006-04-25 06:38:50
如果我创建了一个兼容位图,自己处理后,怎么把位图保存为图片,向QQ截图一样、
...全文
393 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
femalelover 2006-04-26
  • 打赏
  • 举报
回复
上面那么多API还不够吗?
yangxingkuan 2006-04-25
  • 打赏
  • 举报
回复
我要用API,在api下的程序
soaroc 2006-04-25
  • 打赏
  • 举报
回复
出自《精通VisualC++图像处理编程》
BOOL CDib::Write(CFile *pFile)
{
WaitCursorBegin();

BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
LPBITMAPINFOHEADER lpBI; // Pointer to DIB info structure
DWORD dwDIBSize;

// Get a pointer to the DIB memory, the first of which contains
// a BITMAPINFO structure
lpBI = (LPBITMAPINFOHEADER)GlobalLock(m_hDib);
if (!lpBI)
{
GlobalUnlock(m_hDib);
WaitCursorEnd();
return FALSE;
}

// Check to see if we're dealing with an OS/2 DIB. If so, don't
// save it because our functions aren't written to deal with these
// DIBs.
if (lpBI->biSize != sizeof(BITMAPINFOHEADER))
{
GlobalUnlock(m_hDib);
WaitCursorEnd();
return FALSE;
}

// Fill in the fields of the file header

// Fill in file type (first 2 bytes must be "BM" for a bitmap)

bmfHdr.bfType = DIB_HEADER_MARKER; // "BM"

// Partial Calculation

dwDIBSize = *(LPDWORD)lpBI + PaletteSize((LPBYTE)lpBI);

// Now calculate the size of the image

// It's an RLE bitmap, we can't calculate size, so trust the biSizeImage
// field

if ((lpBI->biCompression == BI_RLE8) || (lpBI->biCompression == BI_RLE4))
dwDIBSize += lpBI->biSizeImage;
else
{
DWORD dwBmBitsSize; // Size of Bitmap Bits only

// It's not RLE, so size is Width (DWORD aligned) * Height

dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount)) *
lpBI->biHeight;

dwDIBSize += dwBmBitsSize;

// Now, since we have calculated the correct size, why don't we
// fill in the biSizeImage field (this will fix any .BMP files which
// have this field incorrect).

lpBI->biSizeImage = dwBmBitsSize;
}


// Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)

bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;

// Now, calculate the offset the actual bitmap bits will be in
// the file -- It's the Bitmap file header plus the DIB header,
// plus the size of the color table.

bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize +
PaletteSize((LPBYTE)lpBI);

TRY
{
// Write the file header
pFile->Write(&bmfHdr, sizeof(BITMAPFILEHEADER));
// write DIB buffer
pFile->Write(lpBI, dwDIBSize);
}
CATCH (CException, e)
{
GlobalUnlock(m_hDib);
WaitCursorEnd();
return FALSE;
}
END_CATCH

GlobalUnlock(m_hDib);
WaitCursorEnd();

return TRUE;

// return WriteDIBFile(m_hDib, (HANDLE)pFile->m_hFile);
}
然后保存:
BOOL CDib::Save(LPCTSTR lpszDibFile)
{
TRY
{
CFile file(lpszDibFile, CFile::modeCreate|CFile::modeWrite);

if (! Write(&file))
return FALSE;
}
CATCH (CException, e)
{
return FALSE;
}
END_CATCH

return TRUE;
}

其中PaletteSize为自定义的获取调色板的大小,很容易编写,你试一试

16,550

社区成员

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

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

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