核心对象:
CImage m_imageFile;
绘制图片:
void CFigureView::OnDraw(CDC* pDC)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
CFigureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
![](/Images/OutliningIndicators/InBlock.gif)
// TODO: 在此处为本机数据添加绘制代码
CBrush BackBrush;
BackBrush.CreateSolidBrush(RGB(255,255,255));
CBrush* pOldBrush = pDC->SelectObject(&BackBrush);
CRect rect;
this->GetClientRect(&rect);
pDC->Rectangle(rect);//CRect(-1,-1,3000,3000));
pDC->SelectObject(pOldBrush);
if (!m_imageFile.IsNull())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//图片不为空
m_imageFile.StretchBlt(pDC->m_hDC,CRect(&m_rectShow),SRCCOPY);//复制图片到显示设备
}
}
![](/Images/OutliningIndicators/None.gif)
打开图片:
void CFigureView::OnFileOpen()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{//打开图片文件
// TODO: 在此添加命令处理程序代码
CString strFilter;
CString strImageFileName;
CSimpleArray<GUID> aguidFileTypes;
HRESULT hResult;
hResult = m_imageFile.GetExporterFilterString(strFilter,aguidFileTypes);
if(FAILED(hResult))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox("装入文件类型过滤器操作失败","消息提示",MB_OK);
return;
}
strFilter = "All File(*.*)|*.*|"+strFilter;
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,strFilter);
hResult = (int)dlg.DoModal();
if(hResult != IDOK)
return;
strImageFileName.Format(dlg.GetFileName());
m_imageFile.Destroy();
hResult = m_imageFile.Load(strImageFileName);
if(FAILED(hResult))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox("装入图像文件操作失败","消息提示",MB_OK);
return;
}
m_rectShow = CRect(0,0,m_imageFile.GetWidth(),m_imageFile.GetHeight());//显示区域
SetScrollSizes(MM_TEXT,CSize(m_rectShow.Width(),m_rectShow.Height()));
CWnd* pWnd=AfxGetMainWnd();
pWnd->SetWindowTextA("当前正在打开的文件名称为:"+strImageFileName);
Invalidate();//刷新
}
![](/Images/OutliningIndicators/None.gif)
进行黑白化处理:
void CFigureView::MakeBlackWhiteImage(CImage& pImage, int iType)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{//黑白化
CWaitCursor WaitCursor;
int Height = pImage.GetHeight();//高度
int Width = pImage.GetWidth();//宽度
if(!pImage.IsIndexed())//Indicates that a bitmap's colors are mapped to an indexed palette
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//没有使用调色板
for(int x=0; x<Width; x++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int y=0; y<Height;y++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
COLORREF pixel=pImage.GetPixel(x,y);//Retrieves the color of the pixel at the location specified by x and y.
byte r,g,b,Result;
r = GetRValue(pixel);
g = GetGValue(pixel);
b = GetBValue(pixel);
switch(iType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 0:
Result = ((r+g+b)/3);
break;
case 1:
Result = max(max(r,g),b);
break;
case 2:
Result = (2.7*r+0.2*g+0.1*b);
break;
}
pImage.SetPixel(x,y,RGB(Result,Result,Result));
}
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{//使用调色板
int MaxColors = pImage.GetMaxColorTableEntries();//Retrieves the maximum number of entries in the color table
RGBQUAD* ColorTable = new RGBQUAD[MaxColors];
//Retrieves red, green, blue (RGB) color values from a range of entries in the palette of the DIB section
pImage.GetColorTable(0,MaxColors,ColorTable);
for(int i=0;i<MaxColors;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
byte r,g,b,Result;
r = ColorTable[i].rgbRed;
g = ColorTable[i].rgbGreen;
b = ColorTable[i].rgbBlue;
switch(iType)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 0:
Result = ((r+g+b)/3);
break;
case 1:
Result = max(max(r,g),b);
break;
case 2:
Result = (2.7*r+0.2*g+0.1*b);
break;
}
ColorTable[i].rgbRed = Result;
ColorTable[i].rgbGreen = Result;
ColorTable[i].rgbBlue = Result;
}
pImage.SetColorTable(0,MaxColors,ColorTable);
delete ColorTable;
}
}
![](/Images/OutliningIndicators/None.gif)
void CFigureView::OnPsBw()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{//图片黑白化
// TODO: 在此添加命令处理程序代码
MakeBlackWhiteImage(m_imageFile,0);
![](/Images/OutliningIndicators/InBlock.gif)
CFigureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
pDoc->SetModifiedFlag(TRUE);//设置修改标志
Invalidate();
}
![](/Images/OutliningIndicators/None.gif)
处理效果: