paintImage function

void paintImage (
  1. {@required Canvas canvas,
  2. @required Rect rect,
  3. @required Image image,
  4. String debugImageLabel,
  5. double scale: 1.0,
  6. ColorFilter colorFilter,
  7. BoxFit fit,
  8. Alignment alignment:,
  9. Rect centerSlice,
  10. ImageRepeat repeat: ImageRepeat.noRepeat,
  11. bool flipHorizontally: false,
  12. bool invertColors: false,
  13. FilterQuality filterQuality: FilterQuality.low,
  14. bool isAntiAlias: false}

Paints an image into the given rectangle on the canvas.

The arguments have the following meanings:

  • canvas: The canvas onto which the image will be painted.

  • rect: The region of the canvas into which the image will be painted. The image might not fill the entire rectangle (e.g., depending on the fit). If rect is empty, nothing is painted.

  • image: The image to paint onto the canvas.

  • scale: The number of image pixels for each logical pixel.

  • colorFilter: If non-null, the color filter to apply when painting the image.

  • fit: How the image should be inscribed into rect. If null, the default behavior depends on centerSlice. If centerSlice is also null, the default behavior is BoxFit.scaleDown. If centerSlice is non-null, the default behavior is BoxFit.fill. See BoxFit for details.

  • alignment: How the destination rectangle defined by applying fit is aligned within rect. For example, if fit is BoxFit.contain and alignment is Alignment.bottomRight, the image will be as large as possible within rect and placed with its bottom right corner at the bottom right corner of rect. Defaults to

  • centerSlice: The image is drawn in nine portions described by splitting the image by drawing two horizontal lines and two vertical lines, where centerSlice describes the rectangle formed by the four points where these four lines intersect each other. (This forms a 3-by-3 grid of regions, the center region being described by centerSlice.) The four regions in the corners are drawn, without scaling, in the four corners of the destination rectangle defined by applying fit. The remaining five regions are drawn by stretching them to fit such that they exactly cover the destination rectangle while maintaining their relative positions.

  • repeat: If the image does not fill rect, whether and how the image should be repeated to fill rect. By default, the image is not repeated. See ImageRepeat for details.

  • flipHorizontally: Whether to flip the image horizontally. This is occasionally used with images in right-to-left environments, for images that were designed for left-to-right locales (or vice versa). Be careful, when using this, to not flip images with integral shadows, text, or other effects that will look incorrect when flipped.

  • invertColors: Inverting the colors of an image applies a new color filter to the paint. If there is another specified color filter, the invert will be applied after it. This is primarily used for implementing smart invert on iOS.

  • filterQuality: Use this to change the quality when scaling an image. Use the FilterQuality.low quality setting to scale the image, which corresponds to bilinear interpolation, rather than the default FilterQuality.none which corresponds to nearest-neighbor.

The canvas, rect, image, scale, alignment, repeat, flipHorizontally and filterQuality arguments must not be null.

See also:


void paintImage({
  @required Canvas canvas,
  @required Rect rect,
  @required ui.Image image,
  String debugImageLabel,
  double scale = 1.0,
  ColorFilter colorFilter,
  BoxFit fit,
  Alignment alignment =,
  Rect centerSlice,
  ImageRepeat repeat = ImageRepeat.noRepeat,
  bool flipHorizontally = false,
  bool invertColors = false,
  FilterQuality filterQuality = FilterQuality.low,
  bool isAntiAlias = false,
}) {
  assert(canvas != null);
  assert(image != null);
  assert(alignment != null);
  assert(repeat != null);
  assert(flipHorizontally != null);
  assert(isAntiAlias != null);
  if (rect.isEmpty)
  Size outputSize = rect.size;
  Size inputSize = Size(image.width.toDouble(), image.height.toDouble());
  Offset sliceBorder;
  if (centerSlice != null) {
    sliceBorder = Offset(
      centerSlice.left + inputSize.width - centerSlice.right, + inputSize.height - centerSlice.bottom,
    outputSize = outputSize - sliceBorder as Size;
    inputSize = inputSize - sliceBorder as Size;
  fit ??= centerSlice == null ? BoxFit.scaleDown : BoxFit.fill;
  assert(centerSlice == null || (fit != BoxFit.none && fit != BoxFit.cover));
  final FittedSizes fittedSizes = applyBoxFit(fit, inputSize / scale, outputSize);
  final Size sourceSize = fittedSizes.source * scale;
  Size destinationSize = fittedSizes.destination;
  if (centerSlice != null) {
    outputSize += sliceBorder;
    destinationSize += sliceBorder;
    // We don't have the ability to draw a subset of the image at the same time
    // as we apply a nine-patch stretch.
    assert(sourceSize == inputSize, 'centerSlice was used with a BoxFit that does not guarantee that the image is fully visible.');

  // Output size is fully calculated.
  if (!kReleaseMode) {
    final ImageSizeInfo sizeInfo = ImageSizeInfo(
      // Some ImageProvider implementations may not have given this.
      source: debugImageLabel ?? '<Unknown Image(${image.width}×${image.height})>',
      imageSize: Size(image.width.toDouble(), image.height.toDouble()),
      displaySize: outputSize,
    // Avoid emitting events that are the same as those emitted in the last frame.
    if (!_lastFrameImageSizeInfo.contains(sizeInfo)) {
      final ImageSizeInfo existingSizeInfo = _pendingImageSizeInfo[sizeInfo.source];
      if (existingSizeInfo == null || existingSizeInfo.displaySizeInBytes < sizeInfo.displaySizeInBytes) {
        _pendingImageSizeInfo[sizeInfo.source] = sizeInfo;
      // _pendingImageSizeInfo.add(sizeInfo);
      if (debugOnPaintImage != null) {
      SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) {
        _lastFrameImageSizeInfo = _pendingImageSizeInfo.values.toSet();
        if (_pendingImageSizeInfo.isEmpty) {
          <Object, Object>{
            for (ImageSizeInfo imageSizeInfo in _pendingImageSizeInfo.values)
              imageSizeInfo.source: imageSizeInfo.toJson()
        _pendingImageSizeInfo = <String, ImageSizeInfo>{};


  if (repeat != ImageRepeat.noRepeat && destinationSize == outputSize) {
    // There's no need to repeat the image because we're exactly filling the
    // output rect with the image.
    repeat = ImageRepeat.noRepeat;
  final Paint paint = Paint()..isAntiAlias = isAntiAlias;
  if (colorFilter != null)
    paint.colorFilter = colorFilter;
  if (sourceSize != destinationSize) {
    paint.filterQuality = filterQuality;
  paint.invertColors = invertColors;
  final double halfWidthDelta = (outputSize.width - destinationSize.width) / 2.0;
  final double halfHeightDelta = (outputSize.height - destinationSize.height) / 2.0;
  final double dx = halfWidthDelta + (flipHorizontally ? -alignment.x : alignment.x) * halfWidthDelta;
  final double dy = halfHeightDelta + alignment.y * halfHeightDelta;
  final Offset destinationPosition = rect.topLeft.translate(dx, dy);
  final Rect destinationRect = destinationPosition & destinationSize;
  final bool needSave = repeat != ImageRepeat.noRepeat || flipHorizontally;
  if (needSave);
  if (repeat != ImageRepeat.noRepeat)
  if (flipHorizontally) {
    final double dx = -(rect.left + rect.width / 2.0);
    canvas.translate(-dx, 0.0);
    canvas.scale(-1.0, 1.0);
    canvas.translate(dx, 0.0);
  if (centerSlice == null) {
    final Rect sourceRect = alignment.inscribe(
      sourceSize, & inputSize,
    if (repeat == ImageRepeat.noRepeat) {
      canvas.drawImageRect(image, sourceRect, destinationRect, paint);
    } else {
      for (final Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
        canvas.drawImageRect(image, sourceRect, tileRect, paint);
  } else {
    if (repeat == ImageRepeat.noRepeat) {
      canvas.drawImageNine(image, centerSlice, destinationRect, paint);
    } else {
      for (final Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
        canvas.drawImageNine(image, centerSlice, tileRect, paint);
  if (needSave)