• 大小: 2.18MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-21
  • 语言: C/C++
  • 标签: VC++  bmp  

资源简介

用MFC中的单文档视图打开bmp图片,并显示

资源截图

代码片段和文件信息

// ImageDib.cpp: implementation of the ImageDib class.
//
//////////////////////////////////////////////////////////////////////

#include “stdafx.h“
#include “SingleDocView.h“
#include “ImageDib.h“

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ImageDib::ImageDib()
{
m_lpDib=NULL;
m_lpColorTable=NULL;
m_pImgData=NULL;
m_lpBmpInfoHead=NULL;
m_hPalette=NULL;
}

ImageDib::~ImageDib()
{
if(m_lpDib!=NULL)
delete [] m_lpDib;
if(m_hPalette!=NULL)
::Deleteobject(m_hPalette);
}
BOOL ImageDib::Read(LPCTSTR lpszPathName)
{
CFile file;
if(!file.Open(lpszPathNameCFile::modeRead | CFile::shareDenyWrite))
return FALSE;
BITMAPFILEHEADER bmfh;
int nCount=file.Read((LPVOID)&bmfhsizeof(BITMAPFILEHEADER));
if(m_lpDib!=NULL) delete []m_lpDib;
m_lpDib=new BYTE[file.GetLength()-sizeof(BITMAPFILEHEADER)];
file.Read(m_lpDibfile.GetLength()-sizeof(BITMAPFILEHEADER));
m_lpBmpInfoHead=(LPBITMAPINFOHEADER)m_lpDib;
m_imgWidth=m_lpBmpInfoHead->biWidth;
m_imgHeight=m_lpBmpInfoHead->biHeight;
m_nBitCount=m_lpBmpInfoHead->biBitCount;
m_nColorTableLength=ComputeColorTabalLength(m_lpBmpInfoHead->biBitCount);
m_hPalette=NULL;
if(m_nColorTableLength!=0)
{
m_lpColorTable=(LPRGBQUAD)(m_lpDib+sizeof(BITMAPINFOHEADER));
MakePalette();
}
m_pImgData=(LPBYTE)m_lpDib+sizeof(BITMAPINFOHEADER)+
sizeof(RGBQUAD)*m_nColorTableLength;
return TRUE;
}
BOOL ImageDib::Write(LPCTSTR lpszPathName)
{
CFile file;
if(!file.Open(lpszPathNameCFile::modeCreate | CFile::modeReadWrite 
| CFile::shareExclusive))
return FALSE;
BITMAPFILEHEADER bmfh;
bmfh.bfType=0x4d42;
bmfh.bfSize=0;
bmfh.bfReserved1=bmfh.bfReserved2=0;
bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+
sizeof(RGBQUAD)*m_nColorTableLength;
try
{
file.Write((LPVOID)&bmfhsizeof(BITMAPFILEHEADER));
file.Write(m_lpBmpInfoHeadsizeof(BITMAPINFOHEADER));
if(m_nColorTableLength!=0)
file.Write(m_lpColorTablesizeof(RGBQUAD)*m_nColorTableLength);
int imgBufSize=(m_imgWidth*m_nBitCount/8+3)/4*4*m_imgHeight;
file.Write(m_pImgDataimgBufSize);
}
catch(CException* pe)
{
pe->Delete();
AfxMessageBox(“write error“);
return FALSE;
}
return TRUE;
}
void ImageDib::MakePalette()
{
if(m_nColorTableLength==0)
return;
if(m_hPalette!=NULL)
::Deleteobject(m_hPalette);
LPLOGPALETTE pLogPal=(LPLOGPALETTE)new char[2*sizeof(WORD)+
m_nColorTableLength*sizeof(PALETTEENTRY)];
pLogPal->palVersion=0x300;
pLogPal->palNumEntries=m_nColorTableLength;
LPRGBQUAD m_lpDibQuad=(LPRGBQUAD)m_lpColorTable;
for(int i=0;i {
pLogPal->palPalEntry[i].peRed=m_lpDibQuad->rgbRed;
pLogPal->palPalEntry[i].peGreen=m_lpDibQuad->rgbGreen;
pLogPal-

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      15684  2014-09-28 12:07  SingleDocView\Debug\ImageDib.obj

     文件      20028  2014-09-27 23:11  SingleDocView\Debug\MainFrm.obj

     文件     122955  2014-09-28 12:07  SingleDocView\Debug\SingleDocView.exe

     文件     328220  2014-09-28 12:07  SingleDocView\Debug\SingleDocView.ilk

     文件      23666  2014-09-27 23:11  SingleDocView\Debug\SingleDocView.obj

     文件    5493672  2014-09-27 23:11  SingleDocView\Debug\SingleDocView.pch

     文件     345088  2014-09-28 12:07  SingleDocView\Debug\SingleDocView.pdb

     文件       7452  2014-09-27 23:11  SingleDocView\Debug\SingleDocView.res

     文件      18481  2014-09-28 12:07  SingleDocView\Debug\SingleDocViewDoc.obj

     文件      19990  2014-09-28 12:07  SingleDocView\Debug\SingleDocViewView.obj

     文件     105774  2014-09-27 23:11  SingleDocView\Debug\StdAfx.obj

     文件     214016  2014-09-28 12:07  SingleDocView\Debug\vc60.idb

     文件     364544  2014-09-28 12:07  SingleDocView\Debug\vc60.pdb

     文件       5596  2014-09-28 12:02  SingleDocView\ImageDib.cpp

     文件       1743  2014-09-28 11:57  SingleDocView\ImageDib.h

     文件       2514  2014-09-27 23:11  SingleDocView\MainFrm.cpp

     文件       1581  2014-09-27 23:11  SingleDocView\MainFrm.h

     文件       4776  2014-09-27 23:11  SingleDocView\ReadMe.txt

     文件       1078  2014-09-27 23:11  SingleDocView\res\SingleDocView.ico

     文件        405  2014-09-27 23:11  SingleDocView\res\SingleDocView.rc2

     文件       1078  2014-09-27 23:11  SingleDocView\res\SingleDocViewDoc.ico

     文件       1078  2014-09-27 23:11  SingleDocView\res\Toolbar.bmp

     文件        501  2014-09-27 23:11  SingleDocView\Resource.h

     文件      28820  2014-09-28 08:13  SingleDocView\SingleDocView.aps

     文件       2395  2014-09-28 12:02  SingleDocView\SingleDocView.clw

     文件       4481  2014-09-27 23:11  SingleDocView\SingleDocView.cpp

     文件       4897  2014-09-28 11:57  SingleDocView\SingleDocView.dsp

     文件        551  2014-09-27 23:11  SingleDocView\SingleDocView.dsw

     文件       1433  2014-09-27 23:11  SingleDocView\SingleDocView.h

     文件      91136  2014-09-28 12:19  SingleDocView\SingleDocView.ncb

............此处省略16个文件信息

评论

共有 条评论