gpt4 book ai didi

vb.net - Imgur API和VB.NET帮助-图片发布

转载 作者:行者123 更新时间:2023-12-02 04:16:53 24 4
gpt4 key购买 nike

我正在尝试将图像发送到Imgur的服务器。
一切正常,我从解析器中获取了图像的URL,但是当我尝试在Web浏览器上打开它时,我没有得到图像……只是一个“损坏的图像”图标。

我认为这是在转换为字节数组时出现的问题....但是我不明白。请让我知道/修改我的代码。

   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

最佳答案

这是C#中的Imgur API示例

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

要回答您的问题,您需要先将图像读取为字节数组。然后将原始字节数组转换为Base64编码的字符串

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);

关于vb.net - Imgur API和VB.NET帮助-图片发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2443172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com