Ask your own question, for FREE!
Computer Science 6 Online
OpenStudy (walters):

Hi help please php,javascript and html Is there any way of taking screen shot of any part of the interface but not the whole window and save it as an image in a file.

OpenStudy (cybershadow):

If you are talking about taking the screenshot of a part of a webpage , then yes it is possible.....but if you are talking abt taking the screenshot of user's desktop/wholescreen ,using php and html then No. Screenshot of a webpage could be taken in multiple ways , but the most suitable way in my opinion would be to use , \(\href{http:///html2canvas.hertzen.com/documentation.html}{html2canvas}\) library . Example: Suppose we want to take screenshot of div with id "test" Html Code:- <body> <div id='test'>Take My screenshot</div><p>Some other part of html</p></body> Javascript Code: var element = document.getElementById('test'); html2canvas(element, { onrendered: function(canvas) { //rendered canvas var download = document.createElement('a')//creating link for downloading image download.href =canvas.toDataURL();//use canvas.toDataURL('image/jpeg') for jpg file download.download = true; document.appendChild(download); } });

OpenStudy (walters):

so its that all can i test it now to see the output? or i still have to do extra things

OpenStudy (cybershadow):

That is all, You just have to include html2canvas.js in ur external javascript libraries

OpenStudy (walters):

maybe thats why

OpenStudy (walters):

error on property"canvas let me redo it"

OpenStudy (cybershadow):

here is the working/complete version of the example..... http://pastebin.com/wKqtSmmL

OpenStudy (walters):

<!DOCTYPE HTML> <html> <head> <title>screenshot</title> <link rel="stylesheet" type="text/css" href="my.css"> <script type="text/javascript" src="js/html2canvas.js"></script> <!-- --> </head> <body> <div id='test'>Take My screenshot</div><p>Some other part of html</p> <script> var element = document.getElementById('test'); html2canvas(element, { onrendered: function(canvas) { //rendered canvas var download = document.createElement('a')//creating link for downloading image download.href =canvas.toDataURL();//use canvas.toDataURL('image/jpeg') for jpg file download.download = true; document.appendChild(download); } }); </script> </body> </html>

OpenStudy (walters):

this is the error i am getting check the attachment

OpenStudy (cybershadow):

have you tried using this code ?....... http://pastebin.com/wKqtSmmL <----

OpenStudy (cybershadow):

That error is shown because html2canvas.js isnt downloaded on your system.... you can use cdn version tho....and code given in http://pastebin.com/wKqtSmmL should work fine.What are you getting using that code?

OpenStudy (walters):

it actually outputs the exact code i 'va copied ,is the a way to embed the button which i will click it to take screen shot.but it is working tho.

OpenStudy (cybershadow):

So image is being downloaded when u click on button?or is it giving error?

OpenStudy (walters):

i can say it the image but it output the code as an image so i don't know how am i going to put it on my code

OpenStudy (walters):

THIS IS ACTUALLY THE OUTPUT I GET <!DOCTYPE HTML> <html> <head> <script src=" http://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js "></script> </head> <body> <div id='test'>Take My screenshot</div> <p>Some other part of html</p> <button onclick ="ScreenShot()">Click To Generate ScreenShot</button> <script> function ScreenShot(){ var element = document.getElementById('test'); html2canvas(element, { onrendered: function(canvas) { //rendered canvas var download = document.createElement('a')//creating link for downloading image download.href =canvas.toDataURL();//use canvas.toDataURL('image/jpeg') for jpg file download.download = true; download.text = 'Download'; document.body.appendChild(download); ///REGIN } }); } </script> </body> </html>

OpenStudy (cybershadow):

can you upload the image which is being downloaded?

OpenStudy (walters):

let me look for it ..

OpenStudy (walters):

no image being doawnloaded the output is just the code .

OpenStudy (anonymous):

Hey. i have always wanted to use screen shots in my websites. i just tested and followed the code above, it out puts the code exactly as is.

OpenStudy (walters):

ok i've put it on my local host this is what i get now

OpenStudy (cybershadow):

Yes thats the screenshot of your div...... it is working , If you want to open the image instead of downloading it , just change "download.download = true;" to "download.download =false;" in your code.

OpenStudy (anonymous):

its working now, thank you alot :)

OpenStudy (walters):

ok it is fine thanks :)

OpenStudy (cybershadow):

no worries

OpenStudy (anonymous):

Have you tried Snipping Tool?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!