首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#透明位图方法求解,=-=!解决方案

2012-01-05 
c#透明位图方法求解,-!!!!!!public static boolDrawTransparentBmp(IntPtr hdcDest,int nXOriginDest,in

c#透明位图方法求解,=-=!!!!!!
public static bool DrawTransparentBmp(IntPtr hdcDest,
  int nXOriginDest,
  int nYOriginDest,
  int nWidthDest,
  int nHeightDest,
  IntPtr hdcSrc,
  int nXOriginSrc,
  int nYOriginSrc,
  int nWidthSrc,
  int nHeightSrc, 
  uint crTransparent)  

{

  bool isture1=false;
  bool isture2 = false;
  bool isture3 = false;
  bool isture4 = false;
  bool isture5 = false;
  bool isture6 = false;

  if (nWidthDest < 1) return false;  
  if (nWidthSrc < 1) return false;  
  if (nHeightDest < 1) return false;  
  if (nHeightSrc < 1) return false;

  IntPtr hMaskDC,hImageDC;
  IntPtr hMaskBitmap,hOldMaskBitmap;
  IntPtr hImageBitmap,hOldImageBitmap;

  //创建mask DC 和临时存源文件的 DC

  hMaskDC = CreateCompatibleDC(hdcDest);

  hImageDC = CreateCompatibleDC(hdcDest);

  //创建mask 位图和image 兼容位图

  hMaskBitmap = CreateBitmap(nWidthSrc,nHeightSrc,1,1,null); //单色位图

  hImageBitmap = CreateCompatibleBitmap(hImageDC,nWidthSrc,nHeightSrc); //彩色位图

  //将位图选进DC中

  hOldImageBitmap = SelectObject(hImageDC,hImageBitmap);

  hOldMaskBitmap = SelectObject(hMaskDC,hMaskBitmap);

  //将src位图拷入临时的image DC中

  if (nWidthDest == nWidthSrc && nHeightDest == nHeightSrc)

  isture1 = BitBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
   
  else
  isture2 = StretchBlt(hImageDC, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
 
  //设置整个需要透明的颜色
  SetBkColor(hImageDC,crTransparent);

  //此步生成掩码位图(mask bitmap)
  //整个mask 位图中透明色的地方是白色,其他为黑色
  isture3 = BitBlt(hMaskDC, nXOriginDest, nYOriginDest, nWidthSrc, nHeightSrc, hImageDC, nXOriginSrc, nYOriginSrc, SRCCOPY);

  SetBkColor(hdcDest,RGB(Color.White));
  SetTextColor(hdcDest,RGB(Color.Black));

  isture4 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT);

  isture5 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hMaskDC, nXOriginSrc, nYOriginSrc, SRCAND);

  isture6 = BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthSrc, nHeightDest, hImageDC, nXOriginSrc, nYOriginSrc, SRCINVERT);

  //BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthSrc,nHeightDest,hMaskDC,0,0,SRCAND);

  //BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthSrc,nHeightDest,hImageDC,0,0,SRCPAINT);
   
  return true;  





然后在private void button1_Click(object sender, EventArgs e)
  {
  bool x;
   

  pictureBox3.Image = img;


   
  x=DrawTransparentBmp(pictureBox3.Handle,0,0,pictureBox3.Image.Width,pictureBox3.Image.Height,pictureBox2.Handle,0,0,pictureBox2.Image.Width,pictureBox2.Image.Height,0x12345);
   
   

  }

调用方法。
在方法DrawTransparentBmp 中。hMaskDC,hImageDC一开始就为0,应该是我实参传递有问题。请教各位了。还有就是bitblt也有问题,只有istrue3是true,其余全是false。。。。。。。
我受伤了。。。。。。。。。。。



[解决办法]
你可能是把 api 声明错了吧!
你不把你的api声明给大家怎么给你测试?

还有你这个函数其实整个过程 BitBlt StretchBlt 可能需要声明为api(为啥说可能那因为有可能根本不需要api的,没有样例程序不好说)
其他.net都可以做的;

热点排行