1

I am trying to load icons/images from the web and put them into my TreeView. I have the following code (below) but each time it is run it throws an exception. I don't understand what I have done wrong here. Would you please be able to help me to write the correct code?

public void ImgCellRenderer(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
{
    Gdk.Pixbuf icon = Gdk.PixbufLoader.LoadFromResource("http:\\\\www.free-icons-download.net\\images\\strawberry-icon-37824.png").Pixbuf;
    (cell as Gtk.CellRendererPixbuf).Pixbuf = icon;
}

The exception in full is:

    System.ArgumentException: 'http:\\www.free-icons-download.net\images\strawberry-icon-37824.png' is not a valid resource name of assembly 'MDM, Version=1.0.5862.27549, Culture=neutral, PublicKeyToken=null'.
  at Gdk.PixbufLoader.InitFromAssemblyResource (System.Reflection.Assembly assembly, System.String resource) [0x00051] in /private/tmp/source-mono-mac-4.2.0-branch-64/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/gtk-sharp-2.12.21/gdk/generated/PixbufLoader.custom:104 
  at Gdk.PixbufLoader..ctor (System.Reflection.Assembly assembly, System.String resource) [0x00020] in /private/tmp/source-mono-mac-4.2.0-branch-64/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/gtk-sharp-2.12.21/gdk/generated/PixbufLoader.custom:94 
  at Gdk.PixbufLoader.LoadFromResource (System.String resource) [0x00007] in /private/tmp/source-mono-mac-4.2.0-branch-64/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/gtk-sharp-2.12.21/gdk/generated/PixbufLoader.custom:142 
  at MDM.TweetSelectorWidget.ImgCellRenderer (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, TreeModel model, TreeIter iter) [0x00006] in /Users/tribehive/Projects/MDM/MDM/TweetSelectorWidget.cs:160 
  at GtkSharp.TreeCellDataFuncWrapper.NativeCallback (IntPtr tree_column, IntPtr cell, IntPtr tree_model, IntPtr iter, IntPtr data) [0x0002c] in /private/tmp/source-mono-mac-4.2.0-branch-64/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/gtk-sharp-2.12.21/gtk/generated/GtkSharp.TreeCellDataFuncNative.cs:57 
  at GLib.ExceptionManager.RaiseUnhandledException (System.Exception e, Boolean is_terminal) [0x0003b] in /private/tmp/source-mono-mac-4.2.0-branch-64/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/gtk-sharp-2.12.21/glib/ExceptionManager.cs:58 
  at GtkSharp.TreeCellDataFuncWrapper.NativeCallback (IntPtr tree_column, IntPtr cell, IntPtr tree_model, IntPtr iter, IntPtr data) [0x00051] in /private/tmp/source-mono-mac-4.2.0-branch-64/bockbuild-
Ben Hayward
  • 1,444
  • 23
  • 37
  • Use `/` for URLs, not `\​`. – andlabs Jan 19 '16 at 15:50
  • I've changed it to / and still it doesn't work – Ben Hayward Jan 19 '16 at 15:54
  • 1
    Ah, I see. `LoadFromResource()` does not take a generic URL; it requires you to provide the resource as an assembly resource. I'm not sure what that means for Gtk#. From what I can gather, you'll need to open the URL as a System.IO.Stream to load it. (I'm pretty sure vanilla GdkPixbuf has a function for this, but I'm not quite sure how Gtk# uses GdkPixbuf at all.) – andlabs Jan 19 '16 at 16:00

1 Answers1

1

I have managed to solve it! @andlabs was correct in saying you can achieve this by creating a stream to load it. But this involves the creation of many objects (Stream, BinaryReaders, BinaryWriters, etc). Please see his comment for details. Thanks for your help andlabs.

The easier way to achieve this is with a WebClient. You will notice that you can create a Gdk.Pixbuf from a byte[]. So, to create a pixbuf from a downloaded image:

WebClient webClient = new WebClient();
byte[] image = webClient.DownloadDate(url);
Gdk.Pixbuf myPixbuf = new Gdk.Pixbuf(image);
Ben Hayward
  • 1,444
  • 23
  • 37