Unable to Restore Image

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Unable to Restore Image

Post by hagchr » Fri Oct 16, 2015 10:08 am

14.2.04 - Sep 2015

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Fri Oct 16, 2015 10:18 pm

Even upgrading to the latest version of Macro Scheduler, the code above in question (including Christer's) remains unable to restore a screen capture image after BASE64 processing.
Hope Marcus can help.
Thank you @hagchr.
Thank you Marcus.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Sat Oct 17, 2015 2:44 am

I still can't get my code working so far. Hope someone can help.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Sat Oct 17, 2015 2:47 am

hagchr wrote:Hi, I added one ScreenCapture> to the desktop. Both looks fine and with exact same size. Strange that it does not work on your machine.
Have you opened your resultant image, image.bmp, successfully?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Sat Oct 17, 2015 3:18 am

Still the code can't restore the original image after exhausting all possibilities:
1. Varying the screen capture sizes
2. UTF8 or UNICODE
3. .bmp or .jpg
4. Different folders
5. WLN_NOCRLF=1 or 0
6. Many more...
Problem: The resultant image after being processed by BASE64 can't be decoded. That's, it's corrupt.
MS07.jpg
Hope someone can help. Thank you.

Code: Select all

// Clean up all previously leftover test files
DeleteFile>C:\Temp\WS1_Image.txt
DeleteFile>C:\Temp\Test.jpg
DeleteFile>C:\Temp\Image.jpg
*Let>WLN_ENCODING=UTF8
*Let>WLN_ENCODING=UNICODE

// Capture the entire screen
GetScreenRes>Width,Height
*Let>Width=Width-1000
*Let>Height=Height-1000

ScreenCapture>0,0,Width,Height,C:\Temp\Test.jpg
// Store the screen capture as text file on disk
ReadFile>C:\Temp\Test.jpg,ImageData
Base64>ImageData,Encode,b64data
Let>WLN_NOCRLF=1
WriteLn>C:\Temp\WS1_Image.txt,result,b64data

// Retrieve the previously stored text file
ReadFile>C:\Temp\WS1_Image.txt,Binary
Base64>Binary,DECODE,B64Data
WriteLn>C:\Temp\Image.jpg,result,B64Data

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Sat Oct 17, 2015 5:03 am

Not sure why the file sizes of the original and the resultant image (.jpg) are different.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Sat Oct 17, 2015 5:59 am

The following modified code is to keep track of the size changes during the entire process.
I discover the file size of Image.jpg is significantly different than L3 (the length of the binary created by BASE64 decoding).
Why?

Code: Select all

// Clean up all previously leftover test files
DeleteFile>C:\Temp\WS1_Image.txt
DeleteFile>C:\Temp\Test.jpg
DeleteFile>C:\Temp\Image.jpg
*Let>WLN_ENCODING=UTF8
*Let>WLN_ENCODING=UNICODE

// Capture the entire screen
GetScreenRes>Width,Height
*Let>Width=Width-1000
*Let>Height=Height-1000

ScreenCapture>0,0,Width,Height,C:\Temp\Test.jpg
// Store the screen capture as text file on disk
ReadFile>C:\Temp\Test.jpg,ImageData
Base64>ImageData,Encode,b64Data
Len>b64data,L1

Let>WLN_NOCRLF=1
WriteLn>C:\Temp\WS1_Image.txt,result,b64data

// Retrieve the previously stored text file
ReadFile>C:\Temp\WS1_Image.txt,Text
Len>Text,L2

Base64>Text,DECODE,B64Data
Len>B64Data,L3
WriteLn>C:\Temp\Image.jpg,result,B64Data
MDL>b64Data:%L1%%CRLF%Text:%L2%%CRLF%B64Data:%L3%

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Sat Oct 17, 2015 6:38 am

After repeated tests, I conclude the prime suspect is WRITELN.
The manual clearly indicates that WRITELN is designed to write text, not binary, to disk.
Now the question: How to safely write binary onto disk?

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Re: Unable to Restore Image

Post by armsys » Sat Oct 17, 2015 10:18 am

When attempting to save the binary data (screen image) to disk, the code always pops an error message:
Microsoft VBScript compilation error:1032
Invalid character
Line 2, Column 35

Code: Select all

VBStart
Function SaveBinaryData(FileName, ByteArray)
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write ByteArray
'Save binary data To disk
BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function
VBEND

// Clean up all previously leftover test files
DeleteFile>C:\Temp\WS1_Image.txt
DeleteFile>C:\Temp\Test.jpg
DeleteFile>C:\Temp\Image.jpg
*Let>WLN_ENCODING=UTF8
*Let>WLN_ENCODING=UNICODE

// Capture the entire screen
GetScreenRes>Width,Height
*Let>Width=Width-1000
*Let>Height=Height-1000

ScreenCapture>0,0,Width,Height,C:\Temp\Test.jpg
// Store the screen capture as text file on disk
ReadFile>C:\Temp\Test.jpg,ImageData
Base64>ImageData,Encode,b64Data
Len>b64data,L1

Let>WLN_NOCRLF=1
WriteLn>C:\Temp\WS1_Image.txt,result,b64data

// Retrieve the previously stored text file
ReadFile>C:\Temp\WS1_Image.txt,Text
Len>Text,L2

Base64>Text,DECODE,B64Data
Len>B64Data,L3
*WriteLn>C:\Temp\Image.jpg,result,B64Data
MDL>b64Data:%L1%%CRLF%Text:%L2%%CRLF%B64Data:%L3%%CRLF%Result:%Result%
VBEval>SaveBinaryData("C:\Temp\Image.jpg",%B64Data%),Result

NickD
Pro Scripter
Posts: 58
Joined: Fri Sep 23, 2016 2:17 pm

Re: Unable to Restore Image

Post by NickD » Tue Dec 03, 2019 7:10 am

I know this reply is a few years late, but for anyone else having this problem...
I noticed that line breaks were getting added to my base64 when using labeltovar to pass the base64 string to an Msched variable. You are probably having the same issue when reading the base64 string from a txt file. Try using stringreplace on the populated variable before decoding.

Without stringreplace, the below code produces corrupt .bmp image, even though my label data contains no line breaks. With stringreplace it works fine.

Code: Select all

  IfNotFileExists>%BMP_DIR%\toast-needle.bmp
  LabelToVar>ToastNeedle64,ToastNeedle
  StringReplace>%ToastNeedle%,%CRLF%,,ToastNeedle
  Base64>%ToastNeedle%,DECODE,B64Data
  WriteLn>%BMP_DIR%\toast-needle.bmp,result,B64Data
  Endif

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Unable to Restore Image

Post by Marcus Tettmar » Tue Dec 10, 2019 8:03 am

From the manual for LabelToVar, and also here: https://www.mjtnet.com/manual/labeltovar.htm :
The optional parameter WantLineBreaks can be set to 0 to force LabelToVar to discard line breaks. By default line breaks are retained (WantLineBreaks=1). Ommitting WantLineBreaks or setting it to anything other than zero will cause line breaks to be included as default. Setting to zero causes line breaks to be removed.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts