private void marshallImage(BpmnMemoryModel model, String modelFileName) {
    try {
      final GraphicalViewer graphicalViewer =
          (GraphicalViewer)
              ((DiagramEditor)
                      model.getFeatureProvider().getDiagramTypeProvider().getDiagramEditor())
                  .getAdapter(GraphicalViewer.class);

      if (graphicalViewer == null || graphicalViewer.getEditPartRegistry() == null) {
        return;
      }
      final ScalableFreeformRootEditPart rootEditPart =
          (ScalableFreeformRootEditPart) graphicalViewer.getEditPartRegistry().get(LayerManager.ID);
      final IFigure rootFigure =
          ((LayerManager) rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);
      final IFigure gridFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.GRID_LAYER);
      final Rectangle rootFigureBounds = rootFigure.getBounds();

      final boolean toggleRequired = gridFigure.isShowing();

      final Display display = Display.getDefault();

      final Image img = new Image(display, rootFigureBounds.width, rootFigureBounds.height);
      final GC imageGC = new GC(img);
      final SWTGraphics grap = new SWTGraphics(imageGC);

      // Access UI thread from runnable to print the canvas to the image
      display.syncExec(
          new Runnable() {

            @Override
            public void run() {
              if (toggleRequired) {
                // Disable any grids temporarily
                gridFigure.setVisible(false);
              }
              // Deselect any selections
              graphicalViewer.deselectAll();
              rootFigure.paint(grap);
            }
          });

      ImageLoader imgLoader = new ImageLoader();
      imgLoader.data = new ImageData[] {img.getImageData()};

      ByteArrayOutputStream baos = new ByteArrayOutputStream(imgLoader.data.length);

      imgLoader.save(baos, SWT.IMAGE_PNG);

      imageGC.dispose();
      img.dispose();

      // Access UI thread from runnable
      display.syncExec(
          new Runnable() {

            @Override
            public void run() {
              if (toggleRequired) {
                // Re-enable any grids
                gridFigure.setVisible(true);
              }
            }
          });

      String imageFileName = null;
      if (modelFileName.endsWith(".bpmn20.xml")) {
        imageFileName = modelFileName.substring(0, modelFileName.length() - 11) + ".png";
      } else {
        imageFileName = modelFileName.substring(0, modelFileName.lastIndexOf(".")) + ".png";
      }
      File imageFile = new File(imageFileName);
      FileOutputStream outStream = new FileOutputStream(imageFile);
      baos.writeTo(outStream);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }