网上搜索了从HDC写bmp文件的方法,但是得不到数据!!
BOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg)
{
BITMAPINFOHEADER bmInfoHeader = {0};
bmInfoHeaderbiSize = sizeof(BITMAPINFOHEADER);
bmInfoHeaderbiWidth = sizeImgcx;
bmInfoHeaderbiHeight = sizeImgcy;
bmInfoHeaderbiPlanes = 1;
bmInfoHeaderbiBitCount = 24;
//Bimap file header in order to write bmp file
BITMAPFILEHEADER bmFileHeader = {0};
bmFileHeaderbfType = 0x4d42; //bmp
bmFileHeaderbfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmFileHeaderbfSize = bmFileHeaderbfOffBits + ((bmInfoHeaderbiWidth bmInfoHeaderbiHeight) 3); ///3=(24 / 8)
HANDLE hFile = CreateFile(strFilec_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], vtDatasize(),&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
bmpInfobmiHeaderbiSize = sizeof(BITMAPINFOHEADER);
bmpInfobmiHeaderbiWidth = rcDCright - rcDCleft;
bmpInfobmiHeaderbiHeight = rcDCbottom - rcDCtop;
bmpInfobmiHeaderbiPlanes = 1;
bmpInfobmiHeaderbiBitCount = 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;
}
bool saveBmp3(char bmpName)
{
//以二进制写的方式打开文件
FILE fp=fopen(bmpName,"wb");
if(fp==0) return 0;
int w=1;
int h=1;
int bibitcount=4;
int linebyte=4;
int colortablesize=64;
BITMAPFILEHEADER filehead;
fileheadbfType=0x4d42;
fileheadbfReserved1=0;
fileheadbfReserved2=0;
fileheadbfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+colortablesize+linebyteh;
//bfOffBits是图像文件前三个部分所需空间之和
fileheadbfOffBits=14+40+colortablesize;
//写文件头进文件
fwrite(&filehead, sizeof(BITMAPFILEHEADER),1, fp);
BITMAPINFOHEADER head;
headbiBitCount=bibitcount;
headbiClrImportant=0;
headbiClrUsed=0;
headbiCompression=0;
headbiHeight=h;
headbiPlanes=1;
headbiSize=40;
headbiSizeImage=linebyteh;
headbiWidth=w;
headbiXPelsPerMeter=0;
headbiYPelsPerMeter=0;
//写位图信息头进内存
fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp);
RGBQUAD pcolortable;
pcolortable=new RGBQUAD[16];
int t=15;
for(int i=0;i<16;i++)
{
pcolortable[i]rgbReserved=0;
pcolortable[i]rgbBlue=it;
pcolortable[i]rgbGreen=0;
pcolortable[i]rgbRed=it;
}
fwrite(pcolortable, sizeof(RGBQUAD),16, fp);
unsigned char pbmpbuf;
pbmpbuf=new unsigned char[linebyte h];
pbmpbuf[0]=35;
pbmpbuf[1]=35;
// pbmpbuf[4]=69;
// pbmpbuf[5]=103;
// pbmpbuf[8]=137;
// pbmpbuf[9]=171;
// pbmpbuf[12]=205;
// pbmpbuf[13]=239;
//for(i=0;i<16;i++)
// pbmpbuf[i]=199;
//写位图数据进文件
fwrite(pbmpbuf, hlinebyte, 1, fp);
//关闭文件
fclose(fp);
return 1;
}
我要去上课了,不看你的程序了,上面的是我写的一个写BMP文件的程序,你看看,这个是没问题的。
//写文件头
foutwrite((char)&bitmapInfoHead,sizeof(BITMAPFILEHEADER));
你这句错了,是写文件头信息BITMAPINFOHEADER
步骤一:导入bmp文件:
第二步:设置为背景:
① 添加OnCtlColor消息响应函数:
② 添加响应代码:
HBRUSH CMyDlg::OnCtlColor(CDC pDC, CWnd pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
static CBrush gBr;
static bool isInited = false;
if(!isInited)
{
CBitmap bitmap;
bitmapLoadBitmap(IDB_BITMAP1);
gBrCreatePatternBrush(&bitmap);
COLORREF clearColor = -1;
bitmapDeleteObject();
isInited = true;
}
if(pWnd==this)
{
pDC->SetBkMode(TRANSPARENT);
return gBr; //主窗口背景使用这个背景刷
}
else
{
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)::GetStockObject(NULL_BRUSH); //其他控件使用透明背景
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
③效果图:
静态文本框、单行多行输入文本框、列表框、ListCtrl等都可以共享主窗口的背景,但是按钮需要另外设置才可以。
以上就是关于内存画图,该怎么解决全部的内容,包括:内存画图,该怎么解决、编程问题,程序如下,一个生成bmp渐变图像的简单程序,但是生成bmp无法打开,win7和xp都无法打开,在线等、MFC中如何载入bmp和jpeg格式的图片,并作为文件背景等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)