Imgur and VB.NET API Help - Image POST

I am trying to send an image to an Imgur server. Everything went well and I get the URL of the image from the parser, but when I try to open it in a web browser, I don't get the image ... only the β€œbroken image” icon.

I think this is a problem in converting to an array of bytes .... but I do not understand this. let me know / correct my code.

   Dim image As Image = image.FromFile(OpenFile.FileName)
    Dim ms As New MemoryStream()
    ' Convert Image to byte[]
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
    Dim imageBytes As Byte() = ms.ToArray()
    Dim wb As WebRequest = WebRequest.Create(New Uri("http://imgur.com/api/upload.xml"))
    wb.ContentType = "application/x-www-form-urlencoded"
    wb.Method = "POST"
    wb.Timeout = 10000
    Console.WriteLine(imageBytes.Length)
    Dim parameters As String = "key=a801fa0b08a2117f5bb62b006f769b99&image=" + Convert.ToBase64String(imageBytes)
    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)
        Dim xmlData As String = POSThandling.makePOSTrequest("http://imgur.com/api/upload.xml", New String() {parameters})
        Dim xmlDoc As XmlDocument = New XmlDocument()
        xmlDoc.LoadXml(xmlData)
        Dim name As XmlNodeList = xmlDoc.GetElementsByTagName("original_image")
        Dim imageText As String = (name(0).InnerText).ToString
        messageText.Text = imageText.ToString
        PanelUpload.Visible = False
        UpImage.Enabled = True
        SendMsg.Enabled = True
    Finally
        If Not (os Is Nothing) Then
        End If
    End Try
+3
source share
1 answer

Here is an example of the Imgur API in C #

http://api.imgur.com/examples#uploading_cs

To answer your question, you need to first read the image into an array of bytes. Then convert the array of source bytes to a Base64 encoded string

FileStream fileStream = File.OpenRead(imageFilePath);
byte[] imageData = new byte[fileStream.Length];
fileStream.Read(imageData, 0, imageData.Length);
fileStream.Close();
string base64EncodedImage = System.Convert.ToBase64String(imageData);
+1
source

Source: https://habr.com/ru/post/1736901/


All Articles