StretchBlt不起作用

时间:2011-07-21 09:12:04

标签: .net windows winapi api

protected override void OnPaint(PaintEventArgs e)
{
      Win32Helper.StretchBlt(this.Handle, 0, 0, 200, 300,bitmap.GetHbitmap(), 0, 0, bitmap.Width, bitmap.Height, Win32Helper.TernaryRasterOperations.SRCCOPY);
      this.CreateGraphics().DrawRectangle(new Pen(Color.Black), 0, 0, 100, 100);           

    base.OnPaint(e);
}

绘制了矩形..但是位图不是......我设置了picturebox1.Image=bitmap并且这样做位图不是空的...任何想法我做错了什么? 我是紧凑的框架。

1 个答案:

答案 0 :(得分:1)

我不确定“this.Handle”是什么,但它可能不是DC的句柄。我怀疑你每次创建Pen和Graphics对象时都会泄漏资源。 (垃圾收集器最终会释放它,但是让这些句柄留在周围并不是一个好主意)。在任何情况下,您可以使用Graphics对象本身来执行图像blit,而不是将其转换为StretchBlt。

  protected override void OnPaint(PaintEventArgs e)
  {
      System.Drawing.Graphics g = e.Graphics; // or call your CreateGraphics function
      Pen p = new Pen(Color.Black);

      g.DrawImage(bitmap, 0, 0, 200, 300);
      g.DrawRectangle(p, 0, 0, 100, 100);           

      // cleanup
      p.Dispose();
      // g.Dispose(); Call g.dispose if you allocated it and it didn't come from the PaintEventArgs parameter

      base.OnPaint(e);
  }