My Java application takes a screenshot of the screen and then displays it. Normally this would be a funny prank where a user would click around and there would be no response as they're trying to interact with an image.
However, if the user has Display scaling set on Windows, to 125% for example (pretty much required for larger monitors otherwise your text/icons will be very small), then when I use a JFrame to display their 1920x1080 BufferedImage, it is instead displayed at 2400x1350 as a blurry pixelated mess. Obviously the prank would not work in this case.
How can I make this work as intended regardless of what a user has set for their display scaling?
Code:
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.WinDef.HWND;
public class Test {
public static void main(String[] args) throws Exception {
BufferedImage capture = captureNotepadWindow();
int width = capture.getWidth();
int height = capture.getHeight();
System.out.println(width + "," + height);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JLabel(new ImageIcon(capture)));
frame.setPreferredSize(new Dimension(width, height));
frame.setUndecorated(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static BufferedImage captureNotepadWindow() throws Exception {
// Find notepad window handle
HWND notepadHwnd = User32.INSTANCE.FindWindow("Notepad", null);
if (notepadHwnd == null) {
throw new Exception("Notepad window not found");
}
return GDI32Util.getScreenshot(notepadHwnd);
}
}
Did not work, result is a pixelated upscaled image. I also tried to scale the image down by 1/1.25 such that the scaling by Windows would return it to the original size, but the image was very pixelated due to all the scaling.
displayImagenor how that variable is assigned a value.