为什么在VC++中调用.BMP文件出现颜色失真?

pomelowu 2003-11-07 01:04:11
似乎只有在桌面设置为真彩色、图片是真彩色的时候才不会有问题。请教大家如何修改我的代码才能使在桌面或者位图不是真彩色的时候也能够正常显示图片?
BOOL CDIB::LoadFromFile(LPCTSTR IpszFileName)
{
CFile file;
BITMAPINFO * pBMI=NULL;
BYTE * pDIBData=NULL;

if(!file.Open(IpszFileName,CFile::modeRead|CFile::typeBinary))
{
AfxMessageBox("Cannot Open File!");
return FALSE;
}

BITMAPFILEHEADER bfh;
if (file.Read(&bfh,sizeof(bfh))!=sizeof(bfh))
{
AfxMessageBox("ERROR when reading the file header!");
return FALSE;
}

if (bfh.bfType!=0x4d42)
{
AfxMessageBox("It is not a .BMP file!");
return FALSE;
}

BITMAPINFOHEADER bih;
if (file.Read(&bih,sizeof(bih))!=sizeof(bih))
{
AfxMessageBox("ERROR when reading the file header!");
return FALSE;
}

/*
if (bih.biBitCount!=24)
{
AfxMessageBox("Not a 24-bit color BMP!");
return FALSE;
}
*/

pBMI=(BITMAPINFO*) new char[sizeof (BITMAPINFO)];
if (!pBMI)
{
AfxMessageBox("ERROR when allocating memory!(pBMI)");
return FALSE;
}

file.Seek(sizeof(bfh),CFile::begin);
if(file.Read(pBMI,sizeof(BITMAPINFO))!=sizeof(*pBMI))
{
AfxMessageBox("Error when reading BitMap Info!");
return FALSE;
}

DWORD dataBytes =bfh.bfSize-bfh.bfOffBits;
pDIBData=(BYTE *)new char[dataBytes];

if(!pDIBData)
{
AfxMessageBox("ERROR when allocating memory!(pDIBData)");
delete pBMI;
return FALSE;
}

DWORD test;
if ((test=file.ReadHuge(pDIBData, dataBytes))!=dataBytes)
{
AfxMessageBox("ERROR when reading file!(ReadHuge)");
delete pBMI;
delete pDIBData;
return FALSE;
}

file.Close();

if (m_pBMI !=NULL)
delete m_pBMI;
m_pBMI=pBMI;

if(m_pDIBData!=NULL)
delete m_pDIBData;
m_pDIBData=pDIBData;

return true;
}

void CDIB::ShowDIB(CDC * pDC,int nLeft,int nTop,int nWidth,int nHeight)
{
pDC->SetStretchBltMode(COLORONCOLOR);

StretchDIBits(pDC->GetSafeHdc(),
nLeft,
nTop,
nWidth,
nHeight,
0,
0,
GetDIBWidth(),
GetDIBHeight(),
m_pDIBData,
m_pBMI,
DIB_RGB_COLORS,
SRCCOPY);
}
...全文
79 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pomelowu 2003-11-16
  • 打赏
  • 举报
回复
什么时候试试这个:
http://211.152.9.125/cgi-bin/page.dll?page1?userid=lmj9602&columnno=11&articleid=195
菜牛 2003-11-11
  • 打赏
  • 举报
回复
不是真彩色的位图,自己要处理调色板的吧。
ExcuseMe 2003-11-11
  • 打赏
  • 举报
回复
关注。。。为什么没人回答啦!!!
pomelowu 2003-11-10
  • 打赏
  • 举报
回复
呵呵~~谢谢。我没有安装msdn,这儿又连不上www.msdn.com。
不过我更改了之后图片都出不来了。怎么回事啊?
会思考的草 2003-11-07
  • 打赏
  • 举报
回复
SetStretchBltMode参数,你不会查MSDN么?里面都有解释的。

BLACKONWHITE
Performs a Boolean AND operation using the color values for the eliminated and existing pixels. If the bitmap is a monochrome bitmap, this mode preserves black pixels at the expense of white pixels.

COLORONCOLOR
Deletes the pixels. This mode deletes all eliminated lines of pixels without trying to preserve their information.

HALFTONE
Maps pixels from the source rectangle into blocks of pixels in the destination rectangle. The average color over the destination block of pixels approximates the color of the source pixels. After setting the HALFTONE stretching mode, an application must call the SetBrushOrgEx function to set the brush origin. If it fails to do so, brush misalignment occurs.

STRETCH_ANDSCANS
Same as BLACKONWHITE.

STRETCH_DELETESCANS
Same as COLORONCOLOR.

STRETCH_HALFTONE
Same as HALFTONE.

STRETCH_ORSCANS
Same as WHITEONBLACK.

WHITEONBLACK
Performs a Boolean OR operation using the color values for the eliminated and existing pixels. If the bitmap is a monochrome bitmap, this mode preserves white pixels at the expense of black pixels.
pomelowu 2003-11-07
  • 打赏
  • 举报
回复
不对啊,设置为HALFTONE就没有图像出来了
pomelowu 2003-11-07
  • 打赏
  • 举报
回复
pDC->SetStretchBltMode函数里边可以有哪些参数?分别代表什么意义呢?
会思考的草 2003-11-07
  • 打赏
  • 举报
回复
pDC->SetStretchBltMode(HALFTONE);//true color

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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