Adding ProgressBar to this code

  • Thread starter Thread starter blissid
  • Start date Start date
B

blissid

Guest
Hey guys, i've been using the following code to upload gifs to imgur, and some of them can get pretty big in filesize, so i wanted to add a progress bar to it to allow my users some kind of visual feedback. I've tried a bunch of different ways to try and get this to work, such as putting my string into byte arrays but still no luck to get it to function.

here is the working code, with a simple before upload happens progress update(50%), then a 100% once the upload is completed, anyway someone can help me add incremental progress bar updates? ty!


Private Sub ImgurBGW_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles ImgurBGworker.DoWork
Dim theBackgroundWorker As BackgroundWorker
Dim imgFilePath As String = Application.StartupPath & "\" & LastSavedImageName
Dim apiKey As String = "xxxKEYHERExxx"
theBackgroundWorker = CType(sender, BackgroundWorker)

theBackgroundWorker.ReportProgress(50, "Uploading image. Please wait. . .")

If Not theBackgroundWorker.CancellationPending Then
Try
Dim imageData As Byte()
Dim fileStream As FileStream = File.OpenRead(imgFilePath)
LastSavedImageSize = FormatBytes(fileStream.Length)
imageData = New Byte(fileStream.Length - 1) {}
fileStream.Read(imageData, 0, imageData.Length)
fileStream.Close()
Const MAX_URI_LENGTH As Integer = 32766
Dim base64img As String = System.Convert.ToBase64String(imageData)
Dim sb As StringBuilder = New StringBuilder()
Dim i As Integer = 0

While i < base64img.Length
sb.Append(Uri.EscapeDataString(base64img.Substring(i, Math.Min(MAX_URI_LENGTH, base64img.Length - i))))
i += MAX_URI_LENGTH
End While

Dim uploadRequestString As String = "key=" & apiKey & "&title=" & "title" & "&caption=" & "img" & "&image=" & sb.ToString()
Dim request As HttpWebRequest = CType(WebRequest.Create(" View: https://api.imgur.com/3/image
"), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ServicePoint.Expect100Continue = False
request.Headers("Authorization") = "Client-ID " & apiKey
Dim streamWriter As StreamWriter = New StreamWriter(request.GetRequestStream())
streamWriter.Write(uploadRequestString)
streamWriter.Close()

Dim response As WebResponse = request.GetResponse()
Dim responseStream As Stream = response.GetResponseStream()
Dim responseReader As StreamReader = New StreamReader(responseStream)

Dim jss As New JavaScriptSerializer()
Dim image As ImgurAPI = jss.Deserialize(Of ImgurAPI)(responseReader.ReadToEnd())

e.Result = image.Data.Link
theBackgroundWorker.ReportProgress(100, "Uploaded [" & LastSavedImageSize & "] Image Successfully. Use <Ctrl + V> to paste direct link to the image.")

Catch ex As Exception
FrmMain.Timer1.Enabled = True
FrmMain.Timer2.Enabled = False
FrmMain.ToolStripLabel1.ForeColor = Color.Red
FrmMain.ToolStripLabel1.Text = ex.Message
FrmMain.ToolStrip1.Refresh()
End Try
Else
theBackgroundWorker.ReportProgress(0, "Upload Canceled!")
e.Cancel = True
End If
End Sub

Continue reading...
 

Similar threads

S
Replies
0
Views
152
Shan1986
S
I
Replies
0
Views
156
Innovators World Wide
I
Back
Top