-
Dec 31st, 2012, 06:00 PM
#1
Thread Starter
Junior Member
Converting image to base64 String? Help with code!
Hello, so I am trying to make a program that will convert an image from a screenshot of the users desktop to base64 string so that I can send it to my websites php API.
Here is what i have so far... also know. This is my very first program with VB so it's probably completely wrong but I would greatly appreciate it if you could help me out! Thanks!
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim sd As Bitmap
'Taking the screenshot of desktop here
sd = New Bitmap(My.Computer.Screen.WorkingArea.Width, _
My.Computer.Screen.WorkingArea.Height, _
Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(sd)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), _
New Size(My.Computer.Screen.WorkingArea.Width, _
My.Computer.Screen.WorkingArea.Height))
'Converting the image to a byte[] to later be converted to base64 string
Dim imgStream As MemoryStream = New MemoryStream()
sd.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png)
imgStream.Close()
Dim byteArray As Byte() = imgStream.ToArray()
imgStream.Dispose()
'Convert the byte[] to base64 string for use with WebRequest to upload.
Dim final As String
final = Convert.ToBase64String(byteArray)
'Display the string to know that it's working.
MsgBox(final)
End Sub
The problem is right now that it's not displaying the String in the message box... if this code is completely wrong or not possible I'll get it lol. Like I said, first program. Just trying to piece together stuff I've found and see if it works.
Best Regards, Aurora
-
Dec 31st, 2012, 06:28 PM
#2
New Member
Re: Converting image to base64 String? Help with code!
your code works. I think the problem is that the result is too big to display in a msgbox.
try this line of code where your message box is.
MsgBox(Mid(final, 1, 2048))
-
Dec 31st, 2012, 06:31 PM
#3
Re: Converting image to base64 String? Help with code!
It works fine. A Base64String cannot be displayed in a standard Windows control though even if it could this one would be way too long for anything other than a RichTextBox.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 31st, 2012, 06:44 PM
#4
Thread Starter
Junior Member
Re: Converting image to base64 String? Help with code!
Ah okay that makes sense. Thanks guys! I appreciate it!
-
Dec 31st, 2012, 08:00 PM
#5
Re: Converting image to base64 String? Help with code!
Originally Posted by dunfiddlin
A Base64String cannot be displayed in a standard Windows control
A String is a String. Just because it represents data in base-64 format doesn't make it any different to any other String.
-
Dec 31st, 2012, 08:09 PM
#6
Thread Starter
Junior Member
Re: Converting image to base64 String? Help with code!
I have one more question/request. So I need to now get this String to be sent(I'm guessing with the "POST" method) to my APIs URL.
The Dev of my image script said that it needs to be sent in base64 encoding and the API converts it back into the image.
So here is what I have so far for the POST(I haven't started the response part yet):
Code:
Dim request As WebRequest = WebRequest.Create("http://www.imagereplica.com/api.php?upload=")
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = final.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(final, 0, final.Length)
The only problem with this is that in the writing the stream "final" is underlined and says that a String cannot be converted to a 1-dimensional array of Byte. I'm not sure what this means.... I hope y'all can help! Thanks! If you have any questions about the info cause this might have been kinda vague just let me know!
-
Dec 31st, 2012, 08:15 PM
#7
Re: Converting image to base64 String? Help with code!
The way to write text to a Stream is with a StreamWriter, e.g.
Code:
Using writer As New StreamWriter(myStream)
writer.Write(myString)
End Using
Try that first and, if it doesn't work, you may find that you have to use the StreamWriter constructor that takes an Encoding object as a parameter and specify Encoding.ASCII or Encoding.Unicode.
-
Dec 31st, 2012, 10:26 PM
#8
Thread Starter
Junior Member
Re: Converting image to base64 String? Help with code!
Originally Posted by jmcilhinney
The way to write text to a Stream is with a StreamWriter, e.g.
Code:
Using writer As New StreamWriter(myStream)
writer.Write(myString)
End Using
Try that first and, if it doesn't work, you may find that you have to use the StreamWriter constructor that takes an Encoding object as a parameter and specify Encoding.ASCII or Encoding.Unicode.
I see this helps with writing to a file... How do I use that for writing to a URL?
-
Dec 31st, 2012, 11:01 PM
#9
Re: Converting image to base64 String? Help with code!
Originally Posted by AuroraTheGreat
How do I use that for writing to a URL?
As the name suggests, a StreamWriter is used for writing to a Stream, as I stated in my previous post. That means any Stream. It is most commonly used to write to a file via a FileStream but it makes no distinction between one Stream and another. You are not writing to a URL. Your writing to a Stream, as your own code states:
Code:
Dim dataStream As Stream = request.GetRequestStream()
All a StreamWriter does is convert text to binary and then write it to the underlying Stream.
-
Dec 31st, 2012, 11:38 PM
#10
Thread Starter
Junior Member
Re: Converting image to base64 String? Help with code!
Originally Posted by jmcilhinney
As the name suggests, a StreamWriter is used for writing to a Stream, as I stated in my previous post. That means any Stream. It is most commonly used to write to a file via a FileStream but it makes no distinction between one Stream and another. You are not writing to a URL. Your writing to a Stream, as your own code states:All a StreamWriter does is convert text to binary and then write it to the underlying Stream.
So you're saying something like this...?
Code:
Dim request As WebRequest = WebRequest.Create("http://www.imagereplica.com/api?upload=")
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = final.Length
Dim dataStream As Stream = request.GetRequestStream()
Using writer As New StreamWriter(dataStream)
writer.Write(final)
End Using
Sadly, that code does not work. I'm not sure where to go from here. I suppose I'll have to research some more. Thanks again for the help. If you find anything or come across anything feel free to leave a comment here so I can get the email to check back!
Many Thanks, Aurora
-
Jan 1st, 2013, 12:46 AM
#11
Thread Starter
Junior Member
Re: Converting image to base64 String? Help with code!
Hmm. I thought for sure this would work. But still nothing. Sigh...
Code:
Dim final As String
final = Convert.ToBase64String(byteArray)
Dim wb As WebRequest = WebRequest.Create(New Uri("http://imagereplica.com/api.php"))
wb.ContentType = "application/x-www-form-urlencoded"
wb.Method = "POST"
wb.Timeout = 10000
Dim parameters As String = "format=xml&key=lolapi&upload=" + (final)
Dim encoding As New System.Text.UTF8Encoding()
Dim bytes As Byte() = encoding.GetBytes(parameters)
Dim os As System.IO.Stream = Nothing
Try
wb.ContentLength = bytes.Length
os = wb.GetRequestStream()
os.Write(bytes, 0, bytes.Length)
Finally
If Not (os Is Nothing) Then
End If
End Try
-
Jan 1st, 2013, 01:28 AM
#12
Re: Converting image to base64 String? Help with code!
I thought that I had replied again to this thread but apparently not. You seem to be under the impression that writing to the request stream is sending data to the server. It's not. You've accomplished nothing by using an Encoding object to convert the String to a Byte array because that's exactly what a SteramWriter does internally anyway. If you want to actually send the data to the server then you have to execute the request, which you do by calling GetResponse. I'd be surprised if any example of using an HttpWebRequest didn't include a call to GetResponse.
-
Jan 1st, 2013, 02:32 AM
#13
Thread Starter
Junior Member
Re: Converting image to base64 String? Help with code!
Originally Posted by jmcilhinney
I thought that I had replied again to this thread but apparently not. You seem to be under the impression that writing to the request stream is sending data to the server. It's not. You've accomplished nothing by using an Encoding object to convert the String to a Byte array because that's exactly what a SteramWriter does internally anyway. If you want to actually send the data to the server then you have to execute the request, which you do by calling GetResponse. I'd be surprised if any example of using an HttpWebRequest didn't include a call to GetResponse.
Ah... lol. Sorry, total newbie here. But I finally got it to work. The only problem is I'm getting an error code back from the API saying "invalid source (thumb)" but I think everything is correct as far as the .net goes. So thank you very much for all the help. I appreciate you putting up with me lol. But I think the main problem is with the API and how I'm sending it(like what format) but I don't know what other options I have. Anyways, thanks again!
Happy coding! And Happy New Year!
-
Mar 7th, 2015, 10:36 PM
#14
New Member
Re: Converting image to base64 String? Help with code!
I was wondering if you could show me the final code that got it to post to the URL, since I'm kind of in the same mess.
Also would it be possible to see a snippet of the PHP code used to get the image from a base64 string.
Thanks
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|