Delphi 7,如何将Paintbox内容复制到Tbitmap?

发布于 2024-12-27 17:30:07 字数 1269 浏览 2 评论 0原文

我正在使用 delphi 7,我想如何将 TpaintBox 的内容复制/分配给 Tbitmap?

像这样

 public
  { Public declarations }
   BitMap     : TBitmap;
 end;

我有一个Tbitmap声明为公共,我像这样在FormCreate上创建它

      procedure TForm1.FormCreate(Sender: TObject);
      begin
      BitMap     := TBitMap.Create;
      end;

然后我

      procedure TForm1.DrawOnPainBox;
        begin
         If BitMap.Width  <> PaintBox1.Width  then BitMap.Width  := PaintBox1.Width;
         If BitMap.Height <> PaintBox1.Height then BitMap.Height := PaintBox1.Height;
         BitMap.Canvas.Rectangle(0,0,random(PaintBox1.Width ),random(PaintBox1.Height));
         PaintBox1.Canvas.Draw(0,0,BitMap);
        end;

PaintBox1.Canvas.Draw(0,0,BitMap);在位图上绘制一些东西,我们可以显示什么位图中有油漆盒,但相反的方式是什么?

如何将绘画盒的内容分配/复制到位图?

 `BitMap:=PaintBox1.Canvas.Brush.Bitmap;` 

这个编译,但如果我这样做并再次调用过程TForm1.DrawOnPainBox;我得到访问冲突并且调试器显示位图PaintBox1.Canvas.Brush.Bitmap 即使在paintBox上绘制了一些线条

输入图像此处描述

此处输入图像描述

im working on delphi 7 and i want to how to copy/assign the content of a TpaintBox to a Tbitmap?

like this

 public
  { Public declarations }
   BitMap     : TBitmap;
 end;

i have a Tbitmap declared as public and i create it onFormCreate like this

      procedure TForm1.FormCreate(Sender: TObject);
      begin
      BitMap     := TBitMap.Create;
      end;

Then i draw somthing on the bitmap like this

      procedure TForm1.DrawOnPainBox;
        begin
         If BitMap.Width  <> PaintBox1.Width  then BitMap.Width  := PaintBox1.Width;
         If BitMap.Height <> PaintBox1.Height then BitMap.Height := PaintBox1.Height;
         BitMap.Canvas.Rectangle(0,0,random(PaintBox1.Width ),random(PaintBox1.Height));
         PaintBox1.Canvas.Draw(0,0,BitMap);
        end;

with PaintBox1.Canvas.Draw(0,0,BitMap); we can display what is there in Bitmap to a paintbox but what is the reverse way?

how to assign/copy content of a paintbox to a bitmap?

 `BitMap:=PaintBox1.Canvas.Brush.Bitmap;` 

this compiles but if i do this and again call the procedure TForm1.DrawOnPainBox; i get access Violation and the debugger show the bitmap and PaintBox1.Canvas.Brush.Bitmap even though some lines are drawn on the paintBox

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2025-01-03 17:30:07

要将 TPaintBox(我们称之为 PaintBox1)的内容分配给 TBitmap(例如 Bitmap), 注意

Bitmap.Width := PaintBox1.Width;
Bitmap.Height := PaintBox1.Height;
BitBlt(Bitmap.Canvas.Handle,
  0,
  0,
  Bitmap.Width,
  Bitmap.Height,
  PaintBox1.Canvas.Handle,
  0,
  0,
  SRCCOPY);

:在较新版本的 Delphi 中,您可以使用 Bitmap.SetSize 代替 Bitmap.WidthBitmap.Height

To assign the contents of a TPaintBox (let's call it PaintBox1) to a TBitmap (Bitmap, say), you can do

Bitmap.Width := PaintBox1.Width;
Bitmap.Height := PaintBox1.Height;
BitBlt(Bitmap.Canvas.Handle,
  0,
  0,
  Bitmap.Width,
  Bitmap.Height,
  PaintBox1.Canvas.Handle,
  0,
  0,
  SRCCOPY);

Notice: In newer versions of Delphi, you can use Bitmap.SetSize instead of Bitmap.Width and Bitmap.Height.

鲸落 2025-01-03 17:30:07

TBitmap.setsize 已在 Delphi 2006 中引入,您可能使用的是旧版本。只需更换
Bitmap.SetSize (X, Y)

它的速度

Bitmap.Width := X
Bitmap.Height := Y

您将编译代码

较慢(但只有在循环中使用它时才重要),但如果这种情况发生得太频繁, ,声明一个新的单位 BitmapSize。 pas:

unit BitmapSize;

interface

uses
    graphics;

Type
    TBitmapSize = class (TBitmap)
    public
        procedure Setsize (X, Y : integer);
    end;



implementation

procedure TBitmapsize.Setsize(X, Y: integer);
begin
    Width := X;    // may need some more tests here (X > 0, Y > 0, ...)
    Height := Y;
end;

end.

然后用 TBitmapSize 替换位图 TBitmap 的声明和创建。

..
Var
  B : TBitmapSize;
..
  B := TBitmapSize.Create;

TBitmap.setsize has been introduced in Delphi 2006, you may be using an older version. Just replace
Bitmap.SetSize (X, Y)

by

Bitmap.Width := X
Bitmap.Height := Y

it's slower (but it matters only if you use it in a loop), but you will compile the code

if this happens too often, declare a new unit BitmapSize.pas:

unit BitmapSize;

interface

uses
    graphics;

Type
    TBitmapSize = class (TBitmap)
    public
        procedure Setsize (X, Y : integer);
    end;



implementation

procedure TBitmapsize.Setsize(X, Y: integer);
begin
    Width := X;    // may need some more tests here (X > 0, Y > 0, ...)
    Height := Y;
end;

end.

then replace in declaration and creation of your bitmap TBitmap with TBitmapSize.

..
Var
  B : TBitmapSize;
..
  B := TBitmapSize.Create;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文