简体   繁体   English

从文件中加载图片,文件名作为一个变化的变量

[英]Loading picture from file with filename as a changing variable

I'm currently busy with a project, extremely tired and I can't think straight, and I'm having some trouble loading a picture from a file, where the name of the picture is stored in a variable that changes according to what someone selects in a combobox.我目前正忙于一个项目,非常累,我无法直接思考,而且我在从文件加载图片时遇到了一些问题,其中图片的名称存储在一个变量中,该变量根据某人的内容而变化在组合框中选择。 The combobox is sorted and the first ItemIndex is the name of the first piece in the table.组合框已排序,第一个 ItemIndex 是表中第一部分的名称。

Here's the (horrifying and mediocre) code for declaring the file name to a variable:这是用于将文件名声明为变量的(可怕而平庸的)代码:

begin
  iTel := 0;

  with dmMusiekstukke do
  begin
    while not (tblMusiekstukke.Eof) AND (iTel <= 4) do
    begin
      tblMusiekstukke.First;
          Inc(iTel) ;
       arrMusic[itel] := tblMusiekstukke['Musicpiece name'] ;
       tblMusiekstukke.Next ;
      end;
  end;

        case cbbMusic.ItemIndex of
        0 : Image := arrMusic[1] ;
        1 : image := arrMusic[2] ;
        2 : Image := arrMusic[3] ;
        3 : Image := arrmusic[4] ;

        end;

       end;

The file name of the image is exactly the same as the name of the item in the databases's 'Musicpiece name' field.图像的文件名与数据库的“音乐作品名称”字段中的项目名称完全相同。 The array (of string) should be filled with the name of the Musicpiece, which in turn is also the name of the file.数组(字符串)应填充 Musicpiece 的名称,该名称又是文件的名称。 Out of the combobox, the person selects the music piece they want and the picture for that specific music piece is displayed.在组合框之外,该人选择他们想要的音乐作品,并显示该特定音乐作品的图片。

Code I have for loading the file :我用于加载文件的代码:

procedure TfrmSheets.FormShow(Sender: TObject);
begin

imgSheet.Picture.LoadFromFile(  '''' +  frmMusic.Image + '.png');
end;

end.

As you can see I'm really not sure how to do it.正如你所看到的,我真的不知道该怎么做。 All the pictures are in .png format.所有图片都是.png格式。 Thank you very much.非常感谢。

be careful.当心。 if frmMusic.Image contains only the file name, then the picture file must reside in the \\you_project\\Win32\\Debug如果frmMusic.Image只包含文件名,则图片文件必须驻留在\\you_project\\Win32\\Debug

Can you check, what contains frmMusic.Image你能检查一下,什么包含frmMusic.Image

Try put this value to Label尝试将此值放入Label

Alright I sorted it out.好的,我整理出来了。 It worked by not adding apostrophes on either sides, that was one of my problems, but the other solution to a problem I found here: The system cannot find the file specified它的工作原理是不在任一侧添加撇号,这是我的问题之一,但我在这里找到的问题的另一种解决方案是: 系统找不到指定的文件

I then did this:然后我这样做了:

imgSheet.Picture.LoadFromFile(frmMusic.Image + '.png.png');

I also removed我也删了

tblMusiekstukke.First ; 

out of the While loop, as it was constantly placing the marker of the table to first item.退出 While 循环,因为它不断将表格的标记放置到第一项。 It now works perfectly.它现在可以完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM