我想做个反色的处理,在反色的消息处理函数中使用了setpixel
但是最大化后图片又变成了未经处理的模样,我知道是ondraw的原因,但是不是很懂。最好不要把setpixel放到ondraw里。希望大虾指点。
以下是我的消息处理函数的代码:
void CTest2View::OnRgbtogray()
{
int i,j,R,G,B;
COLORREF RGB;
CDC* pMybitmap=GetDC();
for(i=0;i<width;i++)
for(j=0;j<height;j++){
RGB=pMybitmap->GetPixel(i,j);
R=GetRValue(RGB);
G=GetGValue(RGB);
B=GetBValue(RGB);
R=255-R;
G=255-G;
B=255-B;
pMybitmap->SetPixelV(i,j,RGB(R,G,B));
} // TODO: Add your command handler code her
}ondraw里是这样的:
void CTest2View::OnDraw(CDC* pDC)
{
CTest2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CDC* pMybitmap=GetDC();
pMybitmap->BitBlt(0,0,width,height,&Mybitmapdc,0,0,SRCCOPY);
// TODO: add draw code for native data here
}
但是最大化后图片又变成了未经处理的模样,我知道是ondraw的原因,但是不是很懂。最好不要把setpixel放到ondraw里。希望大虾指点。
以下是我的消息处理函数的代码:
void CTest2View::OnRgbtogray()
{
int i,j,R,G,B;
COLORREF RGB;
CDC* pMybitmap=GetDC();
for(i=0;i<width;i++)
for(j=0;j<height;j++){
RGB=pMybitmap->GetPixel(i,j);
R=GetRValue(RGB);
G=GetGValue(RGB);
B=GetBValue(RGB);
R=255-R;
G=255-G;
B=255-B;
pMybitmap->SetPixelV(i,j,RGB(R,G,B));
} // TODO: Add your command handler code her
}ondraw里是这样的:
void CTest2View::OnDraw(CDC* pDC)
{
CTest2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CDC* pMybitmap=GetDC();
pMybitmap->BitBlt(0,0,width,height,&Mybitmapdc,0,0,SRCCOPY);
// TODO: add draw code for native data here
}
你可以建一个CompatibleDC,设置的时候设CompatibleDC,拷贝的时候拷CompatibleDC的内容
void CTest2View::OnRgbtogray()
{
int i,j,R,G,B;
COLORREF RGB;
CDC* pMybitmap=GetDC();
for(i=0;i<width;i++)
for(j=0;j<height;j++){
RGB=pMybitmap->GetPixel(i,j);
R=GetRValue(RGB);
G=GetGValue(RGB);
B=GetBValue(RGB);
R=255-R;
G=255-G;
B=255-B;
pMybitmap->SetPixelV(i,j,RGB(R,G,B));
}
Mybitmapdc.CreateCompatibleDC(pMybitmap);
Mybitmapdc.SelectObject(&Mybitmap);
BITMAP bminfo;
Mybitmap.GetObject(sizeof(bminfo),&bminfo);
// TODO: Add your command handler code her
}
编译没问题
但为什么运行时反色完毕会弹出错误对话框呢?
这里就用你创建的CompatibleDC设置,
pMybitmap->BitBlt(0,0,width,height,&Mybitmapdc,0,0,SRCCOPY);
这里也用你创建的CompatibleDC
能不能帮我修改下?不胜感激!!!