加载图像到的TImage,通过阵列 [英] Loading images into TImage, via array

查看:234
本文介绍了加载图像到的TImage,通过阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新德尔福,做一个项目对我的水平。当我运行我的code图像只是不显示,我看着无处不在,我的老师也帮不了我。谁能告诉我,我缺少的是什么?

I'm very new to delp doing a project for my A level. When I run my code the images just don't show, I've looked everywhere and my teacher can't help me. Can anyone tell me what I'm missing?

    const
       Animal : array[0..6] of string =                    ('Bears','Dogs','Cats','Chickens','Horses','Cows','Monkeys');
    ImagePaths : array [0..6] of string
      = ('img0.JPG', 'img1.JPG', 'img2.JPG', 'img3.JPG', 'img4.JPG', 'img5.JPG',
        'img6.JPG');

  var i:integer;
  Images : array [0..11] of TImage;

procedure LoadImages;
  var
  k,l:integer;
  begin
  Randomize;
  k:=Random(11);
  for l:= 0 to k do
  begin
    Images[l] := TImage.Create(nil);
    Images[l].Picture.LoadFromFile(ImagePaths[i])
  end

结束;

procedure TForm4.FormCreate(Sender: TObject);
begin
randomize;
i:=random(6);
QuestionLbl.Caption:=Format('How many %s are there?',[Animal[i]]);
LoadImages;
end;

的想法是,显示相同的随机选择的动物的图像的随机数为孩子然后计数和输入,如果没有什么帮助。大部分AP preciate任何帮助。

The idea is that a random number of images of the same randomly selected animal is displayed for a child to then count and input, if that helps. Much appreciate any help.

修改

因为这只是我把它全部复制到新的应用程序的原型,这是所有code我不包括:

as this is only a prototype I have copied it all to a new application and this is all the code I didn't include:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,     Controls, Forms,
  Dialogs, StdCtrls,jpeg, ExtCtrls;

type
  TForm1 = class(TForm)
    QuestionLbl: TLabel;
    procedure FormCreate(Sender: TObject);
      private
    { Private declarations }
  public
    { Public declarations }
  end;

同样的错误发生,我怕我太无知遵循什么我肯定是非常明确的指示。

The same error is occurring and I'm afraid I'm too ignorant to follow what I'm sure were very clear instructions.

推荐答案

为什么你不把你的表格上TImages,只是LoadFromFile要显示的人?
在我看来,这将是更容易。

Why you just don't put your TImages on the form, and just LoadFromFile the ones you want to show ? Appear to me that would be easier.

不过:你想完成什么?从code,我能想象你想显示多个图像的人们指望他们和回答的问题...

But: what you trying to accomplish? From the code, I can imagine you were trying to show a number of images to people count them and answer the question...

所以,如果你在表单中添加(和位置)11空(无图像)TImages,你可以这样做:

So, if you add (and position) the 11 empty(no image) TImages in the form, you can do:

// Any trouble in copying your FormCreate header, David? ;-)
procedure TMyForm.FormCreate(Sender: TObject);
begin
  Images[0] := Image_N1; // First TImage
  Images[1] := Image_N2;
  Images[2] := Image_N3;
  // Do that until the 12 slots are filled
  // As a exercise for Danny Robinson( the OP ), you can do that in a for..do using 
  // the Form.Components array property to automate it instead of 
  // doing one-at-a-line
end;

procedure ClearImages;
var I: Integer;
begin
  for I = Low(Images) to High(Images) do
  begin
    Images.Picture.Graphic := Nil;
  end;
end;



procedure LoadImages;
var
  k,l:integer;
begin
  ClearImages;
  Randomize;
  k:=Random(11);
  for l:= 0 to k do
  begin
    Images[l].Picture.LoadFromFile(ImagePaths[i])
  end;

end;

如果你仍然需要动态创建TImages,只需创建12 TImages
一旦FORMCREATE(如大卫的答案),并保持调用LoadImages。

If you still need to create the TImages on the fly, just create the 12 TImages once on FormCreate (as in David's answer) and keep calling the LoadImages.

编辑:

一些想法,因为你正在学习。结果
在即时创建可视控件是一个很无聊的(在我看来,当然)的任务,包括:

Some ideas, since you are learning.
Creating visual controls on-the-fly is a very boring(in my opinion, of course) task that involves:


  • 创建对象,显然

  • 将其分配到父控件(形式不需要这个通)

  • 相应调整其大小,以你的视觉规划

  • 在你希望它是父控件的地方它定位

  • 设置它的锚,因为它重新定位和/或当父控件大小调整(如果需要)

  • 只有经过这一切,让它做你想做的事情(在这种情况下,显示的图像)的东西。

几乎所有这些步骤<一个href=\"http://stackoverflow.com/questions/7612740/loading-images-into-timage-via-array/7612843#7612843\">David赫弗南的回答显示code吧。但是,除非你真的需要一个动态布局,做所有那些在设计时更加实用; - )

Almost all those steps David Heffernan's answer show the code for it. But, unless you really need a dynamic layout, doing all those on design-time is more practical ;-)

这篇关于加载图像到的TImage,通过阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆