用户登录  |  用户注册
首 页源码下载网络学院最新源码源码排行屏蔽广告
当前位置:新兴网络 > 网络学院 > 编程开发 > C/C++

VC++ HDC 保存到本地BMP图片文件

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2011-03-13 16:04:49

VC++ HDC 保存到本地BMP图片文件:

C/C++ code复制代码
#include "stdafx.h"
#include <string>
#include <vector>
#ifdef UNICODE
    #ifndef TSTRING
        #define TSTRING std::wstring
    #endif
#else
    #ifndef TSTRING
        #define TSTRING std::string
    #endif
#endif

BOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg);
BOOL WriteBmp(const TSTRING &strFile,HDC hdc);
BOOL WriteBmp(const TSTRING &strFile,HDC hdc,const RECT &rcDC);

BOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg)
{   

    BITMAPINFOHEADER bmInfoHeader = {0};
    bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmInfoHeader.biWidth = sizeImg.cx;
    bmInfoHeader.biHeight = sizeImg.cy;
    bmInfoHeader.biPlanes = 1;
    bmInfoHeader.biBitCount = 24;

    //Bimap file header in order to write bmp file
    BITMAPFILEHEADER bmFileHeader = {0};
    bmFileHeader.bfType = 0x4d42;  //bmp 
    bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); ///3=(24 / 8)

    HANDLE hFile = CreateFile(strFile.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }

    DWORD dwWrite = 0;
    WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);
    WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);
    WriteFile(hFile,&vtData[0], vtData.size(),&dwWrite,NULL);

    CloseHandle(hFile);

    return TRUE;
}

BOOL WriteBmp(const TSTRING &strFile,HDC hdc)
{
    int iWidth = GetDeviceCaps(hdc,HORZRES);
    int iHeight = GetDeviceCaps(hdc,VERTRES);
    RECT rcDC = {0,0,iWidth,iHeight};

    return WriteBmp(strFile,hdc,rcDC);   
}

BOOL WriteBmp(const TSTRING &strFile,HDC hdc,const RECT &rcDC)
{
    BOOL bRes = FALSE;
    BITMAPINFO bmpInfo = {0};
    BYTE *pData = NULL;
    SIZE sizeImg = {0};
    HBITMAP hBmp = NULL;
    std::vector<BYTE> vtData;
    HGDIOBJ hOldObj = NULL;
    HDC hdcMem = NULL;

    //Initilaize the bitmap information   
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = rcDC.right - rcDC.left;
    bmpInfo.bmiHeader.biHeight = rcDC.bottom - rcDC.top;
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = 24;

    //Create the compatible DC to get the data
    hdcMem = CreateCompatibleDC(hdc);
    if(hdcMem == NULL)
    {
        goto EXIT;
    }

    //Get the data from the memory DC   
    hBmp = CreateDIBSection(hdcMem,&bmpInfo,DIB_RGB_COLORS,reinterpret_cast<VOID **>(&pData),NULL,0);
    if(hBmp == NULL)
    {
        goto EXIT;
    }
    hOldObj = SelectObject(hdcMem, hBmp);
   
    //Draw to the memory DC
    sizeImg.cx = bmpInfo.bmiHeader.biWidth;
    sizeImg.cy = bmpInfo.bmiHeader.biHeight;
    StretchBlt(hdcMem,
                0,
                0,
                sizeImg.cx,
                sizeImg.cy,
                hdc,
                rcDC.left,
                rcDC.top,
                rcDC.right - rcDC.left + 1,
                rcDC.bottom - rcDC.top + 1,
                SRCCOPY);
  

    vtData.resize(sizeImg.cx * sizeImg.cy * 3);
    memcpy(&vtData[0],pData,vtData.size());
    bRes = WriteBmp(strFile,vtData,sizeImg);

    SelectObject(hdcMem, hOldObj);
  

EXIT:
    if(hBmp != NULL)
    {
        DeleteObject(hBmp);
    }

    if(hdcMem != NULL)
    {
        DeleteDC(hdcMem);
    }

    return bRes;
}

Tags:VC HDC BMP 保存 文件保存

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      100%(2)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

网络学院评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 1 条,得分 55 分,平均 55 分) 查看完整评论
[回复] 1游客   打分:55 分  发表时间:2013-02-18
· word文档经过该程序转出的bmp图片达到100MB,且打不开,是什么原因?