Export Canvas to Image Without rendering it

Flutter tips Published on

Why Export Canvas Without Rendering?

Sometimes, you might want to create an image from your custom drawing logic or a widget without actually showing it on the screen. This is useful for generating thumbnails, sharing images in the background, or saving artwork.

Flutter provides a way to do this using ui.PictureRecorder and Canvas.

How to Export Your Canvas

You can capture the drawing commands you make on a Canvas using a ui.PictureRecorder. This recorder then gives you a ui.Picture which can be converted into a ui.Image.

The Process

  1. Create a ui.PictureRecorder.
  2. Create a Canvas using the recorder.
  3. Apply the necessary transformations (like scaling for different device pixel ratios).
  4. Use your CustomPainter (or direct drawing commands) to paint onto this canvas.
  5. End the recording using recorder.endRecording() to get a ui.Picture.
  6. Convert the ui.Picture to a ui.Image using picture.toImage(width, height).
  7. Convert the ui.Image into a format like PNG (Uint8List) using image.toByteData(format: ui.ImageByteFormat.png).
  8. Get the raw byte data.

Code Example

Future<Uint8List> toImage(BuildContext context, Size size) async {
  final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
  final recorder = ui.PictureRecorder();
  final canvas = Canvas(recorder);

  // important scale the canvas according to pixel ratio
  canvas.scale(devicePixelRatio);

  final painter = MyPainter(); // <-- this is your customPainter
  painter.paint(canvas, size);

  // export with according size
  final width = (size.width * devicePixelRatio).floor();
  final height = (size.height * devicePixelRatio).floor();

  // transform into a ui.image
  final imageResult = recorder.endRecording().toImage(width, height);

  // transform into a regular png byte array
  final byteData = await imageResult.toByteData(
    format: ui.ImageByteFormat.png,
  );

  return byteData!.buffer.asUint8List();
}

// PS: your canvas has to paint some things.
// Create your own CustomPainter first (MyPainter here)

This function gives you the image data as a list of bytes (Uint8List), ready to be saved to a file, uploaded, or processed further.

Important Note

You must have drawing logic to paint on the canvas. In the example above, this is done by a MyPainter (which you need to create yourself). The CustomPainter's paint method will receive the canvas and size to draw upon.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is a ui.PictureRecorder?

A `PictureRecorder` is an object that captures a sequence of drawing commands made on a `Canvas`. Instead of drawing directly to the screen, it records what you tell the canvas to draw.

Why would I export a Canvas without rendering?

You might do this to create images in the background, generate image data for sharing, create thumbnails, or save artwork without showing the drawing process to the user on screen.

Do I need a CustomPainter to use this method?

Yes, you need some code to perform the actual drawing commands on the `Canvas`. A `CustomPainter` is a common and organized way to define your drawing logic, but you could technically use direct `canvas` methods as well.

What is the devicePixelRatio used for?

The `devicePixelRatio` is used to ensure the exported image has the correct resolution for the device's screen density. Scaling the canvas and the final image size by this ratio makes the image look sharp on high-resolution displays.

Read more
You may also be interested in
Easy access to a widget within test  blog card image
Easy access to a widget within test
Published on 2025-05-12T12:42:41.330Z
Discovering OverflowBar  blog card image
Discovering OverflowBar
Published on 2025-05-12T12:42:24.004Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved