Does any one knows how to use VB to convert jpeg file format into base64. I'm still new in VB. What I've got only class module file (.cls) and I have no idea on how to use it. Thanks in advance..
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
I don't think she knows very well about that. That's why I'm asking here. I'm an electrical student. I'm supposed to make the picture that is converted(base 64) can be transfered through serial communication. Both part about serial communication and hardware were already done. Now, it's the last part which I supposed to convert the image file....
What is the purpose of the cls file? My guess is it is a byte to Base64 converter. Maybe post a couple of the public function names so we can see.
If it is a byte to base64 converter, open the JPG file in binary, using VB. Then read the file into a byte array, send the array to the class function which should return another array or string. Send that array/string across the net.
To use the cls file. Declare it in your routines, something like the following.
Have no idea what your class filename is, let's just assume it is default Class1.
Dim myClass As Class1
Set myClass = New Class1
-- now you can call its functions, i.e., myClass.FunctionName
Insomnia is just a byproduct of, "It can't be done"
What is the purpose of the cls file? My guess is it is a byte to Base64 converter. Maybe post a couple of the public function names so we can see.
If it is a byte to base64 converter, open the JPG file in binary, using VB. Then read the file into a byte array, send the array to the class function which should return another array or string. Send that array/string across the net.
To use the cls file. Declare it in your routines, something like the following.
Have no idea what your class filename is, let's just assume it is default Class1.
Dim myClass As Class1
Set myClass = New Class1
-- now you can call its functions, i.e., myClass.FunctionName
Actually I found two example.. So I guess I'll show both of them to you to see... Both of them were really different from each other. Maybe the way of the software design is different.. I don't know.. Thanks..
Not to sure about the 1st class you posted; I discovered errors in there and wouldn't trust it. Not even worth the time to fix it because there are so many Base64 encode/decoders about.
The 2nd one appears correct and is the same one found on this site. Look at where it came from and read the postings; you should see what you are looking for.
Insomnia is just a byproduct of, "It can't be done"
That page talks more on how to do the testing..
Here's some of my code :
Code:
Private Sub cmdOpen_Click()
cdOpen.Filter = "Joint Photo Group (*.jpg)|*.jpg|Joint Photo Expert Group (*.jpeg)|*.jpeg"
cdOpen.ShowOpen
If Not vbCancel Then
txtFileName = cdOpen.FileName
End If
End Sub
Private Sub cmdEncode_Click() 'the problems is here.
Dim myClass As Class1
Set myClass = New Class1
myClass.Encode
End Sub
How am I suppose to convert the file ? The encoded file will then display on a text field as shown as below :
That page talks more on how to do the testing..
Here's some of my code
Yes, but it also showed you how to get the byte data into a string so you can use it with that class. It is Post #8 on that link. I've tweaked your code a bit to include what I was trying to get you to see.
Code:
Private Sub cmdOpen_Click()
cdOpen.Filter = "Joint Photo Group (*.jpg)|*.jpg|Joint Photo Expert Group (*.jpeg)|*.jpeg"
cdOpen.ShowOpen
If Not vbCancel Then
txtFileName = cdOpen.FileName
End If
End Sub
Private Sub cmdEncode_Click() 'the problems is here.
Dim sBase64 As String
Dim sJPG as String
Dim iFile As Integer
Dim myClass As Class1
Set myClass = New Class1
iFile = FreeFile()
Open txtFileName.Text For Binary Access Read Lock Write As #iFile
sJPG = String$(LOF(iFile), Chr$(0))
Get #iFile, , sJPG
Close #iFile
sBase64 = myClass.Encode(sJPG) ' the result from the function is what you want
End Sub
Insomnia is just a byproduct of, "It can't be done"
I still don't have solution for the decoding function...
Here's my code :
Code:
Private Sub cmdDecode_Click()
On Error Resume Next
Dim sBase64 As String
Dim sJPG As Integer
Dim iFile As Integer 'maybe the problems is here
Dim myClass As Class1
Set myClass = New Class1
iFile = FreeFile()
Save Text2.Text 'don't know how to call the encoded code on the receiver text display
sJPG = String$(LOF(iFile), Chr$(0))
Get #iFile, , sJPG
Close #iFile
sBase64 = myClass.Decode(sJPG) 'is this how to save the file?
cdSave.Filter = "Joint Photo Group (*.jpg)|*.jpg|Joint Photo Expert Group (*.jpeg)|*.jpeg"
cdSave.ShowSave
If Err Then
MsgBox "Dialog Cancelled"
Exit Sub
End If
End Sub
not quite. Simply calling cdSave.ShowSave saves nothing, it does return the path & file name, in the .FileName property, of where the user wants the data saved; it is up to you to create a file and write the data.
Edited: Oh, your save filter is for JPG type extensions; saving a Base64 converted JPG image with a JPG extension does not make it a JPG. It will no longer display an image. To get it to JPG format again, you must decode the Base64 to binary data & then write the binary data to a file with a JPG extension. Understand?
Last edited by LaVolpe; Mar 27th, 2008 at 03:15 PM.
Insomnia is just a byproduct of, "It can't be done"
I'm sorry.. So I need to convert it to binary first and then I can convert the binary data into JPG file back which means I need to convert the transmitted data once again into binaries to get the exact JPG files. LOL.. You get me a little confused..
I can write an example, but you should do that instead. Here is what is happening. It isn't confusing if you think about it.
1. You have a JPG file, ok?
2. You convert its bytes to a Base64 string for whatever purposes. Now it is no longer a JPG file, it is encoded text, encrypted. It is no longer what it was.
3. To get it back, you have to decode the Base64 string back to its original format.
The above is also the basics of encryption and compression routines. This is what happens alot on networks: get binary data, convert to Base64, send across network, receive on other end, convert from Base64, save binary data.
Insomnia is just a byproduct of, "It can't be done"
1. You have a JPG file, ok? (OK)
2. You convert its bytes to a Base64 string for whatever purposes. Now it is no longer a JPG file, it is encoded text, encrypted. It is no longer what it was. (Yes.. It is no longer what it was)
3. To get it back, you have to decode the Base64 string back to its original format. (By this, you means that to decode the Base64 string back to its original format, I need to convert it FIRST to binary data & then write the binary data to a file with a JPG extension?)
I just think of this... What are decoding function in the class module for as if I need to convert it to binary first? Do I still need to decode it first and then turn it to binary? Or I just make the encoded data into binary and save it to JPG file? I think this is why I've got confused in the first place.
Base64 uses only characters in a small range of the possible 255 ascii characters and prevents potential of transmission protocols interpreting some bytes incorrectly or as some special commands. I think this link explains it pretty well.
Your original question was how to convert jpg to Base64. I think that question was answered and you can do that now. The entire process is pretty straightforward:
1. Convert binary (jpg image file) to Base64 (encoded)
2. Transmit/Send to wherever
3. It is received from transmission
4. Decode Base64 to binary & save data. Decoded data is now a JPG again.
Insomnia is just a byproduct of, "It can't be done"
Private Function ConverttoBinary(char As Byte) As String
Dim i As Integer
Dim x As Integer
Dim strBinByte As String
For i = 7 To 0 Step -1
x = char And (2 ^ i)
Select Case x
Case Is > 0
strBinByte = strBinByte & "1"
Case Else
strBinByte = strBinByte & "0"
End Select
Next i
ConverttoBinary = strBinByte
End Function
I've tried this :
Code:
Dim sBase64 As String
Dim sJPG As String
Dim iFile As Integer
Dim myClass As Class1
Dim i As Integer
Dim strByte As String
Dim B() As Byte
Dim intLen As Integer
Set myClass = New Class1
iFile = FreeFile()
Open Text2.Text For Binary Access Read Lock Write As #iFile
sJPG = String$(LOF(iFile), Chr$(0))
Get #iFile, , sJPG
Close #iFile
sBase64 = myClass.Decode(sJPG)
B = myClass.Decode(sJPG)
intLen = UBound(B)
For i = 0 To intLen Step 2
strByte = ConverttoBinary(B(i))
Next i
cdSave.Filter = "Joint Photo Group (*.jpg)|*.jpg|Joint Photo Expert Group (*.jpeg)|*.jpeg"
cdSave.ShowSave
If Err Then
' This code runs if the dialog was cancelled
MsgBox "Dialog Cancelled"
Exit Sub
End If
End Sub
But still can't make it into one file.... Any suggestion?
See if this works for you. Assumption is that file in Text2 is original JPG encoded as Base64
Code:
Dim sBase64 As String
Dim sJPG As String
Dim iFile As Integer
Dim myClass As Class1
cdSave.Filter = "Joint Photo Group (*.jpg)|*.jpg|Joint Photo Expert Group (*.jpeg)|*.jpeg"
cdSave.ShowSave
If Err Then
' This code runs if the dialog was cancelled
MsgBox "Dialog Cancelled"
Else
Set myClass = New Class1
iFile = FreeFile()
Open Text2.Text For Binary Access Read Lock Write As #iFile
sJPG = String$(LOF(iFile), 0)
Get #iFile, , sJPG
Close #iFile
sBase64 = myClass.Decode(sJPG)
iFile = FreeFile()
Open cdSave.FileName For Binary As #iFile
Put #iFile, , sBase64
Close #iFile
End If
Insomnia is just a byproduct of, "It can't be done"
See if this works for you. Assumption is that file in Text2 is original JPG encoded as Base64
Code:
Dim sBase64 As String
Dim sJPG As String
Dim iFile As Integer
Dim myClass As Class1
cdSave.Filter = "Joint Photo Group (*.jpg)|*.jpg|Joint Photo Expert Group (*.jpeg)|*.jpeg"
cdSave.ShowSave
If Err Then
' This code runs if the dialog was cancelled
MsgBox "Dialog Cancelled"
Else
Set myClass = New Class1
iFile = FreeFile()
Open Text2.Text For Binary Access Read Lock Write As #iFile
sJPG = String$(LOF(iFile), 0)
Get #iFile, , sJPG
Close #iFile
sBase64 = myClass.Decode(sJPG)
iFile = FreeFile()
Open cdSave.FileName For Binary As #iFile
Put #iFile, , sBase64
Close #iFile
End If
When I was creating my email application I needed a way to send images and other binary files as attachments so I went looking for some base64 encoders. Well, I found a few and tried them in my application and all of them worked quite well. So I implemented one of them and everything was OK. Then one day I sent a bitmap attachment and when I clicked on Send the program went into a loop and stayed there for about an hour or so. That's when I realized that those encoders were only good for small binary attachments and completely fell apart when trying to encode large bitmaps. So I needed a better encoder. The attached zip file contains that encoder. It has a deblocker option on it in case you want to deblock your base64 encoded output. This was necessary because attachments on email had to be deblocked at 74 bytes per line.
Download this and run it as a standalone. If you like it then use the .BAS module in your application.
As far as I can see this is just another one of those "wheels" that doesn't need reinvention. WinNT OSs all have the CryptoAPI as part of the base install. You can use this as is, or via CAPICOM.
CAPICOM is free and freely redistributable. Going that route also gets you support on Win9x with IE5+ installed.
Of course playing with Base64 encoding by hand might be a decent programming excercise for a new VB programmer.
The attached demo uses an internal Class to wrap the CryptoAPI calls, which means it doesn't run on Win9x but also doesn't have CAPICOM as a dependency. I don't know how it compares in speed with hand-rolled VB6 code but it's the code Windows uses all over the place itself.
That's very good, dilettante. It was exactly the same as mine and the same time to encode it. So how would I go about deblocking it at different lengths?
That's very good, dilettante. It was exactly the same as mine and the same time to encode it. So how would I go about deblocking it at different lengths?
I'm not sure what you mean by "deblocking" here. Perhaps you are talking about line wrapping at varying lengths?
I don't think you get an option here, the CryptoAPI library uses 64 characters. I think it uses that to conform to PEM (requires 64 character line length) and MIME (length is arbitrary but cannot exceed 76 character line length).
Both of those limits come from SMTP quirks. Any Base64 decoder is expected to process any line length (including no wrapping at all). The line-wrap symbols can be either CrLf or Lf, and you have to handle both on decoding. It's all part of Postel's Law.
I wasn't able to do much with your example. There are four missing non-standard typelibs. People also should be warned to stop using parts of the VB5 runtime in VB6 programs. Microsoft does not support VB5 after Windows XP and the runtimes aren't included in later OSs.
You're also moving binary data around in String variables which is risky in some locales due to the Unicode translations. I'd have to think the double-conversion overhead would also be a performance killer when you encode and decode data that starts life as binary. Have you tried testing against actual binary data, such as an image file or an EXE?
I'm getting encoding at under 0.02 secs/MB and decoding under 0.03 secs/MB even in the IDE. Of course this will vary based on your computer and almost all of the work is being done in the CryptoAPI library (Crypt32.dll) so compiling isn't an issue. This method is fast even if you compile for p-code to keep programs compact.
Mine doesn't have any dependencies other than requiring an NT OS, so it is easy to test.
If Text2 is the base64 data, then simply do this. Remaining lines should be fine
Code:
REPLACE THESE 6 LINES
iFile = FreeFile()
Open Text2.Text For Binary Access Read Lock Write As #iFile
sJPG = String$(LOF(iFile), 0)
Get #iFile, , sJPG
Close #iFile
sBase64 = myClass.Decode(sJPG)
WITH JUST THIS ONE LINE
sBase64 = myClass.Decode(Text2.Text)
Insomnia is just a byproduct of, "It can't be done"