1. PRASHANT NIRGUN
  2. PowerBuilder
  3. Saturday, 27 August 2022 18:06 PM UTC

I want to convert the image file into base64 Image. used following steps. But wen I check that using any online tool it fails String to image tool

01 I open file in stream Mode and stored into byte variable

02 Convert it to string using AnsiEncode

03 Convert string into base 64 using gcc_system.of_encrypt_base_64('Encrypt Base 64')

 

Code is as follows

//click event of cb_file_open
Blob lblob_data

Integer li_rc, li_fnum
Long ll_length
String ls_title, ls_pathname, ls_filename, ls_filter

ls_title = "Select a File"
ls_filter = "*.jpg,*.jpeg,*.bmp,*.png"

li_rc = GetFileOpenName(ls_title, ls_pathname, ls_filename, "", ls_filter)
If li_rc < 1 Then
Return
End If

li_fnum = FileOpen(ls_pathname, StreamMode!, Read!)
ll_length = FileReadEx(li_fnum, lblob_data)
FileClose(li_fnum)
mle_response.text = String(lblob_data, EncodingAnsi!)
mle_output.txt = gcc_system.of_encrypt_base_64('Encrypt Base 64')

 

I also tried using Topwiz bcrypt tool using following steps attaching image 

01 Button Hash a file

02 copy sle_result.text to mle_message (convert sle_message to mle_message)

03 click button cb_base64

04 copy sle_result.text and test in online tool

Attachments (1)
Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Tuesday, 30 August 2022 03:04 AM UTC
  2. PowerBuilder
  3. # Permalink
Function Boolean CryptBinaryToString ( & blob pbBinary, & ulong cbBinary, & ulong dwFlags, & Ref string pszString, & Ref ulong pcchString) Library "Crypt32.dll" Alias For "CryptBinaryToStringW"
Comment
  1. PRASHANT NIRGUN
  2. Tuesday, 30 August 2022 03:06 AM UTC
Thanks to René Ullrich final code is as follows



Blob lblob_data

Int li_fnum

Long ll_length

String ls_pathname, ls_encoded

ULong lul_len, lul_buflen

Boolean lb_rtn



ls_pathname = "D:\tss\images\png\128x128\msgioin.png"

li_fnum = FileOpen(ls_pathname, StreamMode!, Read!)

ll_length = FileReadEx(li_fnum, lblob_data)

FileClose(li_fnum)



lul_len = Len(lblob_data)

lul_buflen = 2 + 1.5 * (lul_len + 2)



lb_rtn = CryptBinaryToString(lblob_data, &

lul_len, CRYPT_STRING_BASE64, &

ls_encoded, lul_buflen)

mle_response.text = ls_encoded
  1. Helpful
There are no comments made yet.
PRASHANT NIRGUN Accepted Answer Pending Moderation
  1. Saturday, 27 August 2022 19:29 PM UTC
  2. PowerBuilder
  3. # 1

I am very sorry I am using PB 12.5 and my function is as follows and I am returning base64 string only. 

//gcc_system.of_encrypt_base_64('Encrypt Base 64')

String ls_encoded
ULong lul_len, lul_buflen
Boolean lb_rtn

Blob value

// the value to be encoded, needs to be 3 char or more (but not 4?)
value = blob(as_value)
//value = blob(sle_1.text, EncodingUTF16BE!)

lul_len = Len(value)
lul_buflen = lul_len * 2
ls_encoded = Space(lul_buflen)

lb_rtn = CryptBinaryToString(value, &
lul_len, CRYPT_STRING_BASE64, &
ls_encoded, lul_buflen)

IF NOT lb_rtn THEN
ls_encoded = ""
ELSE
// remove the last two chr(0)!
ls_encoded = left(ls_encoded, len(ls_encoded) - 2 )
END IF

Return ls_encoded

Comment
  1. René Ullrich
  2. Monday, 29 August 2022 05:47 AM UTC
To calculate the buffer length (lul_buflen) I use

lul_buflen = 2 + 1.5 * (lul_len + 2)



To remove the CRLF from BASE64 you can combine (add values) CRYPT_STRING_BASE64 with CRYPT_STRING_NOCRLF (= 1073741824).



To get a BASE64 from a image file you should directly convert the blob to BASE64 as Mike already wrote. Do not convert the blob to string first.

Maybe you have to change your of_encrypt_base_64 function to accept a blob value instead of string or add an extra function for that.



  1. Helpful
  1. PRASHANT NIRGUN
  2. Monday, 29 August 2022 08:06 AM UTC
is it working at your end please share the code external function declaration. I not worked with local external functions. I am using PB 12.5 classical
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Saturday, 27 August 2022 18:36 PM UTC
  2. PowerBuilder
  3. # 2

this is wrong -->> mle_output.txt = gcc_system.of_encrypt_base_64('Encrypt Base 64')

 

you need to encrypt the blob to base64

Comment
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Saturday, 27 August 2022 18:27 PM UTC
  2. PowerBuilder
  3. # 3

Hi, Prashant - 

You didn't state what version of PB you are using.

If you are using PB 2017 or newer, I suggest you use the Base64Encode function of the CoderObject object to perform Base64 encoding of a Blob.

Look at the PB Help topic for Base64Encode.

Read the image file into a blob variable in a single step using FileReadEx instead of using FileRead and reading it in 64K chunks. The PB Help topic for FileReadEx contains a code sample showing how to read a .BMP file into a Blob variable.

Best regards, John

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.