Fast screen capture using DirectX in Java (rewriting existing C # code in Java)

My goal is to capture a screen using DirectX in Java . I found the project fully detailed and explained here in C #.

Unfortunately, I have no knowledge in C sharp. I don’t know if I can ask here to rewrite the code above mentioned from C # to Java for those who process both languages, but I think that the end result will be of interest to many people.

In any case, I thank those who would be kind to help me with this. Even if I have never tried, I know that C # β†’ Java conversion software (or any other language) is not recommended, explaining my rewriting question .

Please check out the code below:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX.Direct3D9;

namespace KMPP
{
public class DxScreenCapture
{
    Device d;

    public DxScreenCapture()
    {
        PresentParameters present_params = new PresentParameters();
        present_params.Windowed = true;
        present_params.SwapEffect = SwapEffect.Discard;
        d = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.SoftwareVertexProcessing, present_params);
    }

    public Surface CaptureScreen()
    {
        Surface s = Surface.CreateOffscreenPlain(d, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Format.A8R8G8B8, Pool.Scratch);
        d.GetFrontBufferData(0, s);
        return s;
    }
}
}

PS: Since Surface s is a DirectX type here, I would be interested to convert it to PNG then.

+5
source share
1 answer

Java is not C #

C # is completely different from java. C # can use DirectX, but java cannot use it, because DirectX is not available on all platforms. Instead, you can use the class Robotpresent in the package java.awt. Here's how to get it image.

Doing this with a robot

Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
Robot r = new Robot();
BufferedImage s = r.createScreenCapture(new Rectangle(ss));

It returns BufferedImagewhich contains a screenshot.

-1
source

All Articles