首页新闻找找看学习计划

C++怎么把一块图片二进制数据显示成图片

0
悬赏园豆:50 [已解决问题] 解决于 2014-02-09 12:56

相机传过来一组图像数据,这些数据经过开发板处理后再通过网线传递到电脑上。

电脑能够获取图片内存的启示地址,怎么才能把这图片显示出来呢。求指教,实在VC++  MFC环境下。这里说明一下,传过来的数据没有头也就是只有纯粹的像素数据BUFF

大芝麻的主页 大芝麻 | 初学一级 | 园豆:4
提问于:2014-02-08 12:58
< >
分享
最佳答案
0

假设你的图片图书首地址是 pBuffer,大小是 IMG_BKGND_SIZE,那么大概的代码是这样:

HGLOBAL hGlob = GlobalAlloc(GMEM_FIXED, IMG_BKGND_SIZE);

memcpy_s(hGlob,IMG_BKGND_SIZE,pBuffer,IMG_BKGND_SIZE);

 

CComPtr<IStream> spStream;
CreateStreamOnHGlobal(hGlob, TRUE, &spStream);

IPicture *pPicture; 

OleLoadPicture(spStream,IMG_BKGND_SIZE,TRUE,IID_IPicture,(void **)&pPicture);

// 宽高,MM_HIMETRIC 模式,单位是0.01毫米      

LONG cx,cy;      

pPicture->get_Width(&cx);      

pPicture->get_Height(&cy);

// 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位      

SIZE sz = {cx,cy};      

dc.HIMETRICtoDP(&sz);

pPicture->Render(dc.m_hDC,0,0,sz.cx,sz.cy,0,cy,cx,-cy,NULL);      

pPicture->Release();

spStream.Release();

收获园豆:50
Launcher | 高人七级 |园豆:45040 | 2014-02-08 16:08

出现了一个异常,在pPicture->get_Width(&cx);

大芝麻 | 园豆:4 (初学一级) | 2014-02-08 19:44

unsigned char pBuffer[10000];

memset(pBuffer,255,10000);
HGLOBAL hGlob=GlobalAlloc(GMEM_FIXED,10000);
memcpy_s(hGlob,10000,pBuffer,10000);

CComPtr<IStream> spStream;
CreateStreamOnHGlobal(hGlob,TRUE,&spStream);

IPicture *pPicture;
OleLoadPicture(spStream,10000,TRUE,IID_IPicture,(void**)&pPicture);
LONG cx,cy;
pPicture->get_Width(&cx);
pPicture->get_Height(&cy);
SIZE sz={cx,cy};

this->GetDC()->HIMETRICtoDP(&sz);

pPicture->Render(this->GetDC()->m_hDC,0,0,sz.cx,sz.cy,0,cy,cx,-cy,NULL);
pPicture->Release();
spStream.Release();

大芝麻 | 园豆:4 (初学一级) | 2014-02-08 19:44

不太熟悉这个,但感觉代码好掌握些,求大侠指导

大芝麻 | 园豆:4 (初学一级) | 2014-02-08 19:45

试了一下自己填充一个位图头信息到穿过来的数据前面就能正常显示图片了。谢谢大侠指导!

大芝麻 | 园豆:4 (初学一级) | 2014-02-09 12:55
其他回答(2)
0

  /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="imgBuffer">原图byte[]</param>
    /// <param name="width">生成的缩略图宽度</param>
    /// <param name="height">生成的缩略图高度</param>
    /// <returns></returns>
    private byte[] GenerateThumbImg(byte[] imgBuffer,int width,int height)
    {
        MemoryStream imgStream = null;
        MemoryStream thumbStream = new MemoryStream();;
        System.Drawing.Image img = null;
        System.Drawing.Image thumbImg = null;
        System.Drawing.Graphics g = null;
        try
        {
            imgStream = new MemoryStream(imgBuffer);
            img = System.Drawing.Image.FromStream(imgStream);
            thumbImg = new System.Drawing.Bitmap(img, width, height);
            g = System.Drawing.Graphics.FromImage(thumbImg);
            // 设置画布的描绘质量
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(thumbImg, 0, 0, width, height);
            /*g.DrawImage(img, new System.Drawing.Rectangle(0, 0, width, height),
                0, 0, width, height, System.Drawing.GraphicsUnit.Pixel);*/
            thumbImg.Save(thumbStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            return thumbStream.ToArray();
        }
        catch(Exception ex)
        {
            return null;
        }
        finally
        {
            if (g != null)
                g.Dispose();
            if (thumbImg != null)
                thumbImg.Dispose();
            if (img != null)
                img.Dispose();
            if (thumbStream != null)
                thumbStream.Close();
            if (imgStream != null)
                imgStream.Close();
        }
    }

雾静 | 园豆:561 (小虾三级) | 2014-02-08 13:40

要是C++,MFC框架下开发的

支持(0) 反对(0) 大芝麻 | 园豆:4 (初学一级) | 2014-02-08 14:37
0

MFC里不是有picture控件的么,绘制bmp就可以了啊

三胖他爹 | 园豆:116 (初学一级) | 2014-02-08 15:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册