日期:2014-05-18  浏览次数:21026 次

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)
&n