LibGUI: Thumbnail keep the aspect ratio of the original file.

This commit is contained in:
VAN BOSSUYT Nicolas 2020-04-07 20:38:54 +02:00 committed by Andreas Kling
parent e9de8a445f
commit f85aa8462f
Notes: sideshowbarker 2024-07-19 07:49:28 +09:00

View File

@ -470,9 +470,15 @@ static RefPtr<Gfx::Bitmap> render_thumbnail(const StringView& path)
auto png_bitmap = Gfx::Bitmap::load_from_file(path);
if (!png_bitmap)
return nullptr;
double scale = min(32 / (double)png_bitmap->width(), 32 / (double)png_bitmap->height());
auto thumbnail = Gfx::Bitmap::create(png_bitmap->format(), { 32, 32 });
Gfx::Rect destination = Gfx::Rect(0, 0, (int)(png_bitmap->width() * scale), (int)(png_bitmap->height() * scale));
destination.center_within(thumbnail->rect());
Painter painter(*thumbnail);
painter.draw_scaled_bitmap(thumbnail->rect(), *png_bitmap, png_bitmap->rect());
painter.draw_scaled_bitmap(destination, *png_bitmap, png_bitmap->rect());
return thumbnail;
}