BCB中如何将image中和图像添加到imagelist中?

MY_MCU 2011-03-21 01:54:19
image尺寸设置为120*120,imagelist的尺寸设为30*30,如何将image中的图像添加到imagelist中?自动添加到imagelist的结尾处,和添加到指定的index中,又如何操作?
...全文
897 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
MY_MCU 2011-03-22
  • 打赏
  • 举报
回复
太感谢妖哥了,每次到BCB区,都会得到妖哥的帮助.真的是太感谢了.
ccrun.com 2011-03-22
  • 打赏
  • 举报
回复
另外, TImage的大小和缩放也不必限定了.
ccrun.com 2011-03-22
  • 打赏
  • 举报
回复
OK, 给你一个更好的解决方案, 无需判断Image中图像的格式了:
void __fastcall CrnAddTImageToImageList(TImage *img, TImageList *il)
{
Graphics::TBitmap *bmp = new Graphics::TBitmap;

bmp->Width = 30;
bmp->Height = 30;

bmp->Canvas->StretchDraw(TRect(0, 0, 30, 30), img->Picture->Graphic);

il->Add(bmp, NULL);

delete bmp;
}

// ---------------------------------------------------------------------------
//
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CrnAddTImageToImageList(Image1, ImageList1);
}
MY_MCU 2011-03-22
  • 打赏
  • 举报
回复
妖哥,如果将TImage的大小设定为30*30,预览图像时,会觉得太小,有没有办法修改image的图像至30*30,或转成bmp后再缩小?
ccrun.com 2011-03-21
  • 打赏
  • 举报
回复
另外,多个Form共用ImageList,建议增加一个数据模块(Data Module),然后将ImageList放到数据模块中。
ccrun.com 2011-03-21
  • 打赏
  • 举报
回复
>>>将已打开的icon和jpg图像转成bmp
参考Bitmap的Assign方法。

>>>图像大于120时,增加到imagelist中自动缩小至30*30
将TImage的大小设定为30*30,AutoSize=false,Stretch=true
MY_MCU 2011-03-21
  • 打赏
  • 举报
回复
谢谢妖哥的耐心指导.不过,我的程序是Form2中image1打开图象后,点确定键后,增加到form1的ImageList中,我现在定义了一个bmp全局变量,通过它传递到form1中.所以还有个问题,请妖哥帮下忙:如何将已打开的icon和jpg图像转成bmp?另外,如果图像大于120时,增加到imagelist中时,只显示局部,如何让它自动缩小至30*30?
再次感谢妖哥帮忙!
ccrun.com 2011-03-21
  • 打赏
  • 举报
回复
针对Image中不同格式的图片,分别进行不同的处理即可。
void __fastcall CrnAddTImageToImageList(TImage *img, TImageList *il)
{
if (img->Picture->Graphic->InheritsFrom(__classid(Graphics::TBitmap)))
il->Add(img->Picture->Bitmap, NULL);
else if (img->Picture->Graphic->InheritsFrom(__classid(TIcon)))
il->AddIcon(img->Picture->Icon);
else if (img->Picture->Graphic->InheritsFrom(__classid(TJPEGImage)))
{
Graphics::TBitmap *bmp = new Graphics::TBitmap;

bmp->Width = img->Picture->Width;
bmp->Height = img->Picture->Height;

bmp->Assign(img->Picture->Graphic);

il->Add(bmp, NULL);

delete bmp;
}
}

// ---------------------------------------------------------------------------
//
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CrnAddTImageToImageList(Image1, ImageList1);
}
MY_MCU 2011-03-21
  • 打赏
  • 举报
回复
打开文件时,有三种文件格式.
.jpg,.bmp,.ico
MY_MCU 2011-03-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ccrun 的回复:]
这是因为你的Image中加载的不是位图(Bitmap)格式。需要转换一下。
[/Quote]
如果转换呢?妖哥.新手,请您不要见笑.
ccrun.com 2011-03-21
  • 打赏
  • 举报
回复
这是因为你的Image中加载的不是位图(Bitmap)格式。需要转换一下。
MY_MCU 2011-03-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ccrun 的回复:]
C/C++ code
// 添加到最后:
ImageList1->Add(Image1->Picture->Bitmap, NULL);

// 添加到i后面:
ImageList1->Insert(i, Image1->Picture->Bitmap, NULL);
[/Quote]
妖哥,直接使用您的代码会出现如下错误,请问还要如何处理?
新手发贴,还希望您不厌其烦.
"Project usb_v2.exe raised exception class EInvalidOperation with message'Invalid image size'.Proces stopped.Use Step or Run to continue."
ccrun.com 2011-03-21
  • 打赏
  • 举报
回复
// 添加到最后:
ImageList1->Add(Image1->Picture->Bitmap, NULL);

// 添加到i后面:
ImageList1->Insert(i, Image1->Picture->Bitmap, NULL);
魔方幻影 2011-03-21
  • 打赏
  • 举报
回复
不知道可不可以无损实现你那功能。但不是直接可以从程序外部动态加载到Image或ImageList吗。

13,825

社区成员

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

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