如何将 java 组件渲染为高分辨率图像?

How to render java component to high resolution image?

我正在尝试提取应用程序 window 在 java 中的组件的图像。

我创建了一个覆盖 java.awt.GraphicsConfiguration 的 class,看起来与 sun.awt.Win32GraphicsConfig。问题是,当我使用组件(例如应用程序 window)调用 createAcceleratedImage 函数并且尺寸比实际尺寸大 2 倍时,java 将其渲染在左上角的图像中,而图片的其余部分是空的 space。

我的 createAcceleratedImage 函数与 sun.awt.Win32GraphicsConfig:

中的函数相同
public Image createAcceleratedImage(Component target, int w, int h) {
    ColorModel localColorModel = getColorModel(Transparency.OPAQUE);
    WritableRaster localWritableRaster = localColorModel.createCompatibleWritableRaster(w, h);
    return new OffScreenImage(target, localColorModel, localWritableRaster, localColorModel.isAlphaPremultiplied());
}

如果我忘记提及更多信息,我深表歉意,如果您需要更多信息,请在评论中询问。

感谢您的帮助。

我在 CustomComponentPeer 中使用了 VolatileImage 而不是 OffScreenImage。

public VolatileImage createVolatileImage(int width, int height) {
    GraphicsConfiguration graphicsConfig = getGraphicsConfiguration();
    return graphicsConfig.createCompatibleVolatileImage(width, height);
}

我发现 Graphics2D 中的 drawImage 函数仅支持 MultiResolutionImage 和 VolatileImage 的高分辨率渲染。不支持使用 OffScreenImage 以高 DPI 渲染。