问题描述
为了在 Java 中创建屏幕截图,我一直在使用 java.awt.Robot
类的 createScreenCapture()
方法.但我只能在 Rectangle
形状中创建屏幕截图.现在我的问题是有没有办法通过使用 Robot
类或其他一些显式代码来截取自定义形状的屏幕截图?
For creating screenshots in Java i have been using the java.awt.Robot
class' createScreenCapture()
method. But i was only able to create screenshots in the Rectangle
shape. Now my question is is there any way to take a screenshot of custom shape either by using the Robot
class or some other explicit code?
顺便说一下,对于自定义形状,屏幕截图必须是透明的,我可能会将其存储为 PNG 格式.
And by the way, for custom shape the screenshot has to be transparent and i am likely to store it in PNG format.
感谢任何答案.
推荐答案
我喜欢 Andrew Thompson 的解决方案,该解决方案展示了如何从矩形图像创建成形图像.请参阅以文本形状剪切图像.
I like Andrew Thompson's solution that shows how to create a shaped image from a rectangular image. See Cut Out Image in Shape of Text.
您可以使用任何形状进行此操作.例如,您可以通过执行以下操作来创建自己的多边形:
You can do this with any shape. For example you can create your own Polygon by doing something like:
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);
这篇关于如何拍摄自定义形状的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!