为了在 Java 中创建屏幕截图,我一直在使用 java.awt.Robot
类的 createScreenCapture()
方法。但我只能创建矩形形状的屏幕截图。现在我的问题是有没有办法通过使用 Robot
类或其他显式代码来截取自定义形状的屏幕截图?
顺便说一句,对于自定义形状,屏幕截图必须是透明的,我可能会将其存储为 PNG 格式。
任何答案都值得赞赏。
最佳答案
is there any way to take a screenshot of custom shape either by using the Robot class or some other explicit code?
我喜欢 Andrew Thompson 的解决方案,它展示了如何从矩形图像创建成形图像。请参阅Cut Out Image in Shape of Text 。
您可以对任何形状执行此操作。例如,您可以通过执行以下操作来创建自己的多边形:
Polygon polygon = new Polygon();
polygon.addPoint(250, 50);
polygon.addPoint(350, 50);
polygon.addPoint(450, 150);
polygon.addPoint(350, 150);
g.setClip(polygon);
g.drawImage(originalImage, 0, 0, null);
关于java - 如何截取自定义形状的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17765485/