如何在Delphi中使用CopyRect方法 [英] How to use CopyRect method in Delphi

查看:782
本文介绍了如何在Delphi中使用CopyRect方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从磁盘加载图像,并将其复制(部分)到第二个TImage:

  Image1 .Picture.LoadFromFile(S); 
with Image1.Picture.Bitmap do
Image2.Canvas.CopyRect(Image2.Canvas.ClipRect,Canvas,Canvas.ClipRect);

Image2只显示一个白色的矩形,而Image1则不显示文件从磁盘。如果我删除第二个语句,Image1会显示图像。 (最奇怪的是:如果我只注释掉CopyRect语句,并留下with行(空语句)Image1不显示任何一个!)



如何使用CopyRect复制图像的一部分?



编辑

当我将两个语句分割成两个单独的动作(按钮)发生以下情况:


  1. Image1中的图像加载和显示

  2. Image1消失(!), Image2显示一个白色矩形。

BTW,我使用的是Delphi 2009。

解决方案

TCanvas.CopyRect 使用 StretchBlt 复制矩形。 StretchBlt需要一个位图。如果您正在将任何其他图形类型加载到您的图像中,那么其 Picture.Bitmap 为空。实际上,当您引用该位图时,会创建位图: with Image1.Picture.Bitmap do



你可以使用临时位图的原因:

  var 
Bmp:TBitmap;
begin
Image1.Picture.LoadFromFile(S);

Bmp:= TBitmap.Create;
try
Bmp.Assign(Image1.Picture.Graphic);

与Bmp do
Image2.Canvas.CopyRect(Image2.Canvas.ClipRect,Canvas,Canvas.ClipRect);
finally
Bmp.Free;
..


I'm loading an image from disk and want to copy (part of) it to a second TImage:

Image1.Picture.LoadFromFile(S);
with Image1.Picture.Bitmap do
  Image2.Canvas.CopyRect(Image2.Canvas.ClipRect, Canvas, Canvas.ClipRect);

Image2 just shows a white rectangle, and Image1 doesn't show the file from disk. If I remove the second statement Image1 does show the image. (Strangest thing: if I only comment out the CopyRect statement and leave the "with" line (empty statement) Image1 doesn't show either!)

How do I use CopyRect to copy part of an image?

edit
When I split the two statements into two separate actions (buttons) the following happens:

  1. Image loads and shows in Image1
  2. Image1 disappears(!), and Image2 shows a white rectangle.

BTW, I'm using Delphi 2009.

解决方案

TCanvas.CopyRect copies the rectangle by using StretchBlt. StretchBlt requires a bitmap. If you're loading any other graphic type to your image then its Picture.Bitmap is empty. In fact the bitmap gets created just when you refer to it: with Image1.Picture.Bitmap do.

You can use a temporary bitmap for the cause:

var
  Bmp: TBitmap;
begin
  Image1.Picture.LoadFromFile(S);

  Bmp := TBitmap.Create;
  try
    Bmp.Assign(Image1.Picture.Graphic);

    with Bmp do
      Image2.Canvas.CopyRect(Image2.Canvas.ClipRect, Canvas, Canvas.ClipRect);
  finally
    Bmp.Free;
  ..

这篇关于如何在Delphi中使用CopyRect方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆