相机传过来一组图像数据,这些数据经过开发板处理后再通过网线传递到电脑上。
电脑能够获取图片内存的启示地址,怎么才能把这图片显示出来呢。求指教,实在VC++ MFC环境下。这里说明一下,传过来的数据没有头也就是只有纯粹的像素数据BUFF
假设你的图片图书首地址是 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();
出现了一个异常,在pPicture->get_Width(&cx);
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();
不太熟悉这个,但感觉代码好掌握些,求大侠指导
试了一下自己填充一个位图头信息到穿过来的数据前面就能正常显示图片了。谢谢大侠指导!
/// <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();
}
}
要是C++,MFC框架下开发的
MFC里不是有picture控件的么,绘制bmp就可以了啊