createImageSync method

Image createImageSync([
  1. bool adjustOffset = true,
  2. double resolution = 1
])

Creates an Image object from the rendered image of the object synchronously. You can optionally provide a resolution value, which defaults to 1.

Implementation

Image createImageSync([
  bool adjustOffset = true,
  double resolution = 1,
]) {
  var rect = getBounds();
  if (resolution != 1) {
    rect *= resolution;
  }
  final needsAdjust =
      (rect.x != 0 || rect.y != 0) && adjustOffset || resolution != 1;
  final picture = createPicture(
    !needsAdjust
        ? null
        : (c) {
            if (adjustOffset) {
              c.translate(-rect.left, -rect.top);
            }
            if (resolution != 1) {
              c.scale(resolution);
            }
          },
  );
  final width = adjustOffset ? rect.width.toInt() : rect.right.toInt();
  final height = adjustOffset ? rect.height.toInt() : rect.bottom.toInt();
  final output = picture.toImageSync(width, height);
  picture.dispose();
  return output;
}