簡體   English   中英

WinForms PictureBox:嘗試從文件路徑顯示照片

[英]WinForms PictureBox: Trying to display a photo from a file path

我正在嘗試做的事情感覺非常簡單,但是在嘗試時它會不斷拋出錯誤。

目標:我正在自學 C#,為此我正在構建自己的 SW5E 角色生成器,原因何在。 我目前正在嘗試做的是,當您單擊一個物種時,它將在 RichTextBox 中顯示物種信息,並在 PictureBox 中顯示該物種的照片。

問題:我可以很好地顯示富文本,但是當我嘗試在其中加載圖像時,每次都會引發內存不足錯誤。

我的代碼如下:

private void SpeciesList_Click(object sender, EventArgs e)
        {
            // Here we save the selected list item
            string selectedSpecies = SpeciesList.SelectedItems[0].SubItems[0].Text;

            // We are saving that selection for later
            _selectedSpecies = selectedSpecies;

            // Loading the information in the rich text document just fine
            SpeciesInfoRichTxt.LoadFile($@"D:\repos\Character Builder\Species\{selectedSpecies}\{selectedSpecies}.rtf");

            // This is where we run into issues, as this just throws an Out of Memory error each time
            // It doesn't even have a default image set, the box itself is blank
            SpeciesPictureBox.Image = Image.FromFile($@"D:\repos\Character Builder\Species\{selectedSpecies}\{selectedSpecies}.jpg");
        }

我不是 00% 確定我哪里出錯了。 我已經在幾台不同的計算機上測試了這段代碼,因此文件路徑目前指向 D:。 任何幫助將不勝感激。

我想到了。 並不是我沒有處理圖像(我確實添加SpeciesPictureBox.Image?.Dispose();這並沒有改變結果。當我最初保存文件時,我最終改變了擴展名試圖默認另存為.jpg,完全不認為它會破壞文件。當我回去保存它,然后使用Paint另存為.jpg,然后Image.FromFile停止拋出錯誤並工作。

我想如果您遇到與我相同的問題,請確保在將圖像移動到需要的位置時不只是更改文件擴展名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM