readImageData function

Future<List<int>> readImageData(
  1. GlobalKey<SfCartesianChartState>? globalKey
)

Read image data with background color

Implementation

Future<List<int>> readImageData(
    GlobalKey<SfCartesianChartState>? globalKey) async {
  final dart_ui.Image? chartImage =
      await globalKey!.currentState!.toImage(pixelRatio: 3.0);

  if (chartImage == null) {
    throw Exception("Failed to capture chart image.");
  }

  final recorder = dart_ui.PictureRecorder();
  final canvas = Canvas(recorder);

  final paint = Paint()..color = Colors.white;
  canvas.drawRect(
      Rect.fromLTWH(
          0, 0, chartImage.width.toDouble(), chartImage.height.toDouble()),
      paint);

  // Draw the chart image on top of background
  canvas.drawImage(chartImage, Offset.zero, Paint());

  final picture = recorder.endRecording();
  final dart_ui.Image finalImage =
      await picture.toImage(chartImage.width, chartImage.height);

  final ByteData? bytes =
      await finalImage.toByteData(format: dart_ui.ImageByteFormat.png);

  return bytes!.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
}