简体   繁体   中英

Graphics2D into BufferedImage

I have found many ways to turn a image into a Graphics2d, but I am not sure how to turn my Graphics2d into a BufferedImage. How would I do this.

For example:

Graphics2D SomeGraphics;
SomeGraphics.drawImage(image, x,y,size,size);
SomeGraphics.fillRect(x,y,size,size);

Now how would I convert that Graphics into a Buffered Image?

Now how would I convert that Graphics into a Buffered Image?

Essentially, you can't. You need to start with a BufferedImage and paint to it's Graphics2D context, for example...

BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(25, 25, 50, 50);
g2d.dispose();

The great thing is, the AWT and Swing API's work around the use of Graphics (which Graphics2D extends from), making it really easy to paint components to a BufferedImage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM