Невозможно загрузить встроенное изображение в mono GTK#

#c# #linux #mono #gtk#

#c# #linux #mono #gtk#

Вопрос:

Я пытаюсь загрузить изображение в mono gtk # в Ubuntu.

в файле csproj я добавил

 <ItemGroup>
 <EmbeddedResource Include="Images/Disable_Tilt_200px.png"/>
</ItemGroup>
  

В коде я добавил

 Gdk.Pixbuf  image1 = Gdk.Pixbuf.LoadFromResource("Disable_Tilt_200px.png");
  

но когда я пытаюсь запустить, я получаю следующую ошибку

 Unhandled Exception:
System.ArgumentException: 'Disable_Tilt_200px.png' is not a valid resource name of assembly 'suiModel, Version=1.0.7578.28449, Culture=neutral, PublicKeyToken=null'.
  at Gdk.PixbufLoader.InitFromAssemblyResource (System.Reflection.Assembly assembly, System.String resource, System.Byte[] streamingBuffer) [0x00050] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.PixbufLoader..ctor (System.Reflection.Assembly assembly, System.String resource, System.Byte[] streamingBuffer) [0x00018] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.PixbufLoader..ctor (System.Reflection.Assembly assembly, System.String resource) [0x00000] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.Pixbuf..ctor (System.Reflection.Assembly assembly, System.String resource) [0x00021] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.Pixbuf.LoadFromResource (System.String resource) [0x00005] in <2f625623313f467f9368802ad38d635d>:0 
  at MainWindow..ctor () [0x00033] in <1d872bfd9434487a96ebfc391137905b>:0 
  at suiModel.MainClass.Main (System.String[] args) [0x00007] in <1d872bfd9434487a96ebfc391137905b>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: 'Disable_Tilt_200px.png' is not a valid resource name of assembly 'suiModel, Version=1.0.7578.28449, Culture=neutral, PublicKeyToken=null'.
  at Gdk.PixbufLoader.InitFromAssemblyResource (System.Reflection.Assembly assembly, System.String resource, System.Byte[] streamingBuffer) [0x00050] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.PixbufLoader..ctor (System.Reflection.Assembly assembly, System.String resource, System.Byte[] streamingBuffer) [0x00018] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.PixbufLoader..ctor (System.Reflection.Assembly assembly, System.String resource) [0x00000] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.Pixbuf..ctor (System.Reflection.Assembly assembly, System.String resource) [0x00021] in <2f625623313f467f9368802ad38d635d>:0 
  at Gdk.Pixbuf.LoadFromResource (System.String resource) [0x00005] in <2f625623313f467f9368802ad38d635d>:0 
  at MainWindow..ctor () [0x00033] in <1d872bfd9434487a96ebfc391137905b>:0 
  at suiModel.MainClass.Main (System.String[] args) [0x00007] in <1d872bfd9434487a96ebfc391137905b>:0 
  

Ответ №1:

Я сам нашел ошибку.

В файл csproj необходимо добавить следующее.

 <ItemGroup>
     <EmbeddedResource Include="Images/Disable_Tilt_200px.png"/>
</ItemGroup>
  

Файл должен быть указан как

 Gdk.Pixbuf image1 = Gdk.Pixbuf.LoadFromResource("suiModel.Images.Disable_Tilt_200px.png");
  

где «suiModel» — это имя сборки / проекта.

Если мы не уверены в имени встроенного ресурса, добавьте в код следующее.

 var assemblyName = new AssemblyName(myAssemblyName);
var resources = string.Join(Environment.NewLine, Assembly.Load(assemblyName).GetManifestResourceNames());
Console.WriteLine("List of Manifest Resource Names");
Console.WriteLine("resources");
  

При этом будут напечатаны все встроенные ресурсы.