我找到了很多将图像转换为 Graphics2d 的方法,但我不确定如何将我的 Graphics2d 转换为 BufferedImage。我该怎么做。
例如:
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?
基本上,你不能。您需要从 BufferedImage
开始并涂上它的Graphics2D
上下文,例如...
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();
很棒的是,AWT 和 Swing API 围绕使用 Graphics
工作。 (Graphics2D
延伸自),使得将组件绘制到 BufferedImage
变得非常容易
关于java - Graphics2D 到 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29335588/