void PG4::OnPaint(){
HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,_T("C:\\org.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
CBitmap bitmap; bitmap.Attach(hBitmap);
BITMAP bmp; bitmap.GetBitmap(&bmp);
CPaintDC dc(this);
CDC pCompatableDC; pCompatableDC.CreateCompatibleDC(&dc);
pCompatableDC.SelectObject(&bitmap);
CRect rect; GetClientRect(&rect);//选定绘图区域
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&pCompatableDC,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
CPen pen(PS_SOLID, 20, RGB(30,30,30));
HGDIOBJ hpt=dc.SelectObject(pen);
dc.MoveTo(10,10); dc.LineTo(100,300);
dc.SelectObject(hpt); pen.DeleteObject();
CFont font;
font.CreateFontW(30, 0,300,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,_T("arial") );
hpt=dc.SelectObject(font);
int bk=dc.SetBkMode(TRANSPARENT);
COLORREF clr=dc.SetTextColor(RGB(30,250,30));
dc.TextOutW(20,50,_T("hello,world"));
dc.SelectObject(hpt);
font.DeleteObject();
dc.SetBkMode(bk);
dc.SetTextColor(clr);
static bool first=true;
static bool first=true;
if (first){
CImage imag;
CRect rcClient;
GetClientRect(rcClient);
imag.Create( rcClient.Width(),rcClient.Height(),32);
BitBlt(imag.GetDC(), 0,0,rcClient.Width(),rcClient.Height(), dc.m_hDC,0,0,SRCCOPY );
HRESULT hResult = imag.Save( _T("d:\\new.bmp "));
if (FAILED(hResult))
MessageBox(_T("保存图像文件失败!"));
else
MessageBox(_T("保存图像文件成功!"));
imag.ReleaseDC();
first=false;
}
ReleaseDC(&dc);
}
2 个解决方案
#1
我是这么用的
你可以参考一下
你可以参考一下
CRect rectPic, rectCrood;
CBitmap bitmapTemp, *pOldBitmap;
CImage imageSave;
CDC* pDC = GetDC();
CDC* pMemDC = new CDC;
rectPic.SetRect(0, 0, dlgSaveWave.m_nWidth, dlgSaveWave.m_nHight);
rectCrood.SetRect(30, 30, dlgSaveWave.m_nWidth-30, dlgSaveWave.m_nHight-30);
//创建位图内存
bitmapTemp.CreateCompatibleBitmap(pDC, dlgSaveWave.m_nWidth, dlgSaveWave.m_nHight);
pMemDC->CreateCompatibleDC(pDC);
pOldBitmap = pMemDC->SelectObject(&bitmapTemp);
pMemDC->FillSolidRect(rectPic, GetSysColor(COLOR_BTNFACE));
switch(CShareRes::m_nWitchModel)
{
case MODEL_SUPERVISE:m_pdlgSupervise->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
case MODEL_MINUTES:m_pdlgMinutes->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
case MODEL_PULSE:m_pdlgPulse->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
case MODEL_SYNTONY:m_pdlgSyntony->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
}
imageSave.Attach((HBITMAP)bitmapTemp.GetSafeHandle());
imageSave.Save(dlgSaveWave.m_strSaveFile);
pMemDC->SelectObject(pOldBitmap);
//释放内存
ReleaseDC(pMemDC);
delete pMemDC;
#2
搞定了,
将HRESULT hResult = imag.Save( _T("d:\\new.jpg ")); 改为
HRESULT hResult = imag.Save( _T("d:\\new.jpg")); 或者
HRESULT hResult = imag.Save( _T("d:\\new.jpg ",Gdiplus::ImageFormatJPEG ));
就可以保存了。
imag.Save中的文件名后面有空格,所以无法识别保存类型;如果指定保存类型Gdiplus::ImageFormatJPEG,则文件名可以有空格。
将HRESULT hResult = imag.Save( _T("d:\\new.jpg ")); 改为
HRESULT hResult = imag.Save( _T("d:\\new.jpg")); 或者
HRESULT hResult = imag.Save( _T("d:\\new.jpg ",Gdiplus::ImageFormatJPEG ));
就可以保存了。
imag.Save中的文件名后面有空格,所以无法识别保存类型;如果指定保存类型Gdiplus::ImageFormatJPEG,则文件名可以有空格。
#1
我是这么用的
你可以参考一下
你可以参考一下
CRect rectPic, rectCrood;
CBitmap bitmapTemp, *pOldBitmap;
CImage imageSave;
CDC* pDC = GetDC();
CDC* pMemDC = new CDC;
rectPic.SetRect(0, 0, dlgSaveWave.m_nWidth, dlgSaveWave.m_nHight);
rectCrood.SetRect(30, 30, dlgSaveWave.m_nWidth-30, dlgSaveWave.m_nHight-30);
//创建位图内存
bitmapTemp.CreateCompatibleBitmap(pDC, dlgSaveWave.m_nWidth, dlgSaveWave.m_nHight);
pMemDC->CreateCompatibleDC(pDC);
pOldBitmap = pMemDC->SelectObject(&bitmapTemp);
pMemDC->FillSolidRect(rectPic, GetSysColor(COLOR_BTNFACE));
switch(CShareRes::m_nWitchModel)
{
case MODEL_SUPERVISE:m_pdlgSupervise->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
case MODEL_MINUTES:m_pdlgMinutes->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
case MODEL_PULSE:m_pdlgPulse->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
case MODEL_SYNTONY:m_pdlgSyntony->DrawViewToDC(pMemDC, rectCrood, dlgSaveWave.m_nWhichView);break;
}
imageSave.Attach((HBITMAP)bitmapTemp.GetSafeHandle());
imageSave.Save(dlgSaveWave.m_strSaveFile);
pMemDC->SelectObject(pOldBitmap);
//释放内存
ReleaseDC(pMemDC);
delete pMemDC;
#2
搞定了,
将HRESULT hResult = imag.Save( _T("d:\\new.jpg ")); 改为
HRESULT hResult = imag.Save( _T("d:\\new.jpg")); 或者
HRESULT hResult = imag.Save( _T("d:\\new.jpg ",Gdiplus::ImageFormatJPEG ));
就可以保存了。
imag.Save中的文件名后面有空格,所以无法识别保存类型;如果指定保存类型Gdiplus::ImageFormatJPEG,则文件名可以有空格。
将HRESULT hResult = imag.Save( _T("d:\\new.jpg ")); 改为
HRESULT hResult = imag.Save( _T("d:\\new.jpg")); 或者
HRESULT hResult = imag.Save( _T("d:\\new.jpg ",Gdiplus::ImageFormatJPEG ));
就可以保存了。
imag.Save中的文件名后面有空格,所以无法识别保存类型;如果指定保存类型Gdiplus::ImageFormatJPEG,则文件名可以有空格。