注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

mie

 
 
 

日志

 
 

GDIPlus ObjectBusy  

2010-08-19 14:21:25|  分类: vc界面编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

   Now, i meet with a knotty problem.  When I call the GDIPlus::Graphics::DrawImage function, sometimes it return failed, the value is "ObjectBusy".

   Just as MSDN, the "ObjectBusy" means that: "Indicates that one of the arguments specified in the API call is already in use in another thread. "

   But I have only used the object in one thread! how can I do?  some of my source code :

 if (m_main.GetHwnd()!=drawer->GetHwnd())
 {
  CSimpleImage bm_temp;
  bm_temp.FromHBitmap(m_main.GetCacheTempBitmap(), NULL);

  CRect temp_rect=dst_rect;
  m_main.GetRelativeRect(&temp_rect);

  status=m_cache_dc.graphics->DrawImage(
   bm_temp,
   Rect(dst_rect.left,
   dst_rect.top,
   dst_rect.right-dst_rect.left,
   dst_rect.bottom-dst_rect.top),
   temp_rect.left,
   temp_rect.top,
   temp_rect.right-temp_rect.left,
   temp_rect.bottom-temp_rect.top,
   UnitPixel,
   NULL,
   NULL,
   NULL
   );

  g_SaveDCToFile(m_cache_dc.graphics->GetHDC(), _T("C:\\1.bmp"),dst_rect.right,dst_rect.bottom);

 }

   If it only run once, everything in order!

   If it run twice, the second drawing will be failed, and it return "ObjectBusy", such as:

 if (m_main.GetHwnd()!=drawer->GetHwnd())
 {
  CSimpleImage bm_temp;
  bm_temp.FromHBitmap(m_main.GetCacheTempBitmap(), NULL);

  CRect temp_rect=dst_rect;
  m_main.GetRelativeRect(&temp_rect);

  status=m_cache_dc.graphics->DrawImage(
   bm_temp,
   Rect(dst_rect.left,
   dst_rect.top,
   dst_rect.right-dst_rect.left,
   dst_rect.bottom-dst_rect.top),
   temp_rect.left,
   temp_rect.top,
   temp_rect.right-temp_rect.left,
   temp_rect.bottom-temp_rect.top,
   UnitPixel,
   NULL,
   NULL,
   NULL
   );

  g_SaveDCToFile(m_cache_dc.graphics->GetHDC(), _T("C:\\1.bmp"),dst_rect.right,dst_rect.bottom);

 }

 if (m_main.GetHwnd()!=drawer->GetHwnd())
 {
  CSimpleImage bm_temp;
  bm_temp.FromHBitmap(m_main.GetCacheTempBitmap(), NULL);

  CRect temp_rect=dst_rect;
  m_main.GetRelativeRect(&temp_rect);

  status=m_cache_dc.graphics->DrawImage(
   bm_temp,
   Rect(dst_rect.left,
   dst_rect.top,
   dst_rect.right-dst_rect.left,
   dst_rect.bottom-dst_rect.top),
   temp_rect.left,
   temp_rect.top,
   temp_rect.right-temp_rect.left,
   temp_rect.bottom-temp_rect.top,
   UnitPixel,
   NULL,
   NULL,
   NULL
   );

  g_SaveDCToFile(m_cache_dc.graphics->GetHDC(), _T("C:\\1.bmp"),dst_rect.right,dst_rect.bottom);

 }

 

   also, if i draw one picture 'A', and then i draw the second picture 'B', it will also be failed .  I don't know why, it's too incredible.

 

I found that, if I perform the code above, then it must be failed, no matter that i call other code. such as:

  status=m_cache_dc.graphics->DrawImage(
   bm_temp,

status=m_cache_dc.graphics->FillRectangle

 

 then, it failed again. 

 

I think, the problem is :

CSimpleImage bm_temp;
  bm_temp.FromHBitmap(m_main.GetCacheTempBitmap(), NULL);

  CRect temp_rect=dst_rect;
  m_main.GetRelativeRect(&temp_rect);

  status=m_cache_dc.graphics->DrawImage(

 

But, it perform Ok when i use it elsewhere.

 

My God! it because that i used   g_SaveDCToFile(m_cache_dc.graphics->GetHDC(), _T("C:\\1.bmp"),dst_rect.right,dst_rect.bottom);

 

居然是因为使用了这句代码 g_SaveDCToFile,实在是想不明白,这句代码会有问题?


int g_SaveDCToFile(HDC hdc, LPCTSTR name, int width, int height)
{
 HBITMAP bm;
 HDC hdcBitmap=CreateCompatibleDC(NULL);
 if (hdcBitmap==NULL)
 {
  return -1;
 }

 bm=CreateCompatibleBitmap(hdc, width, height);
 if (bm==NULL)
 {
  DeleteDC(hdcBitmap);

  return -1;
 }

 HBITMAP holdBitmap=(HBITMAP)::SelectObject(hdcBitmap, bm);
 if (holdBitmap==NULL)
 {
  DeleteDC(hdcBitmap);
  DeleteObject(bm);

  return -1;
 }

 BitBlt(hdcBitmap, 0, 0, width, height, hdc, 0, 0, SRCCOPY);

 ::SelectObject(hdcBitmap, holdBitmap);

 DEBUG_SAVE_BITMAP(bm, name);

 DeleteObject(bm);
 DeleteObject(holdBitmap);
 DeleteDC(hdcBitmap);


 return 0;
}

这样的代码怎么存在线程的问题呢!hdc怎么就变成了对象繁忙了!

 

去掉,OK!

 

By:zhanyonhu

  评论这张
 
阅读(1006)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016