| 发表于:2007-02-06 16:14:59 楼主 |
哪位朋友对gdi函数比较熟悉,帮个忙好吗,我有个函数是截取屏幕然后保存为bmp文件,我想修改这些代码,使保存的bmp文件为256色,或者单色,如果能压缩尺寸就更好了,我贴出代码,希望熟悉gdi的朋友帮个忙,急用啊 50分相送 我想不会修改太多地方的吧 int saveclienttofile(hwnd hwnd, lpctstr szfname) { hdc hdc, hmemdc; handle hbits, hfile; hbitmap hbitmap, htmpbmp; hpalette hpal; lpvoid lpbits; rgbquad rgbquad; dword imgsize, plsize, dwwritten; int i, cres, height, width; bitmapfileheader bmfh; lpbitmapinfo pbminfo, pbminfomem; lplogpalette lp; if((hfile = createfile(szfname, generic_write,0,null,create_always,file_attribute_normal,null)) == null) return 1; //按照参数szfname指定的路径创建文件 //setcursor(loadcursor(null, idc_wait)); //我日,居然让鼠标成漏斗状 if(hwnd==hwnd_desktop) { width = getsystemmetrics(sm_cxscreen); height = getsystemmetrics(sm_cyscreen); } else { rect rc; getclientrect(hwnd,&rc); width = rc.right-rc.left; height = rc.bottom-rc.top; } hdc = getdc(hwnd); //获取指定的dc hmemdc = createcompatibledc(hdc); hbitmap= createcompatiblebitmap(hdc, width, height); htmpbmp= createcompatiblebitmap(hdc, 8, 8); pbminfomem = (lpbitmapinfo)globalalloc(ghnd, sizeof(bitmapinfo)+256*sizeof(rgbquad)); pbminfo = (lpbitmapinfo)globallock(pbminfomem); selectobject(hmemdc, hbitmap); bitblt(hmemdc,0,0,width,height,hdc,0,0,srccopy); selectobject(hmemdc, htmpbmp); zeromemory(pbminfo, sizeof(bitmapinfo)); pbminfo-> bmiheader.bisize = (dword)sizeof(bitmapinfoheader); pbminfo-> bmiheader.biwidth = width; pbminfo-> bmiheader.biheight = height; pbminfo-> bmiheader.biplanes = 1; pbminfo-> bmiheader.bibitcount = (word)getdevicecaps(hdc, bitspixel); pbminfo-> bmiheader.bicompression = bi_rgb; //pbminfo-> bmiheader.bicompression = bi_rle8; getdibits(hdc, hbitmap, 0,height, null, pbminfo, dib_rgb_colors); if(!pbminfo-> bmiheader.bisizeimage) pbminfo-> bmiheader.bisizeimage = ((((pbminfo-> bmiheader.biwidth * pbminfo-> bmiheader.bibitcount) + 31) & ~31) / 8) * pbminfo-> bmiheader.biheight; cres = getdevicecaps(hdc, sizepalette); plsize = cres*sizeof(rgbquad); imgsize= pbminfo-> bmiheader.bisizeimage; bmfh.bftype = 0x4d42; // "bm " bmfh.bfoffbits = plsize + sizeof(bitmapinfoheader) + sizeof(bitmapfileheader); bmfh.bfsize = imgsize + bmfh.bfoffbits; bmfh.bfreserved1 = 0; bmfh.bfreserved2 = 0; writefile(hfile, &bmfh, sizeof(bitmapfileheader), &dwwritten, null); writefile(hfile, &(pbminfo-> bmiheader), sizeof(bitmapinfoheader), &dwwritten, null); if(cres) { hpal=(hpalette)globalalloc(ghnd, sizeof(logpalette) + (cres*sizeof(paletteentry))); lp=(lplogpalette)globallock(hpal); lp-> palnumentries=(word)cres; lp-> palversion=0x0300; getsystempaletteentries(hdc, 0, cres, lp-> palpalentry); rgbquad.rgbreserved=0; for(i=0; i <cres; i++) { rgbquad.rgbred = lp-> palpalentry[i].pered; rgbquad.rgbgreen = lp-> palpalentry[i].pegreen; rgbquad.rgbblue = lp-> palpalentry[i].peblue; writefile(hfile, &rgbquad, sizeof(rgbquad), &dwwritten, null); } globalunlock(hpal); globalfree(hpal); } hbits = globalalloc(ghnd, pbminfo-> bmiheader.bisizeimage); lpbits = (lpvoid)globallock(hbits); getdibits(hdc, hbitmap, 0,height, lpbits, pbminfo, dib_rgb_colors); writefile(hfile,lpbits,imgsize,&dwwritten,null); globalunlock(hbits); globalfree(hbits); globalunlock(pbminfo); globalfree(pbminfomem); deleteobject(htmpbmp); deleteobject(hbitmap); deletedc(hmemdc); releasedc(hwnd,hdc); closehandle(hfile); //setcursor(loadcursor(null,idc_arrow)); return 0; } |
|
|
|
|