[英]Error while opening png image with Gdk::Pixbuf::create_from_resource
我正在嘗試使用 Gdk::Pixbuf::create_from_resource 讀取 png 圖像:
#include <iostream>
#include <gtkmm.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
Gtk::Window window;
window.set_default_size(100, 100);
try {
Glib::RefPtr<Gdk::Pixbuf> image
= Gdk::Pixbuf::create_from_resource("image.png");
} catch (const Glib::Error &error) {
std::cerr << "Failed to load an image: " << error.what() << std::endl;
}
return app->run(window);
}
但是會出現錯誤:
$ ./a.out
Failed to load an image: The resource at “image.png” does not exist
$ ls -l
total 128
-rwxrwxr-x. 1 user user 36528 Jul 18 15:01 a.out
-rw-r--r--. 1 user user 88792 Jul 18 15:00 image.png
-rw-r--r--. 1 user user 449 Jul 18 15:00 main.cpp
gtkmm 版本 3.24.6
如果要將圖像文件直接加載到程序中,而不是:
Glib::RefPtr<Gdk::Pixbuf> image
= Gdk::Pixbuf::create_from_resource("image.png");
您將使用以下語句:
Glib::RefPtr<Gdk::Pixbuf> image
= Gdk::Pixbuf::create_from_file("image.png");
如果您確實想將圖像文件用作資源,則需要首先使用資源定義 XML 文件通過“glib-compile-resources”函數生成資源文件。 例如。
glib-compile-resources --target=image.c --generate-source resource.xml
在您的 XML 文件中,您可能會有某種類型的定義,如下所示。
<gresources>
<gresource prefix="Image">
<file preprocess="xml-stripblanks">image.png</file>
</gresource>
</gresources>
然后在您的程序中,您的創建語句將類似於以下內容。
Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_resource("Image/image.png");
最后,您將修改您的編譯命令以將生成的資源文件包含在您的“main.cpp”文件中以創建您的程序。
g++ -Wno-format -o a.out main.cpp image.c `pkg-config --cflags --libs gtkmm-3.0` `pkg-config --cflags --libs gdkmm-3.0`
希望能澄清事情。
問候。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.