简体   繁体   中英

GDI+ drawing on a TBitmap

In addition to drawing GDI+ onto a control canvas via TGPGraphics (which has been working fine), I'm also trying to draw onto a TBitmap using GDI+ as well, and then drawing that bitmap to the control canvas. However, nothing actually appears to get drawn.

The following code is within the WM_PAINT message handler, which again works for the actual control canvas, but when creating an equivalent TGPGraphics object and passing this TBitmap handle, nothing gets drawn:

  FBitmapCanvas:= CreateGPCanvas(FBitmap.Handle);
  try
    FBitmapCanvas.DrawLine(FSomePen, P1, P2); //Same pen used to successfully draw to control canvas
  finally
    FreeAndNil(FBitmapCanvas);
  end;
  Canvas.Draw(0, 0, FBitmap); //Draw this bitmap to control canvas

CreateGPCanvas looks like so, and is used for both this bitmap and the control:

function CreateGPCanvas(const DC: HDC): TGPGraphics;
begin
  Result:= TGPGraphics.Create(DC);
  Result.SetInterpolationMode(InterpolationMode.InterpolationModeHighQuality);
  Result.SetSmoothingMode(SmoothingMode.SmoothingModeHighQuality);
  Result.SetCompositingQuality(CompositingQuality.CompositingQualityHighQuality);
end;

On the other hand, if I don't try to use the TGPGraphics and instead draw a line directly via the TBitmap.Canvas property, it works fine (but of course looks ugly because it's not GDI+). So I know the actual bitmap gets drawn correctly to the control canvas.

FBitmap.Canvas.MoveTo(P1.X, P1.Y);
FBitmap.Canvas.LineTo(P2.X, P2.Y);

What am I doing wrong here, and how do I make the TGPGraphics work on this bitmap canvas?


PS - The only reason I'm using a TBitmap at all is because what I'm actually writing needs to "remember" a portion of what was previously drawn and retain it, rather than repainting it over and over.

Just figured out the problem, and it was a silly mistake.

When creating a TGPGraphics object, instead of passing FBitmap.Handle , it should rather be FBitmap.Canvas.Handle .

You need the handle of the bitmap's canvas, not of the bitmap itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM