StretchBlt 操作

问题描述 投票:0回答:1
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
并且可以工作,因此位图不为空...知道我做错了什么吗? 我处于紧凑的框架中。

.net windows winapi
1个回答
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);
  }
© www.soinside.com 2019 - 2024. All rights reserved.