toPngImage method

Future<PngImage?> toPngImage()

Implementation

Future<PngImage?> toPngImage() async {
  Completer<PngImage> completer = Completer();

  final stream = image.resolve(ImageConfiguration.empty);

  stream.addListener(ImageStreamListener((info, syncCall) async {
    int width = info.image.width;
    int height = info.image.height;
    ByteData? bytes =
        await info.image.toByteData(format: ImageByteFormat.png);
    info.dispose();
    if (bytes == null) {
      completer.completeError("Could not convert image to png format.");
      return;
    }
    final pngImage = PngImage.from(bytes, width: width, height: height);
    completer.complete(pngImage);
  }));

  return completer.future;
}