您的位置:程序门 -> vc/mfc -> 图形处理/算法



如何将24位色bmp图片改为单色或16色?急


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何将24位色bmp图片改为单色或16色?急[已结贴,结贴人:gdlian]
发表于: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;
    }

发表于:2007-02-06 18:44:321楼 得分:0
如果能给个函数将磁盘中的bmp文件压缩或者转换为jpg格式也可以,谢谢大家了啊
发表于:2007-02-06 19:39:382楼 得分:15
用cximage吧
有阈值算法转2值
也可以保存jpg文件
发表于:2007-02-06 21:06:213楼 得分:5
如果能给个函数将磁盘中的bmp文件压缩或者转换为jpg格式也可以,谢谢大家了啊
============================================
用c#很容易实现,估计它也是gdi+,vc应该也可以,呵呵
发表于:2007-02-07 06:32:294楼 得分:0
代码代码   我要代码
发表于:2007-02-07 06:33:425楼 得分:0
不想用类库,我的程序不能太大
发表于:2007-02-07 06:35:276楼 得分:0
也不想用gdi+   就gdi   sdk
发表于:2007-02-26 15:45:517楼 得分:30
http://www.codeguru.com/cpp/g-m/bitmap/article.php/c4927/


快速检索

最新资讯
热门点击