把char*转为C++的BITMAP对象

小·资 2019-06-18 05:37:38
foxit::common::Bitmap bitmap;
bitmap = image_object->CloneBitmap(page);
byte* buffer = (byte*)bitmap.GetBuffer();

BITMAPINFO bmpInfo; //创建位图
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);;
bmpInfo.bmiHeader.biWidth = bitmap.GetWidth();//宽度
bmpInfo.bmiHeader.biHeight = bitmap.GetHeight();//高度
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = bitmap.GetBpp();
bmpInfo.bmiHeader.biCompression = BI_RGB;
//bmpInfo.bmiColors[]= buffer;
hBitmap = CreateDIBSection(_compDC, &bmpInfo, DIB_RGB_COLORS, (void**)out, NULL, 0);
image.Attach(hBitmap);
image.Save("D:\\cunchu1.bmp");





要将别的图片对象转化为Cimage对象,发现可以获取福昕的图片对象里的buffer,上网查了这个是位图的RGB数据,可是不知道怎么使用。又找到了可以使用CreateDIBSection得到BITMAP对象,可是根本不知道怎么初始化bmpInfo.bmiColors[],找了好多帖子也没有。奔溃了要。
...全文
685 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2019-06-19
  • 打赏
  • 举报
回复
小·资 2019-06-19
  • 打赏
  • 举报
回复
问题都解决了,缺损还是大小问题,大小改为( bitmap.GetWidth()+1)*3*bitmap.GetHeight()就可以了,图片是反的我觉得是因为foxit的BITMAP的RGB编码顺序和C++就是反的,我直接在读取前把图片翻转了,顺道一提bitmap是福昕的foxit::common::Bitmap类,结贴了,虽然赵四老师的东西没什么用,还是感谢赵四老师
小·资 2019-06-19
  • 打赏
  • 举报
回复
图片转出来了,重点在RGB数据给到内存的长度,不是buffer本身的长度而是图片的大小,即width*height*3,但是现在出来的图片是反的,而且像素有一块缺损 CImage image; char* buffer = (char*)bitmap.GetBuffer(); HBITMAP hBitmap; char** out=new char*; HDC _compDC = CreateCompatibleDC(AfxGetMainWnd()->GetDC()->m_hDC); BITMAPINFO bmpInfo; //创建位图 bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) ; bmpInfo.bmiHeader.biWidth = bitmap.GetWidth();//宽度 bmpInfo.bmiHeader.biHeight = bitmap.GetHeight();//高度 bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = bitmap.GetBpp(); bmpInfo.bmiHeader.biCompression = BI_RGB; hBitmap = CreateDIBSection(_compDC, &bmpInfo, DIB_RGB_COLORS, (void**)out, NULL, 0); int n = _mbslen((unsigned char*)buffer); memcpy(out[0],buffer, bitmap.GetWidth()*3*bitmap.GetHeight()); image.Attach(hBitmap); image.Save("D:\\cunchu1.jpg"); //Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length);
赵4老师 2019-06-19
  • 打赏
  • 举报
回复
BOOL LoadPngImg(LPCTSTR filename,HBITMAP * hbit) {
    //查找文件是否存在
    if (!FindFile(filename)) return FALSE;
    IImagingFactory* pImageFactory = 0;
    IImage* pImage = 0;
    ImageInfo imageInfo;
    //CoInitializeEx(0, COINIT_MULTITHREADED);
    HBITMAP hBitmap = 0;
    LPBYTE lpByte;
    if (SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory, 0, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void**)&pImageFactory))) {
        if (SUCCEEDED(pImageFactory->CreateImageFromFile(filename, &pImage))&& SUCCEEDED(pImage->GetImageInfo(&imageInfo))) {
            //HDC bmpDC = CreateCompatibleDC(hdc);
            //LPBYTE lpByte;
            BITMAPINFO *pbinfo ;
            pbinfo = (BITMAPINFO *)calloc(1, sizeof(BITMAPINFO) + 4 * sizeof(INT)) ;
            if(!pbinfo) return FALSE ;
            pbinfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
            pbinfo->bmiHeader.biWidth = imageInfo.Width ;
            pbinfo->bmiHeader.biHeight = imageInfo.Height ;
            pbinfo->bmiHeader.biPlanes = 1;
            pbinfo->bmiHeader.biBitCount = 32;
            pbinfo->bmiHeader.biCompression = BI_ALPHABITFIELDS;
            pbinfo->bmiHeader.biSizeImage = 0 ;
            pbinfo->bmiHeader.biXPelsPerMeter = 11811;
            pbinfo->bmiHeader.biYPelsPerMeter = 11811;
            pbinfo->bmiHeader.biClrUsed = 0;
            pbinfo->bmiHeader.biClrImportant = 0;
            int *pMask = (int*)&(pbinfo->bmiColors[0]) ;
            *pMask++ = 0x00FF0000 ;
            *pMask++ = 0x0000FF00 ;
            *pMask++ = 0x000000FF ;
            *pMask++ = 0xFF000000 ;
            hBitmap = CreateDIBSection(NULL, pbinfo, DIB_RGB_COLORS, (void **)&lpByte, NULL, 0) ;
            free(pbinfo) ;
            if(!hBitmap || !lpByte) return FALSE ;
            RECT rect = {0, 0, imageInfo.Width, imageInfo.Height};
            IBitmapImage *pBitmapImage;
            BitmapData bitmapData;
            bitmapData.Width = imageInfo.Width;
            bitmapData.Height = imageInfo.Height;
            bitmapData.PixelFormat = imageInfo.PixelFormat;
            pBitmapImage = NULL;
            pImageFactory->CreateBitmapFromImage(pImage, imageInfo.Width, imageInfo.Height, PIXFMT_32BPP_ARGB,InterpolationHintDefault, &pBitmapImage);
            pBitmapImage->LockBits(&rect, ImageLockModeRead,PIXFMT_32BPP_ARGB, &bitmapData);
            //transferring the pixels
            memcpy(lpByte, bitmapData.Scan0, imageInfo.Width * imageInfo.Height * 4);
            pBitmapImage->UnlockBits(&bitmapData);
            pBitmapImage->Release();
            pImage->Release();
            // DeleteDC(bmpDC);
        }
        pImageFactory->Release();
    }
    //CoUninitialize();
    //ProcessThePixelsWithAlphaChannel Here
    // vertical flip and ProcessThePixelsWithAlphaChannel here
    for (UINT y=0; y<imageInfo.Height/2; y++) {
        BYTE * pPixel = (BYTE *) lpByte + imageInfo.Width * 4 * y;
        BYTE * pDstPixel = (BYTE*) lpByte + imageInfo.Width * 4 * (imageInfo.Height-y-1);
        for (UINT x=0; x<imageInfo.Width; x++) {
            pPixel[0] = pPixel[0] * pPixel[3] / 255;
            pPixel[1] = pPixel[1] * pPixel[3] / 255;
            pPixel[2] = pPixel[2] * pPixel[3] / 255;
            pDstPixel[0] = pDstPixel[0] * pDstPixel[3] / 255;
            pDstPixel[1] = pDstPixel[1] * pDstPixel[3] / 255;
            pDstPixel[2] = pDstPixel[2] * pDstPixel[3] / 255;
            INT* pOrigin = (INT*)pPixel;
            INT* pDst    = (INT*)pDstPixel;
            INT temp = *pOrigin;
            *pOrigin = *pDst;
            *pDst = temp;
            pPixel += 4;
            pDstPixel += 4;
        }
    }
    *hbit= hBitmap;
    if (!hbit) return FALSE;
    return TRUE;
}
小·资 2019-06-19
  • 打赏
  • 举报
回复
char* buffer = (char*)bitmap.GetBuffer(); CString str = buffer; foxit::common::Bitmap mybit(bitmap.GetWidth(), bitmap.GetWidth(), bitmap.GetFormat(), (foxit::uint8*) buffer, 0); HBITMAP hBitmap; char** out=new char*; HDC _compDC = CreateCompatibleDC(AfxGetMainWnd()->GetDC()->m_hDC); //setbih(iWidth, iHeight); BITMAPINFO bmpInfo; //创建位图 bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);; bmpInfo.bmiHeader.biWidth = bitmap.GetWidth();//宽度 bmpInfo.bmiHeader.biHeight = bitmap.GetHeight();//高度 bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = bitmap.GetBpp(); bmpInfo.bmiHeader.biCompression = BI_RGB; //bmpInfo.bmiColors[]= buffer; hBitmap = CreateDIBSection(_compDC, &bmpInfo, DIB_RGB_COLORS, (void**)out, NULL, 0); memcpy(out,buffer,str.GetLength()); image.Attach(hBitmap); image.Save("D:\\cunchu1.bmp");
小·资 2019-06-19
  • 打赏
  • 举报
回复
有人告诉我CreateDIBSection的第四个参数就是像素数据,把char*的像素数据memcpy给第四个输出指针就行,可是我操作了以后失败了

64,686

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧