I want to display an SVG image in a GTK DrawingArea without losing quality when scaling. Currently, I am using the following code:
fn build_ui(application: >k::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_default_size(WIDTH, HEIGHT);
let drawing_area = DrawingArea::new();
window.add(&drawing_area);
let file_data = include_str!("../file.svg").as_bytes();
let loader = PixbufLoader::new();
loader.write(file_data);
loader.close();
let pixbuf = loader.pixbuf().unwrap();
let pixbuf = pixbuf.scale_simple(100, 100, InterpType::Hyper).unwrap();
drawing_area.connect_draw(move |_, cr| {
cr.set_source_pixbuf(&pixbuf, 0.0, 0.0);
cr.paint();
Inhibit(false)
});
window.show_all();
}
The original image size is 50x50 pixels, and I am scaling it to 100x100 pixels, which results in pixelation. This is because Pixbuf works with raster images. How can I work with vector output without losing quality at any scaling?
To load an SVG image and scale it to whatever size you desire, you can use the
from_file_at_scale()function. It takes four arguments: the path to the image file, the width to scale it to, the height to scale it to, and a bool to control whether or not the aspect ratio is preserved.So, instead of all the
PixbufLoadershenanigans, you can load and scale your image in one line, like this:Putting this into a full program, it will look something like this: