作者:章小胭 | 来源:互联网 | 2023-05-18 07:14
希望实现的程序功能主要是读取一个tga文件并转换文件信息到BITMAP结构中。现在我已经把TGA格式数据转换到BITMAP结构中了,但是在空间上看不到绘制结果。绘制方法:voidCPreView
希望实现的程序功能主要是读取一个tga文件并转换文件信息到BITMAP结构中。现在我已经把TGA格式数据转换到BITMAP结构中了,但是在空间上看不到绘制结果。绘制方法:
void CPreViewDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CPoint pt;
CSize size;
CRect rect;
GetClientRect(rect);
bitmap.CreateBitmapIndirect(&m_bmpColor);
pt.x = 0;
pt.y = 0;
size.cx = rect.Width();
size.cy = rect.Height();
dc.DrawState(pt,size,&bitmap,DST_BITMAP);
// Do not call CDialog::OnPaint() for painting messages
}
其中m_bmpColor是已经定义过了的BITMAP结构。
如果调用资源里的Bitmap位图就可以显示。
void CPreViewDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CPoint pt;
CSize size;
BITMAP Color;
CBitmap bitmap;
CRect rect;
GetClientRect(rect);
bitmap.LoadBitmap(IDB_BITMAP2);
bitmap.GetBitmap(&Color);
pt.x = 0;
pt.y = 0;
size.cx = rect.Width();
size.cy = rect.Height();
dc.DrawState(pt,size,&bitmap,DST_BITMAP);
// Do not call CDialog::OnPaint() for painting messages
}
希望能得到解答
9 个解决方案
现在我已经把TGA格式数据转换到BITMAP结构中了,但是在空间上看不到绘制结果。
===============================
BITMAP结构只是一个结构,它里面记录了位图的一些格式,但是这里面并不含有位图的图像数据,当然画不出来,就像你知道一幅画的长度,大小,色深,你能将这个画还原出来吗?你下面的代码能成功是因为这一句:bitmap.LoadBitmap(IDB_BITMAP2); 它将资源中的位图加载进来了。
BITMAP.bmBits不是存储位图图像数据的么?
如果这种方式不对,那我应该怎么做才能正确的把图像数据显示出来?
谢谢
BITMAP.bmBits不是存储位图图像数据的么?
==============
这不过是一个指向位图数据的指针,它指向一个数组,数组里面放着位图数据,不过,这个数组的内容要你自己放进去的。
问题就是我已经制定了数组,还是显示不出来
//Color
ImageColor.bmHeight = nHight;
ImageColor.bmWidth = nWidth;
ImageColor.bmBitsPixel = 24;
ImageColor.bmPlanes = 1;
ImageColor.bmType = 0;
ImageColor.bmWidthBytes = nWidth * 3;
if((ImageColor.bmWidthBytes % 2) != 0)
{
ImageColor.bmWidthBytes += 1;
}
nBmpDatasize = ImageColor.bmWidthBytes * ImageColor.bmHeight;
for(int IndexHight = 0;IndexHight < nHight;IndexHight++)
{
BmpIndex = (long)IndexHight * ImageColor.bmWidthBytes;
for(int IndexWidth = 0;IndexWidth < nWidth;IndexWidth++)
{
fTgaRead.Read(p8Bitbuf,1);//r
pImagedata[BmpIndex] = p8Bitbuf[0];
fTgaRead.Read(p8Bitbuf,1);//g
pImagedata[BmpIndex+1] = p8Bitbuf[0];
fTgaRead.Read(p8Bitbuf,1);//b
pImagedata[BmpIndex+2] = p8Bitbuf[0];
BmpIndex+=3;
}
}
不好意思少了一段
//Color
ImageColor.bmHeight = nHight;
ImageColor.bmWidth = nWidth;
ImageColor.bmBitsPixel = 24;
ImageColor.bmPlanes = 1;
ImageColor.bmType = 0;
ImageColor.bmWidthBytes = nWidth * 3;
if((ImageColor.bmWidthBytes % 2) != 0)
{
ImageColor.bmWidthBytes += 1;
}
nBmpDatasize = ImageColor.bmWidthBytes * ImageColor.bmHeight;
ImageColor.bmBits = new char[nBmpDatasize];
char * pImagedata = (char *)ImageColor.bmBits;
dc.DrawState(pt,size,&bitmap,DST_BITMAP);
===================
你这里的bitmap是什么啊,按MSDN上说,这应该是HBITMAP,是一个句柄,并不是BITMAP结构啊。
CBitmap bitmap; 是类CBitmap
if(MemDC==NULL)
MemDC=::CreateCompatibleDC(NULL);
hBitmap=::CreateDIBSection(MemDC,ImageColor,DIB_RGB_COLORS,(VOID**)&m_pBits,NULL,0);
if(hBitmap!=0)
{
hOldbmp=(HBITMAP)SelectObject(MemDC,hBitmap);
}
m_pBits=(byte*)pImagedata
Bitblt(显示的设备,0,0,width,height,memdc,0,0,srccopy);
这个代码不一定通过 但是原理是这样的 你都知道了DIB 所有数据 那就用CreateDIBSection创建一个BITMAP 然后把他选入内存 再用bitblt贴到你要显示的DC上 就可以了