我想在RxRichEdit里面添加图片,想让添加的图片如果大小超过了Rxrichedit 的大小,就把图片的Width和Height的设置成跟Rxrichedit大小一样,用的Timage,发现更大大小没什么用,下面的代码是声明为TPicture,代码如下:procedure TForm1.btn1Click(Sender: TObject);
var
aBitmap:TPicture;
aclipboard:TClipboard ;
begin
aBitmap:=Tpicture.Create ;
// aBitmap:=TImage.Create(Self);
aBitmap.LoadFromFile('D:\Startbild.bmp');
// aBitmap.LoadFromFile() ;
// aBitmap.Width :=RxRichEdit1.Width ;
//aBitmap.Height :=RxRichEdit1.Height ;
// aBitmap.AutoSize:=true ;
aclipboard:=TClipboard.Create ;
aclipboard.Assign(aBitmap);
RxRichEdit1.PasteFromClipboard ;end;如上所示,我该怎么改变添加图片的大小,以适应Rxrichedit的大小呢
var
aBitmap:TPicture;
aclipboard:TClipboard ;
begin
aBitmap:=Tpicture.Create ;
// aBitmap:=TImage.Create(Self);
aBitmap.LoadFromFile('D:\Startbild.bmp');
// aBitmap.LoadFromFile() ;
// aBitmap.Width :=RxRichEdit1.Width ;
//aBitmap.Height :=RxRichEdit1.Height ;
// aBitmap.AutoSize:=true ;
aclipboard:=TClipboard.Create ;
aclipboard.Assign(aBitmap);
RxRichEdit1.PasteFromClipboard ;end;如上所示,我该怎么改变添加图片的大小,以适应Rxrichedit的大小呢
解决方案 »
- @@@@@pString在以下情况是否需要是否内存?多谢!!@@@@@
- 国内正版的DELPHI7到底多少钱?
- 为什么用 SetLength 给动态数组设大小,编译出错
- 一个关于报表的问题
- TreeView按照数据库动态生成节点
- FastReport VS QuickReport (熟悉两种报表使用方法的请进)
- TBatchMove?
- TWebBrowser为什么有很多Navigate属性,有什么区别?
- 如何实现拨号网络通信?
- 如何使程序不被Ctrl+Alt+Del强行关闭?或只屏蔽Ctrl+Alt+Del?
- 请问哪里能找到非阻塞的wininet的DEMO?
- 失踪的cookies项,求大家帮忙找找
var Image:TImage;
aclipboard:TClipboard;
begin
Image:=TImage.Create(self);
try
Image.Picture.Bitmap.LoadFromFile('D:\Startbild.bmp');
Image.AutoSize:=true;
if (Image.Width>RxRichEdit1.Width)or(Image.Height>RxRichEdit1.Height) then begin
Image.AutoSize:=false;
Image.Stretch:=true;
Image.Width:=RxRichEdit1.Width;
Image.Height:=RxRichEdit1.Height;
end;
aclipboard:=TClipboard.Create ;
try
aclipboard.Assign(Image);
RxRichEdit1.PasteFromClipboard ;
finally
aclipboard.Free;
end;
finally
Image.Free;
end;
end;
var Image:TImage;
bmp:TBitmap;
aclipboard:TClipboard;
begin
Image:=TImage.Create(self);
try
Image.Picture.Bitmap.LoadFromFile('D:\Startbild.bmp');
Image.AutoSize:=true;
bmp:=TBitMap.Create;
try
if (Image.Width>RxRichEdit1.Width)or(Image.Height>RxRichEdit1.Height) then begin
bmp.Width:=RxRichEdit1.Width;
bmp.Height:=RxRichEdit1.Height ;
end
else begin
bmp.Width:=Image.Width;
bmp.Height:=Image.Height ;
end;
bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect,Image.Picture.Bitmap);
aclipboard:=TClipboard.Create ;
try
aclipboard.Assign(bmp);
RxRichEdit1.PasteFromClipboard ;
finally
aclipboard.Free;
end;
finally
bmp.Free;
end;
finally
Image.Free;
end;
end;
为除符合LZ要求外,还保证按原图的长宽比添加到RxRichEdit,可以这么做:
procedure TForm1.btn1Click(Sender: TObject);
var Image:TImage;
bmp:TBitmap;
aclipboard:TClipboard;
x,y,h:integer;
function GetBitmapSize(var x1,y1:integer;x2,y2:integer):boolean;
var k:Extended;
begin
Result:=false;
k:=x1 / y1; //原图的宽长比
if k<=0 then exit;
while x1>x2 do dec(x1);
if x1<1 then exit;
y1:=trunc(x1 / k);
while y1>y2 do dec(y1);
if y1<1 then exit;
x1:=trunc(y1 * k );
Result:=true;
end;
begin
if not OpenDialog1.Execute then exit;
Image:=TImage.Create(self);
try
Image.Picture.Bitmap.LoadFromFile(OpenDialog1.FileName);
Image.AutoSize:=true;
bmp:=TBitMap.Create;
try
x:=Image.Width;//图片原始宽度
y:=Image.Height;//图片原始高度
h:=0;
if RxRichEdit1.BorderStyle=bsSingle then h:=8; //有边框时,有效高度-8
if not GetBitmapSize(x,y,RxRichEdit1.Width,RxRichEdit1.Height-h) then exit;
//让待复制的图片小于、等于RxRichEdit1尺寸,并保持原有长宽比:
bmp.Width:=x;
bmp.Height:=y;
//将图片按自动拉伸方式画到bmp的画布上:
bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect,Image.Picture.Bitmap);
aclipboard:=TClipboard.Create ;
try
aclipboard.Assign(bmp);//复制到剪贴板
RxRichEdit1.PasteFromClipboard ;//粘贴到RxRichEdit1
finally
aclipboard.Free;
end;
finally
bmp.Free;
end;
finally
Image.Free;
end;
end;
bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect,Image.Picture.Graphic);这样BMP和JPG都可以插入,但是我当我插入PNG图片的时候,发现只能插入部分,会漏掉了一部分,我引用pngImage,不知道这个问题怎么解决呢?如果我把这个PNG图片插入到WORD里面,然后我直接复制WORD里面的图片通过Ctrl+V复制到rxRichedit,发现可以显示全部,好奇怪啊,不知道为什么
var Image:TImage;
bmp,ibmp:TBitmap;
aclipboard:TClipboard;
x,y,h:integer;
function GetImage(img: TImage): boolean;
var ms:TMemoryStream;
OpenDialog: TOpenDialog;
jpg:TJPEGImage;
Buffer:Word;
begin
Result:=false;
OpenDialog:=TOpenDialog.Create(self);
try
OpenDialog.Filter:='*.bmp;*.png;*.jpg|*.bmp;*.png;*.jpg';
if OpenDialog.Execute then
begin
Result:=false;
ms:=TMemoryStream.Create;
try
ms.LoadFromFile(OpenDialog.FileName);
if ms.Size>0 then begin
ms.Position := 0;
ms.ReadBuffer(Buffer,2);
if Buffer=$4D42 then begin //是BMP格式
img.Picture.Bitmap.LoadFromFile(OpenDialog.FileName);
Result:=true;
end
else if Buffer=$D8FF then begin //是JPG格式
jpg:=TJPEGImage.Create;
try
jpg.LoadFromFile(OpenDialog.FileName);
img.Picture.Bitmap.Assign(jpg);
Result:=true;
finally
jpg.Free;
end;
end
else if Buffer=$5089 then begin //PNG 格式
img.Picture.LoadFromFile(OpenDialog.FileName);
Result:=true;
end;
end;
finally
ms.Free;
end;
end;
finally
OpenDialog.Free;
end;
end;
function GetBitmapSize(var x1,y1:integer;x2,y2:integer):boolean;
var k:Extended;
begin
Result:=false;
k:=x1 / y1; //原图的宽长比
if k<=0 then exit;
if x1>x2 then x1:=x2;
if x1<1 then exit;
y1:=trunc(x1 / k);
if y1>y2 then y1:=y2;
if y1<1 then exit;
x1:=trunc(y1 * k );
Result:=true;
end;
begin
Image:=TImage.Create(self);
try
if not GetImage(Image) then raise Exception.Create('操作无效。');
Image.AutoSize:=true;
x:=Image.Width;//图片原始宽度
y:=Image.Height;//图片原始高度
ibmp:=TBitmap.Create;
try
ibmp.Width:=x;
ibmp.Height:=y;
ibmp.Canvas.StretchDraw(ibmp.Canvas.ClipRect,Image.Picture.Graphic);
bmp:=TBitMap.Create;
try
h:=0;
if RxRichEdit1.BorderStyle=bsSingle then h:=8; //有边框时,有效高度-8
if not GetBitmapSize(x,y,RxRichEdit1.Width,RxRichEdit1.Height-h) then exit;
//让待复制的图片小于、等于RxRichEdit1尺寸,并保持原有长宽比:
bmp.Width:=x;
bmp.Height:=y;
//将图片按自动拉伸方式画到bmp的画布上:
bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect,ibmp);
aclipboard:=TClipboard.Create ;
try
aclipboard.Assign(bmp);//复制到剪贴板
RxRichEdit1.PasteFromClipboard ;//粘贴到RxRichEdit1
finally
aclipboard.Free;
end;
finally
bmp.Free;
end;
finally
ibmp.Free;
end;
finally
Image.Free;
end;
end;
if not GetBitmapSize(x,y,RxRichEdit1.Width,RxRichEdit1.Height-h) then
raise Exception.Create('无效尺寸。');
var
ms:TMemoryStream ;
OpenDialog:TOpenDialog;
Jpg:TJPEGImage;
Buffer:Word ;
begin
Result:=false ;
OpenDialog:=TOpenDialog.Create(self);
try
OpenDialog.Filter :='*.bmp;*.png;*.jpg|*.bmp;*.png;*.jpg';
if OpenDialog.Execute then
begin
Result :=false ;
ms:=TMemoryStream.Create ;
try
ms.LoadFromFile(OpenDialog.FileName);
if ms.Size >0 then
begin
ms.Position:=0;
ms.ReadBuffer(Buffer,2);
if Buffer =$4D42 then //BMP¸ñʽ
begin
aImage.Picture.Bitmap.LoadFromFile(OpenDialog.FileName);
Result :=true ;
end
else if Buffer =$D8FF then //JPG¸ñʽ
begin
Jpg:=TjpegImage.Create ;
try
Jpg.LoadFromFile(OpenDialog.FileName);
aImage.Picture.Assign(Jpg);
Result :=true ;
finally
Jpg.Free ;
end;
end
else if Buffer =$5089 then //PNG¸ñʽ
begin
aImage.Picture.LoadFromFile(OpenDialog.FileName );
Result :=true ;
end; end;
finally
ms.Free ;
end;
end;
finally
OpenDialog.Free ;
end ;
end;procedure TForm1.RzButton1Click(Sender: TObject);
var
aImage:TImage;
bmp:TBitmap;
aClipboard:TClipboard ;
x,y,h:Integer ; function GetBitmapSize(var x_Width,y_height:Integer;Rich_width,Rich_Height:Integer):Boolean ;
var
K:Extended;
begin
Result :=false ;
K:=x_Width / y_height;//ÔͼµÄ¿í/¸ß ±È
if k<0 then Exit ;
if x_Width <1 then Exit ;
// while x_Width>Rich_width do Dec (x_Width ,1);
if x_Width >Rich_width then x_Width:=Rich_width ;
y_height:=Trunc(x_Width / k);
if y_height >Rich_Height then y_height :=Rich_Height ;
if y_height <1 then y_height :=0;
x_Width :=Trunc(y_height*k);
Result :=true ; end;
begin
aImage:=TImage.Create(Self);
try
if not GetImage(aImage) then raise Exception.Create('²Ù×÷ÎÞЧ!') ;
{ if not Opendlg1.Execute then exit ;
aImage.Picture.LoadFromFile(Opendlg1.FileName); }
aImage.AutoSize :=true ;
x:=aImage.Width ; //ͼƬÔʼ¿í¶È
y:=aImage.Height ; //ͼƬÔʼ¸ß¶È
h:=0;
bmp:=TBitmap.Create ;
// if RxRichEdit1.BorderStyle=bsSingle then h:=8;//Óб߿òʱ£¬ÓÐЧ¿í¶È-8
if not GetBitmapSize(x,y,RxRichEdit1.Width,RxRichEdit1.Height ) then exit ;
//±£³ÖÔÓÐͼƬ¿í¶È±È
bmp.Width :=x;
bmp.Height :=y;
bmp.Canvas.StretchDraw(bmp.Canvas.ClipRect,aImage.Picture.Graphic );
aClipboard:=TClipboard.Create ;
try
aClipboard.Assign(bmp);
RxRichEdit1.PasteFromClipboard ;
finally
aClipboard.Free ;
end;
finally
bmp.Free ;
end;
aImage.Free ;
end;如过用下面的方法,插入PNG,也是可以,我们常用的操作,不就是image直接插入图片吗,而不是要自己写代码去分析它属于哪个类型图片,我觉得是image自己会处理分析图片格式 //if not GetImage(aImage) then raise Exception.Create('²Ù×÷ÎÞЧ!') ;
if not Opendlg1.Execute then exit ;
aImage.Picture.LoadFromFile(Opendlg1.FileName);
然后你那个粘帖字适应大小怎么搞的呢?应该算是怎么样判断Ctrl+V这个过程,然后再判断粘帖的内容是不是图片吧?
if not Opendlg1.Execute then exit ;
aImage.Picture.LoadFromFile(Opendlg1.FileName);
难道我真的缺品.下面是将你的图片加入的截图,没你说的现象:
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, RxRichEd, ExtCtrls;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
procedure Button1Click(Sender: TObject);
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Clipbrd,pngimage,jpeg;
var Button1:TButton;
RxRichEdit1:TRxRichEdit;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var Image:TImage;
bmp,ibmp:TBitmap;
aclipboard:TClipboard;
x,y,h:integer;
function GetImage(img: TImage): boolean;
var ms:TMemoryStream;
OpenDialog: TOpenDialog;
jpg:TJPEGImage;
Buffer:Word;
begin
Result:=false;
OpenDialog:=TOpenDialog.Create(self);
try
OpenDialog.Filter:='*.bmp;*.png;*.jpg|*.bmp;*.png;*.jpg';
if OpenDialog.Execute then
begin
Result:=false;
ms:=TMemoryStream.Create;
try
ms.LoadFromFile(OpenDialog.FileName);
if ms.Size>0 then begin
ms.Position := 0;
ms.ReadBuffer(Buffer,2);
if Buffer=$4D42 then begin //是BMP格式
img.Picture.Bitmap.LoadFromFile(OpenDialog.FileName);
Result:=true;
end
else if Buffer=$D8FF then begin //是JPG格式
jpg:=TJPEGImage.Create;
try
jpg.LoadFromFile(OpenDialog.FileName);
img.Picture.Bitmap.Assign(jpg);
Result:=true;
finally
jpg.Free;
end;
end
else if Buffer=$5089 then begin //PNG 格式
img.Picture.LoadFromFile(OpenDialog.FileName);
Result:=true;
end;
end;
finally
ms.Free;
end;
end;
finally
OpenDialog.Free;
end;
end;
function GetBitmapSize(var x1,y1:integer;x2,y2:integer):boolean;
var k:Extended;
begin
Result:=false;
k:=x1 / y1; //原图的宽长比
if k<=0 then exit;
if x1>x2 then x1:=x2;
if x1<1 then exit;
y1:=trunc(x1 / k);
if y1>y2 then y1:=y2;
if y1<1 then exit;
x1:=trunc(y1 * k );
Result:=true;
end;
begin
Image:=TImage.Create(self);
try
if not GetImage(Image) then raise Exception.Create('操作无效。');
Image.AutoSize:=true;
x:=Image.Width;//图片原始宽度
y:=Image.Height;//图片原始高度
ibmp:=TBitmap.Create;
try
ibmp.Width:=x;
ibmp.Height:=y;
ibmp.Canvas.StretchDraw(ibmp.Canvas.ClipRect,Image.Picture.Graphic);
bmp:=TBitMap.Create;
try
h:=0;
if RxRichEdit1.BorderStyle=bsSingle then h:=8; //有边框时,有效高度-8
if not GetBitmapSize(x,y,RxRichEdit1.Width,RxRichEdit1.Height-h) then
raise Exception.Create('无效尺寸。');
//让待复制的图片小于、等于RxRichEdit1尺寸,并保持原有长宽比:
bmp.Width:=x;
bmp.Height:=y;
//将图片按自动拉伸方式画到bmp的画布上:
bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect,ibmp);
aclipboard:=TClipboard.Create ;
try
aclipboard.Assign(bmp);//复制到剪贴板
RxRichEdit1.PasteFromClipboard ;//粘贴到RxRichEdit1
finally
aclipboard.Free;
end;
finally
bmp.Free;
end;
finally
ibmp.Free;
end;
finally
Image.Free;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
with Form1 do begin
Height:= 256;
Width:= 340;
Position:= poScreenCenter;
Caption:='粘贴自适应图片';
RxRichEdit1:=TRxRichEdit.Create(self);
try
RxRichEdit1.Parent:=Form1;
RxRichEdit1.Align:=alTop;
RxRichEdit1.Height:=Height - 80;
except
freeAndNil(RxRichEdit1);
end;
Button1:=TButton.Create(self);
try
Button1.Parent:=Form1;
Button1.Top:=Height - 70;
Button1.Left:=Width div 2 - 38;
Button1.Caption:='加入';
Button1.OnClick:=Button1Click;
except
freeAndNil(Button1);
end;
end;
end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if RxRichEdit1<>nil then RxRichEdit1.Free;
if Button1<>nil then Button1.Free;
end;end.
if not Opendlg1.Execute then exit ;
aImage.Picture.LoadFromFile(Opendlg1.FileName);
aImage.AutoSize :=true ;
x:=aImage.Width ; //图片原始宽度
y:=aImage.Height ; //图片原始高度
h:=0;
bmp:=TBitmap.Create ;
// if RxRichEdit1.BorderStyle=bsSingle then h:=8;//有效边框时,宽度-8
if not GetBitmapSize(x,y,RxRichEdit1.Width,RxRichEdit1.Height ) then exit ;
//保持原有宽度比
bmp.Width :=x;
bmp.Height :=y;
bmp.Canvas.StretchDraw(bmp.Canvas.ClipRect,aImage.Picture.Graphic );
aClipboard:=TClipboard.Create ;
try
aClipboard.Assign(bmp);
RxRichEdit1.PasteFromClipboard ;
finally
aClipboard.Free ;
end;
finally
bmp.Free ;
end;
你说你那个为什么不行, if not Opendlg1.Execute then exit ;
aImage.Picture.LoadFromFile(Opendlg1.FileName);我看了一下我的引用单元, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Clipbrd, RzButton, StdCtrls, RxRichEd, ExtCtrls,jpeg,pngimage;多了一个Variants类型,Variants我记得是可以自动判断数据类型的吧,是不?我刚才在你的程序添加了variants单元,就不报你说的那个错了啊,你试试看哦,呵呵
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, RxRichEd, ExtCtrls;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
procedure Button1Click(Sender: TObject);
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Clipbrd,pngimage,jpeg;
var Button1:TButton;
RxRichEdit1:TRxRichEdit;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var Image:TImage;
bmp,ibmp:TBitmap;
aclipboard:TClipboard;
x,y,h,w:integer;
OpenDialog: TOpenDialog;
function GetBitmapSize(var x1,y1:integer;x2,y2:integer):boolean;
var k:Extended;
begin
Result:=false;
k:=x1 / y1; //原图的宽长比
if k<=0 then exit;
if x1>x2 then x1:=x2;
if x1<1 then exit;
y1:=trunc(x1 / k);
if y1>y2 then y1:=y2;
if y1<1 then exit;
x1:=trunc(y1 * k );
Result:=true;
end;
begin
Image:=TImage.Create(self);
try
OpenDialog:=TOpenDialog.Create(self);
try
OpenDialog.Filter:='*.bmp;*.png;*.jpg|*.bmp;*.png;*.jpg';
if OpenDialog.Execute then begin
Image.Picture.LoadFromFile(OpenDialog.FileName);
Image.AutoSize:=true;
x:=Image.Width;//图片原始宽度
y:=Image.Height;//图片原始高度
ibmp:=TBitmap.Create;
try
ibmp.Width:=x;
ibmp.Height:=y;
ibmp.Canvas.StretchDraw(ibmp.Canvas.ClipRect,Image.Picture.Graphic);
bmp:=TBitMap.Create;
try
h:=0;
w:=0;
if RxRichEdit1.BorderStyle=bsSingle then begin
h:=8; //有边框时,有效高度-8
w:=8; //有边框时,有效宽度-8
end;
if not GetBitmapSize(x,y,RxRichEdit1.Width-w,RxRichEdit1.Height-h) then
raise Exception.Create('无效尺寸。');
//让待复制的图片小于、等于RxRichEdit1尺寸,并保持原有长宽比:
bmp.Width:=x;
bmp.Height:=y;
//将图片按自动拉伸方式画到bmp的画布上:
bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect,ibmp);
aclipboard:=TClipboard.Create ;
try
aclipboard.Assign(bmp);//复制到剪贴板
RxRichEdit1.PasteFromClipboard ;//粘贴到RxRichEdit1
finally
aclipboard.Free;
end;
finally
bmp.Free;
end;
finally
ibmp.Free;
end;
end;
finally
OpenDialog.Free;
end;
finally
Image.Free;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
with Form1 do begin
Height:= 256;
Width:= 340;
Position:= poScreenCenter;
Caption:='粘贴自适应图片';
RxRichEdit1:=TRxRichEdit.Create(self);
try
RxRichEdit1.Parent:=Form1;
RxRichEdit1.Align:=alTop;
RxRichEdit1.Height:=Height - 80;
except
freeAndNil(RxRichEdit1);
end;
Button1:=TButton.Create(self);
try
Button1.Parent:=Form1;
Button1.Top:=Height - 70;
Button1.Left:=Width div 2 - 38;
Button1.Caption:='加入';
Button1.OnClick:=Button1Click;
except
freeAndNil(Button1);
end;
end;
end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if RxRichEdit1<>nil then RxRichEdit1.Free;
if Button1<>nil then Button1.Free;
end;end.
procedure TForm1.Button1Click(Sender: TObject);
var Image:TImage;
bmp,ibmp:TBitmap;
aclipboard:TClipboard;
x,y,h,w:integer;
OpenDialog: TOpenDialog;
function GetBitmapSize(var x1,y1:integer;x2,y2:integer):boolean;
var k:Extended;
begin
Result:=false;
k:=x1 / y1; //原图的宽长比
if k<=0 then exit;
if x1>x2 then x1:=x2;
if x1<1 then exit;
y1:=trunc(x1 / k);
if y1>y2 then y1:=y2;
if y1<1 then exit;
x1:=trunc(y1 * k );
Result:=true;
end;
begin
OpenDialog:=TOpenDialog.Create(self);
try
OpenDialog.Filter:='*.bmp;*.png;*.jpg|*.bmp;*.png;*.jpg';
if OpenDialog.Execute then begin
Image:=TImage.Create(self);
try
Image.Picture.LoadFromFile(OpenDialog.FileName);
Image.AutoSize:=true;
x:=Image.Width;//图片原始宽度
y:=Image.Height;//图片原始高度
ibmp:=TBitmap.Create;
try
ibmp.Width:=x;
ibmp.Height:=y;
ibmp.Canvas.StretchDraw(ibmp.Canvas.ClipRect,Image.Picture.Graphic);
bmp:=TBitMap.Create;
try
h:=0;
w:=0;
if RxRichEdit1.BorderStyle=bsSingle then begin
h:=8; //有边框时,有效高度-8
w:=8; //有边框时,有效宽度-8
end;
if not GetBitmapSize(x,y,RxRichEdit1.Width-w,RxRichEdit1.Height-h) then
raise Exception.Create('无效尺寸。');
//让待复制的图片小于、等于RxRichEdit1尺寸,并保持原有长宽比:
bmp.Width:=x;
bmp.Height:=y;
//将图片按自动拉伸方式画到bmp的画布上:
bmp.Canvas.StretchDraw(Bmp.Canvas.ClipRect,ibmp);
aclipboard:=TClipboard.Create ;
try
aclipboard.Assign(bmp);//复制到剪贴板
RxRichEdit1.PasteFromClipboard ;//粘贴到RxRichEdit1
finally
aclipboard.Free;
end;
finally
bmp.Free;
end;
finally
ibmp.Free;
end;
finally
Image.Free;
end;
end;
finally
OpenDialog.Free;
end;
end;
那是pngimage单元的bug,若要不使用bmp和ibmp两次画布,得修改pngimage的源码:
......
StretchBlt(BufferDC, 0, 0, Header.Width, Header.height, DC, Rect.Left,
Rect.Top, Header.Width, Header.Height, SRCCOPY);//我这里的是4376行
......从源码可见,它并没按的图尺寸拉伸。
StretchBlt
函数从源矩形中复制一个位图到目标矩形,必要时按目前目标设备设置的模式进行图像的拉伸或压缩以满足目标矩形的尺寸。原型:BOOL StretchBlt( HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeighDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, DWORD dwRop);参数:hdcDest:指向目标设备环境的句柄。nXOriginDest、nYOriginDest:指定目标矩形左上角的X轴和Y轴坐标,按逻辑单位表示。nWidthDest、nHeightDest:指定目标矩形的宽度和高度,按逻辑单位表示。hdcSrc:指向源设备环境的句柄。nXOriginSrc、nYOriginSrc:指向源矩形区域左上角的X轴和Y轴坐标,按逻辑单位表示。nWidthSrc、nHeightSrc:指定源矩形的宽度和高度,按逻辑单位表示。dwRop:指定要进行的光栅操作。光栅操作码定义了系统如何在输出操作中组合颜色,这些操作包括画刷、源位图和目标位图等对象。参考 BitBlt 可了解常用的光栅操作码列表。在Windows CE 1.0和1.01版中,参数dwRop只支持SRCCOPY 和 SRCINVERT 。返回值:如果函数执行成功,那么返回值为非零,如果函数执行失败,那么返回值为零。若想获得更多的错误信息,请调用GetLastError函数
......
StretchBlt(DC, Rect.Left, Rect.Top, Header.Width, Header.Height, BufferDC,
0, 0, Header.Width, Header.Height, SRCCOPY);//单元中的4482行
......从源码4482行代码可见,它并没按的图尺寸(Rect.Right-Rect.Left, Rect.Bottom-Rect.Top)拉伸,而是按源图尺寸(Header.Width, Header.Height)复制。