//打开磁盘上位图文件
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
Width=Width+4;
Height=(sourceinfoheader.biHeight/4)*4;
if(Height
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=1;i
{
for(j=1;j
{
for (k=0; k<3; k++)
{
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);
}