图像缩放和居中显示的问题

flmn 2003-05-01 11:05:35
一个MDI子窗体中,上面放一个TImage,动态载入图片

我想让载入的图像缩放正好显示,如果图片的长和宽都小于MDI主窗体(FormMain)的长和宽,就按原来大小显示,如果不满足以上条件,就缩放,显示的图像尽量大,最后让这个子窗体显示在主窗体的中间。源码如下:

if (Image->Picture->Width < FormMain->ClientWidth - 55
&& Image->Picture->Height < FormMain->ClientHeight - 55) {
int picWidth = Image->Picture->Width;
int picHeight = Image->Picture->Height;
Image->Width = picWidth;
Image->Height = picHeight;
ClientWidth = picWidth;
ClientHeight = picHeight;
} else if (Image->Picture->Width > Image->Picture->Height) {
Image->Width = FormMain->ClientWidth - 50;
ClientWidth = Image->Width;
Image->Height = Image->Picture->Height* ClientHeight/Image->Picture->Width;
} else {
Image->Height = FormMain->ClientHeight - 50;
ClientHeight = Image->Height;
Image->Width = Image->Picture->Width * ClientWidth / Image->Picture->Height;
}

Left = (FormMain->ClientWidth - Width) / 2;
Top = (FormMain->ClientHeight - Height) / 2;

现在的问题是,如果图片的长或宽有一个大于MDI主窗体(FormMain)的长或宽 ,显示出来的图片旁边有很大空白,我不明白,请高手给我看看。

Image的几个属性:
autosize=false
Stretch = true
Proportional=true
...全文
124 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuv2002 2003-05-12
  • 打赏
  • 举报
回复
给你一个函数吧

TRect Set_Size( int OwnerWidth, int OwnerHeight, int BmpWidth, int BmpHeight)
{
TRect result;
int High,Width;
int X1,Y1,X2,Y2;
if (((float)OwnerWidth/OwnerHeight)>((float)BmpWidth/BmpHeight)){
High = OwnerHeight;
Width = (double)High*(double)BmpWidth/(double)BmpHeight;
}
else{
Width = OwnerWidth;
High = (double)Width*(double)BmpHeight/(double)BmpWidth;
}
X1 = (OwnerWidth-Width)/2;
Y1 = (OwnerHeight-High)/2;
X2 = X1 + Width;
Y2 = Y1 +High;
result.Left = X1;
result.Top = Y1;
result.Right = X2;
result.Bottom = Y2;
return result;
}
参数OwnerWidth OwnerHeight 是Image 的宽高 BmpWidth BmpHeight 是bmp 的宽高
返回的矩形就是bmp应该在image 里面的位置
记住Image AutoSize = false Stretch = false
这样就能保证图象显示的宽高和原图宽高比例一样了
例:
Graphics::TBitmap * bmp = new Graphics::TBitmap;
bmp->LoadFromFile("c:\\b.bmp");
TRect rect = Set_Size(Image1->Width,Image1->Height,bmp->Width,bmp->Height);
Image1->Canvas->StretchDraw(rect,bmp);
delete bmp; bmp = NULL;
sprewellkobe 2003-05-10
  • 打赏
  • 举报
回复
那是自然,因为图象缩小的比例和你窗口(不符合要求的)的比例不一样呀
flmn 2003-05-02
  • 打赏
  • 举报
回复
bmp->Width,bmp->Height是什么变量?

指代哪个?
Huy 2003-05-01
  • 打赏
  • 举报
回复
老兄!我想你是搞错了!如果你不想使用Autosize=true,这样啊!
bmp->Width = Image->Width;
bmp->Height = Image->Height;

然后再来确定Image控件的位置!

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧