首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从TBitMap到PBitMap KOL

从TBitMap到PBitMap KOL
EN

Stack Overflow用户
提问于 2012-06-23 23:15:14
回答 1查看 233关注 0票数 1

我想在KOL中将TBitMap转换为PBitMap。

我试过了,但我得到一张黑色图片作为输出:

代码语言:javascript
复制
function TbitMapToPBitMap (bitmap : TBitMap) : PbitMap;
begin
 result := NIL;
 if Assigned(bitmap) then begin
  result := NewBitmap(bitmap.Width, bitmap.Height);
  result.Draw(bitmap.Canvas.Handle, bitmap.Width, bitmap.Height);
 end;
end;

知道它出什么问题了吗?我正在使用Delphi7。

谢谢你的帮助。

编辑:新代码:

代码语言:javascript
复制
function TbitMapToPBitMap (const src : TBitMap; var dest : PBitMap) : Bool; 
begin
 result := false;
 if (( Assigned(src) ) and ( Assigned (dest) )) then begin
 dest.Draw(src.Canvas.Handle, src.Width, src.Height);
 result := true;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 TBitMapTest : TBitMap;
 PBitMapTest : PBitMap;
begin
 TBitMapTest := TBitMap.Create;
 TBitMapTest.LoadFromFile ('C:\test.bmp');
 PBitMapTest := NewBitMap (TBitMapTest.Width, TBitMapTest.Height);
 TbitMapToPBitMap (TBitMapTest, PBitMapTest);
 PBitMapTest.SaveToFile ('C:\test2.bmp');
 PBitMapTest.Free;
 TBitMapTest.Free;
end;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-24 00:36:14

为了回答您的问题,为什么目标图像是黑色的;这是因为您将这些目标图像绘制为源图像,而它们是黑色的,因为NewBitmap将图像初始化为黑色。

如果你想要一个TBitmap到KOL PBitmap,如何复制或转换我发现只有一种方法(也许我在KOL中错过了这样的函数,但即使是这样,下面代码中使用的方法也是非常有效的)。您可以使用Windows GDI函数进行位块传输,即BitBlt,它只是将指定区域从一个画布复制到另一个画布。

下面的代码,当您单击按钮时,创建VCL和KOL位图实例,将图像加载到VCL位图,调用VCL到KOL位图复制函数,如果此函数成功,则将KOL位图绘制到表单画布并释放这两个位图实例:

代码语言:javascript
复制
uses
  Graphics, KOL;

function CopyBitmapToKOL(Source: Graphics.TBitmap; Target: PBitmap): Boolean;
begin
  Result := False;
  if Assigned(Source) and Assigned(Target) then
  begin
    Result := BitBlt(Target.Canvas.Handle, 0, 0, Source.Width, Source.Height,
      Source.Canvas.Handle, 0, 0, SRCCOPY);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  KOLBitmap: PBitmap;
  VCLBitmap: Graphics.TBitmap;
begin
  VCLBitmap := Graphics.TBitmap.Create;
  try
    VCLBitmap.LoadFromFile('d:\CGLIn.bmp');
    KOLBitmap := NewBitmap(VCLBitmap.Width, VCLBitmap.Height);
    try
      if CopyBitmapToKOL(VCLBitmap, KOLBitmap) then
        KOLBitmap.Draw(Canvas.Handle, 0, 0);
    finally
      KOLBitmap.Free;
    end;
  finally
    VCLBitmap.Free;
  end;
end;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11170595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档