核心对象:
CImage m_imageFile;
绘制图片:
void CFigureView::OnDraw(CDC* pDC)


{
CFigureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

// 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())

{//图片不为空
m_imageFile.StretchBlt(pDC->m_hDC,CRect(&m_rectShow),SRCCOPY);//复制图片到显示设备
}
}

打开图片:
void CFigureView::OnFileOpen()


{//打开图片文件
// TODO: 在此添加命令处理程序代码
CString strFilter;
CString strImageFileName;
CSimpleArray<GUID> aguidFileTypes;
HRESULT hResult;
hResult = m_imageFile.GetExporterFilterString(strFilter,aguidFileTypes);
if(FAILED(hResult))

{
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))

{
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();//刷新
}

进行黑白化处理:
void CFigureView::MakeBlackWhiteImage(CImage& pImage, int iType)


{//黑白化
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

{//没有使用调色板
for(int x=0; x<Width; x++)

{
for(int y=0; y<Height;y++)

{
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)

{
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

{//使用调色板
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++)

{
byte r,g,b,Result;
r = ColorTable[i].rgbRed;
g = ColorTable[i].rgbGreen;
b = ColorTable[i].rgbBlue;
switch(iType)

{
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;
}
}

void CFigureView::OnPsBw()


{//图片黑白化
// TODO: 在此添加命令处理程序代码
MakeBlackWhiteImage(m_imageFile,0);

CFigureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
pDoc->SetModifiedFlag(TRUE);//设置修改标志
Invalidate();
}

处理效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述