2016-12-21 135 views
1

我找到了一個從資源文件加載位圖的解決方案How to store images in FireMonkey?我試圖在我的Firemonkey應用程序中使用它,但它找不到資源並引發錯誤「EresNotFound」。 我的資源.RC文件是這樣的從資源加載位圖

Bitmap_1 BITMAP "Test.bmp" 

和我的代碼是

procedure Tform1.load_image_from_resource(var Im1: Timage; res_name: String); 
var InStream: TResourceStream; 
begin 
    InStream := TResourceStream.Create(HInstance, res_name,RC_RTDATA); 
    try 
    Im1.Picture.Bitmap.LoadFromStream(InStream); 
    finally 
    InStream.Free; 
    end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    Load_image_from_resource(Image1,'Bitmap_1'); 
end; 

我也發現了這個問題Delphi 2010: unable to find resource - EResNotFound的解決方案。 但它仍然沒有找到林資源

+0

你讀這篇文章https://delphihaven.wordpress.com/2013/01/26/surviving-without-image-lists-in- fmx /並遵循了從那裏開始的所有步驟? – RBA

回答

7

有在你的代碼的幾個問題,你需要聲明的資源作爲RCDATA

Bitmap_1 RCDATA "Test.bmp" 

也像你創建VCL應用程序並且在資源類型錯字名字,它應該是RT_RCDATA,工作FireMonkey代碼看起來像這樣

procedure Tform1.load_image_from_resource(var Im1: Timage; res_name: String); 
var InStream: TResourceStream; 
begin 
    InStream := TResourceStream.Create(HInstance, res_name, RT_RCDATA); 
    try 
    Im1.Bitmap.LoadFromStream(InStream); 
    finally 
    InStream.Free; 
    end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    Load_image_from_resource(Image1, 'Bitmap_1'); 
end; 
+0

非常感謝。它工作正常。非常感謝。 – Fayyaz

+0

@Fayyaz,請[接受](http://stackoverflow.com/help/someone-answers)這個答案,如果它是解決你的問題...... – whosrdaddy

+0

'BITMAP'是一個有效的[資源類型](HTTPS: //msdn.microsoft.com/en-us/library/windows/desktop/ms648009.aspx),創建'TResourceStream'時可以使用'RT_BITMAP'而不是'RT_RCDATA'。 –