如何在MFC中加载BMP图,并进行处理

如何在MFC中加载BMP图,并进行处理,第1张

//打开磁盘上位图文件

void CMyBitmapView::OnFileOpen()

{

// TODO: Add your command handler code here

CFileDialog fileDlg(TRUE)

fileDlg.m_ofn.lpstrTitle = "我的位图打开对话框"

fileDlg.m_ofn.lpstrFilter = "Text Files(*.bmp)\0*.bmp\0All Files(*.*)\0*.*\0\0"

if (IDOK==fileDlg.DoModal())

{

m_sourcefile = fileDlg.GetFileName()

}

Invalidate(TRUE) //重绘

}

//处理位图

void CMyBitmapView::OnMakeBmp()

{

// TODO: Add your command handler code here

CFile file(m_sourcefile,CFile::modeRead)

//BYTE* sourcebuf

//提取原图文件头

file.Read((void*)&sourcefileheader,sizeof(BITMAPFILEHEADER))

//提取文件信息头

file.Read((void*)&sourceinfoheader,sizeof(BITMAPINFOHEADER))

//这里是因为BMP规定保存时长度和宽度必须是4的整数倍,如果不是则要补足

int Width,Height,i,j,k

Width=(sourceinfoheader.biWidth/4)*4

if(Width<sourceinfoheader.biWidth)

Width=Width+4

Height=(sourceinfoheader.biHeight/4)*4

if(Height<sourceinfoheader.biHeight)

Height=Height+4

sourcebuf=(BYTE*)malloc(Width*Height*3)

//读取原图的颜色矩阵像素

file.Read(sourcebuf,Width*Height*3)

file.Close()

BYTE* targetbuf

targetbuf=(BYTE*)malloc(Width*Height*3)

for(i=1i<Height-1i++) // 每行

{

for(j=1j<Width-1j++) // 每列

{

for (k=0k<3k++)

{

targetbuf[(i*Width+j)*3+k] = sourcebuf[(i*Width+j)*3+k] - sourcebuf[((i-1)*Width+(j+1))*3+k] +128

if (targetbuf[(i*Width+j)*3+k]>255)

targetbuf[(i*Width+j)*3+k] = 255

if (targetbuf[(i*Width+j)*3+k]<0)

targetbuf[(i*Width+j)*3+k] = 0

}

}

}

CFile file1(m_sourcefile,CFile::modeCreate | CFile::modeWrite)

//file.SeekToBegin()

//写入保存位图文件头

file1.Write((void*)&sourcefileheader,sizeof(BITMAPFILEHEADER))

//写入保存位图信息头

file1.Write((void*)&sourceinfoheader,sizeof(BITMAPINFOHEADER))

//写入保存位图的颜色矩阵像素

file1.Write(targetbuf,Width*Height*3)

//关闭位图文件

file1.Close()

//重绘

Invalidate(TRUE)

// 释放内存

free( sourcebuf )

free( targetbuf )

}

//显示位图

void CMyBitmapView::OnDraw(CDC* pDC)

{

CMyBitmapDoc* pDoc = GetDocument()

ASSERT_VALID(pDoc)

// TODO: add draw code for native data here

HBITMAP bitmap

//读取制定路径的位图文件

bitmap=(HBITMAP)LoadImage(

AfxGetInstanceHandle(),

m_sourcefile,

IMAGE_BITMAP,

0,

0,

LR_LOADFROMFILE|LR_CREATEDIBSECTION

)

//创建兼容的设备描述表

HBITMAP OldBitmap

CDC MemDC

MemDC.CreateCompatibleDC(pDC)

CRect rect

GetClientRect(rect)

//位图选入兼容DC中

OldBitmap=(HBITMAP)MemDC.SelectObject(bitmap)

//绘制位图

pDC->BitBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,SRCCOPY)

MemDC.SelectObject(OldBitmap)

}

步骤一:导入bmp文件:

第二步:设置为背景:

① 添加OnCtlColor消息响应函数:

② 添加响应代码:

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 

{

HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor)

// TODO: Change any attributes of the DC here

static CBrush gBr

static bool isInited = false

if(!isInited)

{

CBitmap bitmap

bitmap.LoadBitmap(IDB_BITMAP1)

gBr.CreatePatternBrush(&bitmap)

COLORREF clearColor = -1

bitmap.DeleteObject()

isInited = true

}

if(pWnd==this)

{

pDC->SetBkMode(TRANSPARENT)

return gBr //主窗口背景使用这个背景刷

}

else

{

pDC->SetBkMode(TRANSPARENT)

return   (HBRUSH)::GetStockObject(NULL_BRUSH) //其他控件使用透明背景

}

// TODO: Return a different brush if the default is not desired

return hbr

}

③效果图:

静态文本框、单行多行输入文本框、列表框、ListCtrl等都可以共享主窗口的背景,但是按钮需要另外设置才可以。

MFC中有一个位图类CBitmap,你可在资源中导入图片,必须是.bmp格式,然后定义类,最后Bitblt这个函数可以把位图下载到单文档或者其它工程中直接显示图片。\x0d\x0a下面是一份资料,你自己看看吧。\x0d\x0a在Windows中可以将预先准备好的图像复制到显示区域中,这种内存拷贝执行起来是非常快的。在Windows中提供了两种使用图形拷贝的方法:通过设备相关位图(DDB)和设备无关位图(DIB)。\x0d\x0a\x0d\x0aDDB可以用MFC中的CBitmap来表示,而DDB一般是存储在资源文件中,在加载时只需要通过资源ID号就可以将图形装入。BOOL CBitmap::LoadBitmap( UINT nIDResource )可以装入指定DDB,但是在绘制时必须借助另一个和当前绘图DC兼容的内存DC来进行。通过CDC::BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop )绘制图形,同时指定光栅 *** 作的类型。BitBlt可以将源DC中位图复制到目的DC中,其中前四个参数为目的区域的坐标,接下来是源DC指针,然后是源DC中的起始坐标,由于BitBlt为等比例复制,所以不需要再次指定长宽,(StretchBlt可以进行缩放)最后一个参数为光栅 *** 作的类型,可取以下值: \x0d\x0a\x0d\x0aBLACKNESS 输出区域为黑色 Turns all output black.\x0d\x0a\x0d\x0aDSTINVERT 反色输出区域 Inverts the destination bitmap.\x0d\x0a\x0d\x0aMERGECOPY 在源和目的间使用AND *** 作 Combines the pattern and the source bitmap using the Boolean AND operator.\x0d\x0a\x0d\x0aMERGEPAINT 在反色后的目的和源间使用OR *** 作 Combines the inverted source bitmap with the destination bitmap using the Boolean OR operator.\x0d\x0a\x0d\x0aNOTSRCCOPY 将反色后的源拷贝到目的区 Copies the inverted source bitmap to the destination.\x0d\x0a\x0d\x0aPATINVERT 源和目的间进行XOR *** 作 Combines the destination bitmap with the pattern using the Boolean XOR operator.\x0d\x0a\x0d\x0aSRCAND 源和目的间进行AND *** 作 Combines pixels of the destination and source bitmaps using the Boolean AND operator.\x0d\x0a\x0d\x0aSRCCOPY 复制源到目的区 Copies the source bitmap to the destination bitmap.\x0d\x0a\x0d\x0aSRCINVERT 源和目的间进行XOR *** 作 Combines pixels of the destination and source bitmaps using the Boolean XOR operator.\x0d\x0a\x0d\x0aSRCPAINT 源和目的间进行OR *** 作 Combines pixels of the destination and source bitmaps using the Boolean OR operator.\x0d\x0a\x0d\x0aWHITENESS 输出区域为白色 Turns all output white. \x0d\x0a下面用代码演示这种方法:\x0d\x0aCYourView::OnDraw(CDC* pDC)\x0d\x0a{\x0d\x0a CDC memDC//定义一个兼容DC\x0d\x0a memDC.CreateCompatibleDC(pDC)//创建DC\x0d\x0a CBitmap bmpDraw\x0d\x0a bmpDraw.LoadBitmap(ID_BMP)//装入DDB\x0d\x0a CBitmap* pbmpOld=memDC.SelectObject(&bmpDraw)//保存原有DDB,并选入新DDB入DC\x0d\x0a pDC->BitBlt(0,0,20,20,&memDC,0,0,SRCCOPY)//将源DC中(0,0,20,20)复制到目的DC(0,0,20,20)\x0d\x0a pDC->BitBlt(20,20,40,40,&memDC,0,0,SRCAND)//将源DC中(0,0,20,20)和目的DC(20,20,40,40)中区域进行AND *** 作\x0d\x0a memDC.SelectObject(pbmpOld)//选入原DDB\x0d\x0a}\x0d\x0a\x0d\x0a(图标并不是一个GDI对象,所以不需要选入DC)在MFC中没有一个专门的图标类,因为图标的 *** 作比较简单,使用HICON CWinApp::LoadIcon( UINT nIDResource )或是HICON CWinApp::LoadStandardIcon( LPCTSTR lpszIconName ) 装入后就可以利用BOOL CDC::DrawIcon( int x, int y, HICON hIcon )绘制。由于在图标中可以指定透明区域,所以在某些需要使用非规则图形而且面积不大的时候使用图标会比较简单。下面给出简单的代码: \x0d\x0a\x0d\x0aOnDraw(CDC* pDC)\x0d\x0a{\x0d\x0a HICON hIcon1=AfxGetApp()->LoadIcon(IDI_I1)\x0d\x0a HICON hIcon2=AfxGetApp()->LoadIcon(IDI_I2)\x0d\x0a pDC->DrawIcon(0,0,hIcon1)\x0d\x0a pDC->DrawIcon(0,40,hIcon2)\x0d\x0a DestroyIcon(hIcon1)\x0d\x0a DestroyIcon(hIcon2)\x0d\x0a}\x0d\x0a\x0d\x0a同样在MFC也没有提供一个DIB的类,所以在使用DIB位图时我们需要自己读取位图文件中的头信息,并读入数据,并利用API函数StretchDIBits绘制。位图文件以BITMAPFILEHEADER结构开始,然后是BITMAPINFOHEADER结构和调色版信息和数据,其实位图格式是图形格式中最简单的一种,而且也是Windows可以理解的一种。我不详细讲解DIB位图的结构,提供一个CDib类供大家使用,这个类包含了基本的功能如:Load,Save,Draw。DownLoad


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11652450.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存