我正在尝试从网络加载图标/图像并将它们放入我的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-
答案 0 :(得分:1)
我设法解决了! @andlabs说你可以通过创建一个流来加载它来实现这个目的是正确的。但这涉及创建许多对象(Stream
,BinaryReaders
,BinaryWriters
等)。 详情请见他的评论。感谢您的帮助和帮助。
实现此目标的更简单方法是使用WebClient
。您会注意到您可以从byte[]
创建Gdk.Pixbuf。因此,要从下载的图像创建pixbuf:
WebClient webClient = new WebClient();
byte[] image = webClient.DownloadDate(url);
Gdk.Pixbuf myPixbuf = new Gdk.Pixbuf(image);