title: How to crop and resize JavaFX Image?
body: <p>I'm trying to display very large images on JavaFX canvas. Resolution of a single image is 11980x8365.
  Each image has a corresponding world file and I can use it to position images correctly.
  My canvas size is 800x600. Sometimes I need to write whole image on the canvas, and sometimes just a part of it.</p>
  
  <p>Here is what I've done so far:</p>
  
  <ul>
  <li>Load full sized image from a file into the Image object. </li>
  <li>Calculate which part of Image to show and calculate scale
  parameters to correctly fit it into 800x600 canvas.</li>
  </ul>
  
  <p>So basically I wanted to use <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/canvas/GraphicsContext.html#drawImage(javafx.scene.image.Image,%20double,%20double,%20double,%20double,%20double,%20double,%20double,%20double)" rel="nofollow noreferrer">GraphicsContext.drawImage(...)</a> - Draws the current source rectangle of the given image to the given destination rectangle of the Canvas.</p>
  
  <p>For this method I calculated all parameters correctly. Problem is that sometimes Image is larger than 2048x2048, and for some reason JavaFX tries to draw this image directly to the canvas using GPU (if I understood that correctly). That's when I get exception:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.sun.prism.sw.SWGraphics.drawTexture(SWGraphics.java:686) at com.sun.prism.sw.SWGraphics.drawTexture(SWGraphics.java:686)
      at com.sun.prism.sw.SWGraphics.drawTexture(SWGraphics.java:665)
      at com.sun.prism.sw.SWGraphics.drawTexture(SWGraphics.java:648)
      at com.sun.javafx.sg.prism.NGCanvas.handleRenderOp(NGCanvas.java:1228)
      at com.sun.javafx.sg.prism.NGCanvas.renderStream(NGCanvas.java:997)
      at com.sun.javafx.sg.prism.NGCanvas.renderContent(NGCanvas.java:578)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2282)
      at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2176)
      at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2202)
      at com.sun.javafx.sg.prism.CacheFilter.impl_renderNodeToCache(CacheFilter.java:655)
      at com.sun.javafx.sg.prism.CacheFilter.render(CacheFilter.java:561)
      at com.sun.javafx.sg.prism.NGNode.renderCached(NGNode.java:2346)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2034)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2282)
      at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2176)
      at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2202)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2037)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2282)
      at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2176)
      at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2202)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2037)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:225)
      at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:575)
      at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2043)
      at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1951)
      at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:469)
      at com.sun.javafx.tk.quantum.ViewPainter.paintImpl(ViewPainter.java:317)
      at com.sun.javafx.tk.quantum.UploadingPainter.run(UploadingPainter.java:132)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
      at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:129)
      at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>So what I want to try next is to crop and scale Image in some temporary object before sending it to canvas. I can't find anywhere example how to do that. Only example I found is how to <a href="https://stackoverflow.com/questions/14802374/effective-image-cropping-in-javafx">crop Image using WritableImage</a>, but I don't know how to scale it after cropping and convert it to Image.</p>