#windows #thumbnail #preview

thumbcache

Get file thumbnail on Windows

6 releases (3 breaking)

new 0.4.0 May 13, 2025
0.3.0 May 8, 2025
0.2.1 Jan 25, 2025
0.2.0 Sep 27, 2024
0.1.1 Aug 27, 2024

#5 in #thumbnail

Download history 108/week @ 2025-01-25 12/week @ 2025-02-01 2/week @ 2025-02-08 1/week @ 2025-02-15 2/week @ 2025-04-12 99/week @ 2025-05-03 141/week @ 2025-05-10

240 downloads per month

MIT license

9KB
137 lines

thumbcache

Uses Windows thumbcache to get bmp preview for a file.

Usage

When trying to get preview from file that is not an image (.zip or .exe) will result in error.

use std::io::{Error, Write};

pub fn main() -> Result<(), Error> {
  let bmp = thumbcache::get_bmp(r"C:\path-to-file.jpeg", thumbcache::ThumbSize::S96)?;
  
  let mut file_out = std::fs::File::create("./out.bmp")?;
  let _ = file_out.write_all(&bmp);
  
  Ok(())
}

Examples

Convert BMP into JPEG

Windows can only return BMP, but this format may not always be convenient. In this example, BMP is converted to JPEG using image crate.

use thumbcache::{get_bmp};
use image::{load_from_memory};

fn main() {
  let bmp = get_bmp(r"C:\path-to-file.jpeg", thumbcache::ThumbSize::S256).unwrap();

  let image = load_from_memory(&bmp).unwrap();

  let mut buf: Vec<u8> = Vec::new();
  let mut writer = std::io::Cursor::new(&mut buf);

  image.write_to(&mut writer, image::ImageFormat::Jpeg).unwrap();

  std::fs::write("./output.jpeg", buf).unwrap();
}

Sources

https://stackoverflow.com/questions/14207618/get-bytes-from-hbitmap

https://stackoverflow.com/questions/21751747/extract-thumbnail-for-any-file-in-windows

https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishellitemimagefactory-getimage

Dependencies

~114MB
~2M SLoC