Skip to content

Instantly share code, notes, and snippets.

@H3mnz
Created August 28, 2024 11:17
Show Gist options
  • Save H3mnz/b5c4c2523f42b9839a67e25d441243b0 to your computer and use it in GitHub Desktop.
Save H3mnz/b5c4c2523f42b9839a67e25d441243b0 to your computer and use it in GitHub Desktop.
Get Thumbnail from File in Windows with Dart
import 'dart:developer';
import 'dart:typed_data';
import 'package:windows_storage/windows_storage.dart';
class ThumbnailGenerator {
ThumbnailGenerator._();
static Future<Uint8List> generate(String path) async {
try {
final storageFile = await StorageFile.getFileFromPathAsync(path);
if (storageFile == null) throw Exception('File Not Exists.');
final thumbnail = await storageFile.getThumbnailAsyncOverloadDefaultSizeDefaultOptions(ThumbnailMode.singleItem);
if (thumbnail == null) throw Exception('Thumbnail Not Exists.');
DataReader reader = DataReader.createDataReader(thumbnail.getInputStreamAt(0));
await reader.loadAsync(thumbnail.size);
return Uint8List.fromList(reader.readBytes(thumbnail.size));
} catch (e) {
log(e.toString());
rethrow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment