1

我正在尝试从网络加载图标/图像并将它们放入我的 TreeView 中。我有以下代码(如下),但每次运行它都会引发异常。我不明白我在这里做错了什么。你能帮我写正确的代码吗?

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;
}

完整的例外是:

    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-
4

1 回答 1

1

我已经设法解决了!@andlabs 说您可以通过创建一个流来加载它来实现这一点是正确的。但这涉及到许多对象(StreamBinaryReadersBinaryWriters等)的创建。详情请看他的评论。感谢您的帮助和实验室。

实现这一点的更简单方法是使用WebClient. 您会注意到您可以从byte[]. 因此,要从下载的图像创建 pixbuf:

WebClient webClient = new WebClient();
byte[] image = webClient.DownloadDate(url);
Gdk.Pixbuf myPixbuf = new Gdk.Pixbuf(image);
于 2016-01-20T10:10:30.410 回答