Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

screenshot - How to take screen shot of entire client machine not only web browser or a web page from javascript

I want to capture the screenshot to a image not the video. I found ways to do that but they are either capturing the current web page or recoding the video from entire screen. I found This library to record screen. This is doing something similar which WebRTC does. My requirement is to just take an image of entire screen programmatically from my web application written in plain javascript. Is there any way I can do it ? Thanks

question from:https://stackoverflow.com/questions/66058460/how-to-take-screen-shot-of-entire-client-machine-not-only-web-browser-or-a-web-p

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the MediaStream you get through the Media Capture and Streams API, you can create an ImageCapture instance and call its grabFrame() method that will produce an ImageBitmap you'll be able to paint on a <canvas>.

const stream = await navigator.mediaDevices.getDisplayMedia();
const track = stream.getVideoTracks()[0];
const capture = new ImageCapture(track);
// when you need the still image
const bitmap = await capture.grabFrame();
// if you want a Blob version
const canvas = document.createElement("canvas");
canvas.width = bitmap.width;
canvas.height = bitmap.height;
canvas.getContext("bitmaprenderer").transferFromImageBitmap(bitmap);
const blob = await new Promise((res) => canvas.toBlob(res));

Now, I should note that this ideal path is currently only accessible to Chromium based browsers.
For other browsers you'd need to set the srcObject of an HTMLVideoElement to the MediaStream, and to drawImage that HTMLVideoElement on a 2D context.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...