delphi - 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.
解决方案
刚刚发现问题,这是一个愚蠢的错误。
创建TGPGraphics
对象时,与其传递FBitmap.Handle
,不如传递FBitmap.Canvas.Handle
。
您需要位图画布的句柄,而不是位图本身的句柄。
推荐阅读
- php - woocommerce结账时对城市下拉菜单的限制
- javascript - Javascript - 使用 matchAll() 获取第一个 RegExp 匹配
- google-drive-api - 如何从 Google Drive API 按名称下载文件?
- python - 循环遍历多个字典以从 python 中的值创建新字典
- sql - Sqlite3 中 UNION 的 SUM( ) 的反直觉行为
- reactjs - React 路由器仅显示没有在我的浏览器中定义的组件的路由
- linux - 使用 SHIFT+ARROW(左或右)的 Ubuntu 终端上的文本选择不起作用
- sql - 递归获取 Postgres 表中树的根 ID
- javascript - 如何判断是否有 href 属性?
- javascript - 内容脚本无法在所有网页上正常运行