如果使用Delphi的TImage加载jpg图片或png图片,如果你修改canvas则会报错:
can only modify an image if it contains a bitmap?
那么你可以通过一个方式进行变通,然后就没有问题了,如何弄呢?
看代码吧:(够长了点)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, jpeg, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure MyDraw;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
MyDraw;
end;
procedure TForm1.MyDraw;
var
bf: BLENDFUNCTION;
desBmp, srcBmp: TBitmap;
rgn: HRGN;
begin
with bf do
begin
BlendOp := AC_SRC_OVER;
BlendFlags := 0;
AlphaFormat := 0;
SourceConstantAlpha := 180; // 透明度,0~255
end;
desBmp := TBitmap.Create;
srcBmp := TBitmap.Create;
try
srcBmp.Assign(image1.Picture.Graphic);
desBmp.Width := srcBmp.Width;
desBmp.Height := srcBmp.Height;
Windows.AlphaBlend(desBmp.Canvas.Handle, 0, 0,
desBmp.Width, desBmp.Height, srcBmp.Canvas.Handle,
0, 0, srcBmp.Width, srcBmp.Height, bf);
rgn := CreateEllipticRgn(200,0, 500, 400); // 创建一个圆形区域
SelectClipRgn(srcBmp.Canvas.Handle, rgn);
srcBmp.Canvas.Draw(0, 0, desBmp);
image1.Picture.Assign(nil);
image1.Picture.Bitmap.Assign(nil);
image1.Picture.Bitmap.Assign(srcBmp);
finally
desBmp.Free;
srcBmp.Free;
end
end;
procedure TForm1.FormCreate(Sender: TObject);
var
jp:TJPEGImage;
begin
jp:=TJPEGImage.Create;
jp.loadfromFile('1.jpg');
Image1.Picture.Assign(jp);
jp.Free;
DoubleBuffered := True;
end;
end.
|
请发表评论