📄 bitmapview.cpp
字号:
// BitmapView.cpp : implementation of the CBitmapView class
//
#include <math.h>
#include "stdafx.h"
#include "Bitmap.h"
#include "BitmapDoc.h"
#include "BitmapView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBitmapView
IMPLEMENT_DYNCREATE(CBitmapView, CView)
BEGIN_MESSAGE_MAP(CBitmapView, CView)
//{{AFX_MSG_MAP(CBitmapView)
ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771)
ON_COMMAND(ID_MENUITEM32772, OnMenuitem32772)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBitmapView construction/destruction
CBitmapView::CBitmapView()
{
// TODO: add construction code here
}
CBitmapView::~CBitmapView()
{
}
BOOL CBitmapView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CBitmapView drawing
void CBitmapView::OnDraw(CDC* pDC)
{
CBitmapDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CBitmapView printing
BOOL CBitmapView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CBitmapView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CBitmapView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CBitmapView diagnostics
#ifdef _DEBUG
void CBitmapView::AssertValid() const
{
CView::AssertValid();
}
void CBitmapView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CBitmapDoc* CBitmapView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBitmapDoc)));
return (CBitmapDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBitmapView message handlers
void CBitmapView::OnMenuitem32771()
{
// TODO: Add your command handler code here
CString StrFilter="位图文件(*.bmp)|*.bmp|所有文件(*.*)|*.*||";
CFileDialog Dlg(TRUE,NULL,NULL,NULL,StrFilter,this);
if(!Dlg.DoModal()==IDOK)
return ;
CString StrFileName=Dlg.GetPathName();
CClientDC *pDC=new CClientDC(this);
CBitmap Bitmap;
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,StrFileName,
IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
Bitmap.Attach(hBitmap);
CDC DCCompatible;
DCCompatible.CreateCompatibleDC(pDC);
DCCompatible.SelectObject(&Bitmap);
BITMAP bmInfo;
Bitmap.GetObject(sizeof(bmInfo),&bmInfo);
pDC->BitBlt(0,0,bmInfo.bmWidth,bmInfo.bmHeight, &DCCompatible, 0,0,SRCCOPY);
StrFileName=Dlg.GetPathName();
CFile MyFile;
if(!MyFile.Open(StrFileName,CFile::modeRead))
return;
BITMAPFILEHEADER bmfHeader;
//读位图文件头信息
if (MyFile.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
return ;
//判断是否是BMP
if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
return ;
//读位图头信息
BITMAPINFOHEADER bmiHeader;
if (MyFile.Read((LPSTR)&bmiHeader, sizeof(bmiHeader)) !=sizeof(bmiHeader))
return ;
//获得大小信息,并显示
bmWidth=bmiHeader.biWidth;
bmHeight=bmiHeader.biHeight;
CString StrInfo;
StrInfo.Format("%s位图的大小为:%d X %d",StrFileName,bmiHeader.biWidth,bmiHeader.biHeight);
MessageBox(StrInfo,"信息提示",MB_OK);
}
void CBitmapView::OnMenuitem32772()
{
// TODO: Add your command handler code here
COLORREF MyColor;
CDC *pDC=GetDC();
int sum=0;
int i,j;
for(i=0;i<bmHeight;i++)
{
for(j=0;j<bmWidth;j++)
{
MyColor=pDC->GetPixel(j,i);
rgbp[i][j].r=GetRValue(MyColor);
rgbp[i][j].g=GetGValue(MyColor);
rgbp[i][j].b=GetBValue(MyColor);
}
}
for(i=0;i<bmHeight;i++)
{
for(j=0;j<bmWidth;j++)
{
if(rgbp[i][j].r==255 && rgbp[i][j].g==255 && rgbp[i][j].b==255)
data[i][j]=1;
if(rgbp[i][j].r==0 && rgbp[i][j].g==0 && rgbp[i][j].b==0)
data[i][j]=0;
}
}
CString StrFileName="C:\\MyNewFile.txt";
CFile MyFile;
if(!MyFile.Open(StrFileName,CFile::modeCreate|CFile::modeReadWrite,NULL))
{
MessageBox("打开文件操作失败","信息提示",MB_OK);
}
else
{ CString temp;
int flag;
long *tem=new long [80000000];//用来存放每个像素点
for(i=0;i<bmHeight;i++)
{
for(j=0;j<bmWidth;j++)
{ tem[sum]=data[i][j];
sum++;
if(sum%16==0)
{ if(sum%256==0)
MyFile.Write(".word ",6);
int k=sum-16;
flag=tem[k]*8+tem[k+1]*4+tem[k+2]*2+tem[k+3];
switch(flag)
{
case 0:temp="0";break;
case 1:temp="1";break;
case 2:temp="2";break;
case 3:temp="3";break;
case 4:temp="4";break;
case 5:temp="5";break;
case 6:temp="6";break;
case 7:temp="7";break;
case 8:temp="8";break;
case 9:temp="9";break;
case 10:temp="A";break;
case 11:temp="B";break;
case 12:temp="C";break;
case 13:temp="D";break;
case 14:temp="E";break;
case 15:temp="F";break;
}
if(temp=="A")MyFile.Write("0",1);
if(temp=="B")MyFile.Write("0",1);
if(temp=="C")MyFile.Write("0",1);
if(temp=="D")MyFile.Write("0",1);
if(temp=="E")MyFile.Write("0",1);
if(temp=="F")MyFile.Write("0",1);
MyFile.Write(temp,1);
flag=tem[k+4]*8+tem[k+5]*4+tem[k+6]*2+tem[k+7];
switch(flag)
{
case 0:temp="0";break;
case 1:temp="1";break;
case 2:temp="2";break;
case 3:temp="3";break;
case 4:temp="4";break;
case 5:temp="5";break;
case 6:temp="6";break;
case 7:temp="7";break;
case 8:temp="8";break;
case 9:temp="9";break;
case 10:temp="A";break;
case 11:temp="B";break;
case 12:temp="C";break;
case 13:temp="D";break;
case 14:temp="E";break;
case 15:temp="F";break;
}
MyFile.Write(temp,1);
flag=tem[k+8]*8+tem[k+9]*4+tem[k+10]*2+tem[k+11];
switch(flag)
{
case 0:temp="0";break;
case 1:temp="1";break;
case 2:temp="2";break;
case 3:temp="3";break;
case 4:temp="4";break;
case 5:temp="5";break;
case 6:temp="6";break;
case 7:temp="7";break;
case 8:temp="8";break;
case 9:temp="9";break;
case 10:temp="A";break;
case 11:temp="B";break;
case 12:temp="C";break;
case 13:temp="D";break;
case 14:temp="E";break;
case 15:temp="F";break;
}
MyFile.Write(temp,1);
flag=tem[k+12]*8+tem[k+13]*4+tem[k+14]*2+tem[k+15];
switch(flag)
{
case 0:temp="0";break;
case 1:temp="1";break;
case 2:temp="2";break;
case 3:temp="3";break;
case 4:temp="4";break;
case 5:temp="5";break;
case 6:temp="6";break;
case 7:temp="7";break;
case 8:temp="8";break;
case 9:temp="9";break;
case 10:temp="A";break;
case 11:temp="B";break;
case 12:temp="C";break;
case 13:temp="D";break;
case 14:temp="E";break;
case 15:temp="F";break;
}
MyFile.Write(temp,1);
MyFile.Write("H",1);
if(sum%256==0)
MyFile.Write("\n",1);
else
MyFile.Write(",",1);
//MyFile.Write(".word ",6);
}
}
}
MyFile.Close();
MessageBox("写C:\\MyNewFile.txt文件操作完成!","信息提示",MB_OK);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -