China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-28 11:57
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Implement screenshot to system clipboard or Paint using VBS script View 4,322 Replies 22
Original Poster Posted 2007-06-16 21:48 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
This way, integrating short third-party tools into a VBS script can achieve more abundant functions.

This post can also serve as a general method for embedding binary code in VBS.


data = "4D5A90000300000004000000FFFF0000B8000000000000004000000000000000000000000000000000000000000000000000000000000000000000C00000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000450E6EDA016F0089016F0089016F0089016F0189006F008963701389026F0089E9700B89006F008952696368016F008900000000000000000000000000000000504500004C01010017E773460000000000000000E0000F010B01060000000000000400000000000080110000001000000010000000004000001000000002000004000000000000000400000000000000002000000002000000000000020000000000100000100000000010000010000000000000100000000000000000000000F012000028000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000080000000000000000000000000000000000000000000000000000002E646174610000003A030000001000000004000000020000000000000000000000000000400000C0000000000000000000000000000000000000000000000000000000000000000020130000000000004D657373616765426F784100476574436F6D6D616E644C696E6541006B657962645F6576656E74004578697450726F6365737300467265654C696272617279004C6F61644C6962726172794100000000757365723332000053637265656E4361746368204279205368696C7978000000B3C9B9A6BDABD7A5CDBCB1A3B4E6B5BDCFB5CDB3BCF4CCF9B0E5000000000000000000000000000083EC0C535556578B7C24208B473C03C78138504500000F858E0000008B48788B44392003CF03C7894C2418C7442420000000008B491885C9894C2414766C8B6C2424894424248A4D008BD181E2FF00000089542410EB048B5424108B008BCD8D343833C08A062BC275248A55002BF584D274178A51014133C08BDA8A040E81E3FF0000002BC374E7EB0485C074268B4C24208B4424248B5424144183C0043BCA894C24208944242472AD5F5E5D33C05B83C40CC38B4424188B5424208B48248B401C8D0C5133D2668B14398D0C908B043903C75F5E5D5B83C40CC39090909090558BEC81EC48010000535657B906000000BE781040008D7DBCF3A566A5A4B905000000BE601040008D7DD8A158104000F3A5668B0D5C1040008A155E10400066A58945F466894DF88855FAC745FC0000000064A1180000008945FC8B45FC68481040008B48308B510C8B420C8B088B118B721856E8A7FEFFFF683C104000568BF8E89AFEFFFF6830104000568945FCE88CFEFFFF83C4188945F08D45F450FFD78BD8682410400053E873FEFFFF8B3D0010400083C4086A006A006A006A2CFFD76A006A026A006A2CFFD7681410400056E84BFEFFFF8BD0B94000000033C08DBDB9FEFFFFC685B8FEFFFF0083C408F3AB66ABAAFFD28BF883C9FF33C08D95B8FEFFFFF2AEF7D12BF98BC18BF78BFAC1E902F3A58BC833C083E103F3A48DBDB8FEFFFF83C9FFF2AEF7D1498A8C0DB7FEFFFF80F973742180F953741C680810400053E8DAFDFFFF83C4088D4DD88D55BC6A4051526A00FFD053FF55FC6A00FF55F05F5E5B8BE55DC39090909090909090901813000000000000000000002E1300000010000000000000000000000000000000000000000000002013000000000000AA026B657962645F6576656E74005553455233322E646C6C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

TargetFileName = "C:\tmp.exe"

With CreateObject("ADODB.Stream"):.Type = 1:.Open:.Write StrToByte(data):.SaveToFile TargetFileName, 2:.Close:End With

WScript.CreateObject("WScript.Shell").Run TargetFileName & " /s"
MsgBox "The current screen image has been copied to the system clipboard", 64, "Information"

Function StrToByte(str)
Set xmldoc = CreateObject("Microsoft.XMLDOM")
xmldoc.loadXML "<?xml version=""1.0""?>"
Set pic = xmldoc.createElement("pic")
pic.dataType = "bin.hex"
pic.nodeTypedValue = str
StrToByte = pic.nodeTypedValue
End Function


The code from the twelfth floor adds support for sending to Paint

[ Last edited by my3439955 on 2007-6-18 at 07:12 PM ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
lxmxn +8 2007-06-16 21:52
wudixin96 +8 2007-06-16 22:04
zh159 +9 2007-06-16 23:24
Floor 2 Posted 2007-06-16 21:51 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
This is pretty good, give some extra points to encourage.
Floor 3 Posted 2007-06-16 22:01 ·  中国 浙江 杭州 联通
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Also use XML. Not bad. Add points, :-)
Floor 4 Posted 2007-06-16 22:05 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
Otherwise, you can't write binary files.
Floor 5 Posted 2007-06-16 23:04 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
The larger the binary file, the more space it seems to save when converted by baomaboy's "Convert to Batch File" (small files mainly have more script writing parts)

LZ's is 3.57k, this one is 3.1k
bs=_
"NpFkAMAAAAABAAAA//PAAgLAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAA4wH66AA0mQzhgbAM1cIUhWazBCcy92ZyFWbgMWYu52b0BiYlBic15GIp5GIE90Ug02bkVmLN0gCkAAAAAAAAAQR"+_
"O4m2B8GAJGwbAkYAvBQiB8WAJCwbAk4YwNRiC8GAJmOcLkIAvBQiSl2YoFwbAkIAAAAAAAAAAAAAAAAAAAAAQVEAAwUABAwFnPnRAAAAAAAAAAA4A8QALEgBAAAAAAAAEAAAAA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p+///oxDEABgVLiP6a6///jGM"+_
"QAEAWlYR8jOj+///DSMGJWE8NWE9Q9/1LiNakABQAMF6z5///vYPAABQAMIxIoGAqBgaAoGL/ftaAomAqBgas8/1oRBEABgVovk////iQnLQAAAAzAcj9mr///vxFir///PADSMCzvqZrq6/SvI+Dm8/zAcjVir///v8uef0rk/i"+_
"Bv49LqfwpLw8luIyzA8ghPw8k2Yv46///PYy/Lvr3HdSKyYD36///DY+zRXIAm/U0xBaIABQAMF6a3///PIxI0YTY3YV8qGQRJlaA8P0T9fV8rGA/XF8f51WLWeXDDJkQCJkQCJkQixEAAAAAAAAAAAAA4yEAAAAQAAAAAAAAAAA"+_
"AAAAAAAAAAAAAAAAAAAITAAAAAAAAoqArVWeiR2XlZXZuRHAVNVRSNjMuQGbsBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+_
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+_
"":set rs=CreateObject("ADODB.Recordset")
set ado=CreateObject("ADODB.Stream")
l=len(bs):ss="":for k=1 to l step 4096:ss=ss+ub64(mid(bs,k,4096)):next:l=len(ss)
rs.fields.append "b",205,l/2:rs.open:rs.addnew:rs("b")=ss+chrb(0):rs.update
ado.mode=3:ado.type=1:ado.open:ado.write rs("b").getchunk(l/2)
ado.savetofile "tmp.exe",2:ado.close

WScript.CreateObject("WScript.Shell").Run "tmp.exe /s"
MsgBox "The current screen image has been copied to the system clipboard", 64, "Information"

function ub64(s):dim t(4),b(3):ub64="":n=len(s):r=2
if n mod 4<>0 then exit function:end if:for i=1 to n step 4:for j=0 to 3
a=asc(mid(s,i+j,1)):if a=43 then:a=62:else if a=47 then:a=63:else if a>47 and a<58 then:_
a=a+4:else if a=61 then:a=0:if r=2 then r=j-2:end if:else if a>64 and a<91 then:_
a=a-65:else if a>96 and a<123 then:a=a-71:else:exit function:_
end if:end if:end if:end if:end if:end if:t(j)=a:next
b(0)=t(0)+t(1)*64 mod 256:b(1)=t(1)\4+t(2)*16 mod 256:b(2)=t(2)\16+t(3)*4
for j=0 to r:if b(j)<16 then ub64=ub64+"0":end if:ub64=ub64+hex(b(j))
next:next:end function


[ Last edited by zh159 on 2007-6-16 at 11:05 PM ]
Floor 6 Posted 2007-06-16 23:06 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
Right. This one uses a much larger character set than mine. Therefore, it must have an advantage in terms of compression ratio.
Floor 7 Posted 2007-06-16 23:14 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
The advantage of this is that you can directly write hexadecimal data into a bin; I just used it before, converting GB characters to hexadecimal UTF-8 encoding and writing it as a BIN file
Floor 8 Posted 2007-06-16 23:39 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 906
Posts 346
Joined 2006-07-10 09:58
19-year member
UID 58334
Gender Male
Status Offline
Great, after asking for so long, there's finally a result. Can we directly send the captured image to Paint? That would be much more convenient. I wonder if the requirement is too excessive, heh.
Floor 9 Posted 2007-06-16 23:49 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 906
Posts 346
Joined 2006-07-10 09:58
19-year member
UID 58334
Gender Male
Status Offline
Isn't it good to add another line of code to delete the generated temporary files? Just generate them when needed.
Floor 10 Posted 2007-06-16 23:53 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
This is a program written purely with APIs. It's already not easy to be as small as 1.5K. If it needs to support image saving, the program will be extremely complicated. Using class libraries is simpler, but the target program is then very large. It's a dilemma.
Floor 11 Posted 2007-06-16 23:57 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
Originally posted by eech at 2007-6-16 23:49:
Is it not good to add another sentence to delete the code that generates temporary files? Just generate it when needed.


Hmm
That's pretty good
But many times that program will pop up a notification dialog box
The program is resident in memory
It cannot be deleted
Floor 12 Posted 2007-06-17 00:44 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
Now it can support sending to Paint.

The Exe file generated by the following script with the /c parameter can achieve the function of sending to Paint.


Data = "4D5A90000300000004000000FFFF0000B800000000000000400000000000000000000000000000000000000000000000000000000000000000000000C00000000E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F742062652072756E20696E20444F53206D6F64652E0D0D0A2400000000000000450E6EDA016F0089016F0089016F0089016F0189006F008963701389026F0089E9700B89006F008952696368016F008900000000000000000000000000000000504500004C010100E61174460000000000000000E0000F010B010600000000000004000000000000C0110000001000000010000000004000001000000002000004000000000000000400000000000000002000000002000000000000020000000000100000100000000010000010000000000000100000000000000000000000B013000028000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000080000000000000000000000000000000000000000000000000000002E64617461000000FA030000001000000004000000020000000000000000000000000000400000C00000000000000000000000000000000000000000000000000000000000000000E0130000000000006D737061696E74004D535061696E744170700000536C65657000000057696E4578656300506F73744D657373616765410000000046696E6457696E646F7741004D657373616765426F784100476574436F6D6D616E644C696E6541006B657962645F6576656E74004578697450726F6365737300467265654C696272617279004C6F61644C6962726172794100000000757365723332000053637265656E4361746368204279205368696C7978000000B3C9B9A6BDABD7A5CDBCB1A3B4E6B5BDCFB5CDB3BCF4CCF9B0E5000000000000000000000000000083EC0C535556578B7C24208B473C03C78138504500000F858E0000008B48788B44392003CF03C7894C2418C7442420000000008B491885C9894C2414766C8B6C2424894424248A4D008BD181E2FF00000089542410EB048B5424108B008BCD8D343833C08A062BC275248A55002BF584D274178A51014133C08BDA8A040E81E3FF0000002BC374E7EB0485C074268B4C24208B4424248B5424144183C0043BCA894C24208944242472AD5F5E5D33C05B83C40CC38B4424188B5424208B48248B401C8D0C5133D2668B14398D0C908B043903C75F5E5D5B83C40CC39090909090558BEC83EC50535657B906000000BEB81040008D7DB0F3A566A5A4B905000000BEA01040008D7DCCA198104000F3A5668B0D9C1040008A159E10400066A58945E466894DE88855EAC745FC0000000064A1180000008945FC8B45FC68881040008B48308B510C8B420C8B088B118B5A1853E8AAFEFFFF687C104000538BF0E89DFEFFFF6870104000538945F0E88FFEFFFF83C4188945EC8D45E450FFD66864104000508945FCE875FEFFFF8B350010400083C4086A006A006A006A2CFFD66A006A026A006A2CFFD6685410400053E84DFEFFFF33C983C408894DF58BF0C645F40066894DF9FFD68BF883C9FF33C0F2AEF7D1498BF9FFD68D7C07FB8D55F483C9FF33C0F2AEF7D12BF98BC18BF78BFAC1E902F3A58BC833C083E103F3A48D7DF483C9FFF2AEF7D1498A4C0DF380F963743880F943743380F9730F849C00000080F9530F84930000008B4DFC684810400051E8CAFDFFFF83C4088D55CC8D4DB06A4052516A00FFD0EB728B7DFC683C10400057E8A9FDFFFF682C104000578BF0E89CFDFFFF6824104000538945F4E88EFDFFFF681C104000538BF8E881FDFFFF83C4208BD86A006810104000FFD685C0751A6A016808104000FFD76A0AFFD36A006810104000FFD685C074EF6A006825E10000681101000050FF55F48B55FC52FF55F06A00FF55EC5F5E5B8BE55DC39090D81300000000000000000000EE130000001000000000000000000000000000000000000000000000E013000000000000AA026B657962645F6576656E74005553455233322E646C6C0000000000"

With CreateObject("ADODB.Stream"):.Type = 1:.Open:.Write StrToByte(data):.SaveToFile GetTargetFileName, 2:.Close:End With

Function GetTargetFileName
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "All Files|*.*"
objDialog.InitialDir = "."
If objDialog.ShowOpen = 0 Then
WScript.Quit
Else
GetTargetFileName = objDialog.FileName
End If
Set objDialog = Nothing
End Function

Function StrToByte(str)
Set xmldoc = CreateObject("Microsoft.XMLDOM")
xmldoc.loadXML "<?xml version=""1.0""?>"
Set pic = xmldoc.createElement("pic")
pic.dataType = "bin.hex"
pic.nodeTypedValue = str
StrToByte = pic.nodeTypedValue
End Function


View how to use this type of VBS script

[ Last edited by my3439955 on 2007-6-17 at 06:49 PM ]
Floor 13 Posted 2007-06-17 01:15 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Modify it can delete temporary files
WScript.CreateObject("WScript.Shell").Run TargetFileName & " /s",,true
WScript.CreateObject("WScript.Shell").Run "cmd /c del " &TargetFileName,vbhid
MsgBox "The current screen image has been copied to the system clipboard", 64, "Information"
Floor 14 Posted 2007-06-17 11:54 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
Could it be that the program has already been deleted before it had the chance to start?
Floor 15 Posted 2007-06-17 13:51 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by my3439955 at 2007-6-17 11:54:
Could the program have been deleted before it had a chance to start?

I've tried it and can capture the screen. You can also give it a try.
The key is WScript.CreateObject("WScript.Shell").Run TargetFileName & " /s",,true. Using true ensures that the next step is entered only after the program being run finishes execution.
PS: Two commas are needed because the first comma segment can increase the use of vbhid for hidden running.
Forum Jump: