请教:如何使Timage中的图片按比例显示? (30分)

  • 主题发起人 主题发起人 sinahappy
  • 开始时间 开始时间
S

sinahappy

Unregistered / Unconfirmed
GUEST, unregistred user!
把image2放到panel1上,panel1是正方形,尺寸固定,widthxheight=230X230
procedure TForm3.Button3Click(Sender: TObject);
begin
image2.Picture.LoadFromFile(dbedit1.text);
end;
button3是看图片,dbedit1.text对应的是数据库中的文件名,可以把图片显示出来,比例有问题, image2属性:Image2.AutoSize:=false; Image2.Stretch:=true;
现在需要把image2按宽和高的比例显示全,这样的程序语句应如何写,请帮助,谢谢!
举例:
如果图片实际尺寸为widthxheight=230x460 则显示为widthXheight=115x230 在宽度左右留白。
如果图片实际尺寸为widthxheight=460x230 则显示为widthXheight=230x115 在高度上下留白。
 
用canvas ,具体的搜索一下。
 
可以使用Image.Canvas.CopyRect函数来解决
载入图片的大小可以通过image2.Picture.Width和Height获得。

还有问题吗?
 
请把具体的程序语句写下来好吗,谢谢!
 
请把具体的程序语句写下来好吗,谢谢!
 
procedure TForm1.BitBtn1Click(Sender: TObject);
var
ARect:TRect;
begin
//Image1.AutoSize:=true; Image1.Stretch:=false; Image1.Visible:=false;
Image1.Picture.LoadFromFile(dbedit1.text);
//ARect 是Image2要显示的区域,你可以根据Image1.Picture.Width 和height计算
Image2.Canvas.CopyRect(ARect, Image1.Canvas, Image1.BoundsRect);
end;
这样够了吗?
 
哈哈,这样代码全了(已测试)
procedure TForm1.BitBtn1Click(Sender: TObject);
var
ARect:TRect;
AScale:single;
begin
Self.DoubleBuffered:=true;
//Image1.AutoSize:=true; Image1.Stretch:=false; Image1.Visible:=false;
Image1.Picture.LoadFromFile(dbedit1.text);
//ARect 是Image2要显示的区域,你可以根据Image1.Picture.Width 和height计算
if Image1.Picture.Bitmap.Empty then Exit;
AScale:=Image2.Width/Image1.Picture.Width;
if AScale>(Image2.Height/Image1.Picture.Height) then
AScale:=Image2.Height/Image1.Picture.Height;
ARect:=Rect(0, 0, Round(AScale*Image1.Picture.Width), Round(AScale*Image1.Picture.Height));
Image2.Canvas.CopyRect(ARect, Image1.Canvas, Image1.BoundsRect);
end;
 
谢谢yostgxf的回复:
我把你的代码贴上:
procedure TForm3.Button3Click(Sender: TObject);
var
ARect:TRect;
AScale:single;
begin
Self.DoubleBuffered:=true;
// Image1.AutoSize:=true; Image1.Stretch:=false; Image1.Visible:=false;
Image1.Picture.LoadFromFile(dbedit1.text);
//ARect 是Image2要显示的区域,你可以根据Image1.Picture.Width 和height计算
if Image1.Picture.Bitmap.Empty then Exit;
AScale:=Image2.Width/Image1.Picture.Width;
if AScale>(Image2.Height/Image1.Picture.Height) then
AScale:=Image2.Height/Image1.Picture.Height;
ARect:=Rect(0, 0, Round(AScale*Image1.Picture.Width),
Round(AScale*Image1.Picture.Height));
Image2.Canvas.CopyRect(ARect, Image1.Canvas, Rect(0, 0, Image1.Width,
Image1.Height));
end;
图片显示不全,请帮助!
图片显示区域我给他是固定大小230x230
 
只有图象是.bmp才能显示,如果是.jpg图象就无法显示,如何解决,请帮助谢谢!
 
呵呵,再修改。
1。图片显示不全,不会的。你修改一下image2的属性呢,我例子删除了。
2。如果是.jpg图象 就这样:
uses jpeg;
var jpgimage: Tjpegimage;

jpgimage:=Tjpegimage.Create;
jpgimage.LoadFromFile(dbedit1.text);
Image1.Picture.Bitmap.Assign(jpgimage);
jpgimage.Free;

OK!

 
吃饭了,还有问题吃完饭再说。我想应该没问题了,哈哈。再有,该挨骂了,哈哈
 
谢谢yostgxf的回复:
图片可以显全,这段语句uses jpeg;
var jpgimage: Tjpegimage;

jpgimage:=Tjpegimage.Create;
jpgimage.LoadFromFile(dbedit1.text);
Image1.Picture.Bitmap.Assign(jpgimage);
jpgimage.Free;
如何放到你提供程序语句中,请帮助!
 
我要疯了,再加点分,太累了

呵呵,是该挨骂了吧。
1。把jpeg加到最前面的uses中去, 这不会不知道吧。

2。这样:
procedure TForm3.Button3Click(Sender: TObject);
var
ARect:TRect;
AScale:single;
jpgimage: Tjpegimage;
begin
Self.DoubleBuffered:=true;
// Image1.AutoSize:=true; Image1.Stretch:=false; Image1.Visible:=false;
//加在这里!!!
jpgimage:=Tjpegimage.Create;
jpgimage.LoadFromFile(dbedit1.text);
Image1.Picture.Bitmap.Assign(jpgimage);
jpgimage.Free;
//去掉这句 Image1.Picture.LoadFromFile(dbedit1.text);
//ARect 是Image2要显示的区域,你可以根据Image1.Picture.Width 和height计算
if Image1.Picture.Bitmap.Empty then Exit;
AScale:=Image2.Width/Image1.Picture.Width;
if AScale>(Image2.Height/Image1.Picture.Height) then
AScale:=Image2.Height/Image1.Picture.Height;
ARect:=Rect(0, 0, Round(AScale*Image1.Picture.Width),
Round(AScale*Image1.Picture.Height));
Image2.Canvas.CopyRect(ARect, Image1.Canvas, Rect(0, 0, Image1.Width,
Image1.Height));
end;
 
接受答案了.
 
后退
顶部