RTF文件结构分析及其应用
void WmfToBmp(int width,int height,char *metafile)
{
//width为图片宽度,height为图片高度,*metafile为元文件名
static HMETAFILE hmf;
char num[4];
FILE *fh;
HDC hMemDC;
HBITMAP hbmp,hold;
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbmpih;
HGLOBAL hg;
BitmapCount++;
if(BitmapCount==3)
{
width=128;
height=132;
}
if(BitmapCount==1)
{
width=80;
height=50;
}
ToString10(BitmapCount,num);
//把字符串转换成数字的函数
lstrcpy(bmpFile,FilePath);
lstrcat(bmpFile,num);
lstrcpy(gifFile,bmpFile);
lstrcat(bmpFile,".bmp");
lstrcat(gifFile,".gif");
hmf=GetMetaFile(metafile);
hDisplayDC=CreateDC("DISPLAY",NULL,NULL,NULL);
hMemDC=CreateCompatibleDC(hDisplayDC);
hg=GlobalAlloc(GHND,sizeof(BITMAPINFOHEADER));
lpbmpih=(LPBITMAPINFOHEADER)GlobalLock(hg);
lpbmpih->biSize=sizeof(BITMAPINFOHEADER);
lpbmpih->biWidth=width;
lpbmpih->biHeight=height;
lpbmpih->biPlanes=1;
lpbmpih->biBitCount=8;
lpbmpih->biCompression=0;
hbmp=CreateCompatibleBitmap(hDisplayDC,width,height);
GlobalUnlock(hg);
GlobalFree(hg);
hold=SelectObject(hMemDC,hbmp);
SetMapMode(hMemDC,MM_ANISOTROPIC);
SetWindowOrgEx(hMemDC,0,0,NULL);
SetViewportExtEx(hMemDC,width,height,NULL);
PlayMetaFile(hMemDC,hmf);
hbmp=SelectObject(hMemDC,hold);
if((fh=fopen(bmpFile,"w+b"))==NULL)
{
return;
}
hdr.bfType=0x4d42;
hdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+width*height;
hdr.bfReserved1=0;
hdr.bfReserved2=0;
hdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD);
fwrite((LPSTR)&hdr,sizeof(BITMAPFILEHEADER),1,fh);
hg=GlobalAlloc(GHND,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD));
lpbmpih=(LPBITMAPINFOHEADER)GlobalLock(hg);
lpbmpih->biSize=sizeof(BITMAPINFOHEADER);
lpbmpih->biWidth=width;
lpbmpih->biHeight=height;
lpbmpih->biPlanes=1;
lpbmpih->biBitCount=8;
lpbmpih->biCompression=0;
lpbmpih->biSizeImage=width*height;
lpbmpih->biClrUsed=256;
lpbmpih->biClrImportant=0;
j=GetDIBits(hMemDC,hbmp,0,height,NULL,(BITMAPINFO*)lpbmpih,DIB_RGB_COLORS);
lpbmpih->biSize=sizeof(BITMAPINFOHEADER);
fwrite(lpbmpih,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD),1,fh);
GetDIBits(hMemDC,hbmp,0,height,(LPSTR)FileBuffer,(BITMAPINFO*)lpbmpih,DIB_RGB_COLS);
fwrite(FileBuffer,width*height,1,fh);
GlobalUnlock(hg);
GlobalFree(hg);
fclose(fh);
DeleteDC(hMemDC);
DeleteDC(hDisplayDC);
DeleteObject(hbmp);
DeleteObject(hold);
DeleteMetaFile(hmf);
}//end function WmfToBmp()
用这种方法还有一个好处,那就是当你从最底层开始开发RTF阅读器时,只需把PlayMetaFile的参数从内存DC换成屏幕DC就可以显示图片了。 |