使用 HTML POST 将图像上传到网络服务器
Upload an image to webserver using HTML POST
我正在尝试制作一个屏幕捕获程序,然后将其直接上传到其中一台服务器。不幸的是,我尝试的所有方法要么给我错误,要么根本没有解决。
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
Form2.Hide()
Me.Hide()
Dim bounds As Form2
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Form2
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(Form2.Bounds.X, Form2.Bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Form2.BackgroundImage = screenshot
Using sendto As New Net.WebClient
Dim param As New Specialized.NameValueCollection
param.Add("userfile", screenshot)
Dim response_bytes = sendto.UploadValues("http://www.directimg.eu/index.php", "POST", param)
Dim response_body = (New Text.UTF8Encoding).GetString(response_bytes)
End Using
Me.Close()
End Sub
这是我当前的代码,不幸的是它不起作用,因为 "screenshot" 需要是一个字符串。
希望能帮到我!
谢谢。
要在 POST 数据中发送图像,您需要将它们转换为 Base64 字符串。但首先你必须得到图像字节。这是一个示例,我已根据需要进行了修改,因为我没有您的表格,但您应该可以遵循它:
Imports System.Drawing
Imports System.IO
Module Module1
Sub Main()
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
screenshot = New System.Drawing.Bitmap(400, 400, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(20, 500, 0, 0, New Size(400, 400), CopyPixelOperation.SourceCopy)
Dim ms As MemoryStream = New MemoryStream()
screenshot.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
'screenshot.Save("c:\deleteme\screenshot.png")
Dim imgBytes As Byte() = ms.ToArray()
Dim imgStr As String = System.Convert.ToBase64String(imgBytes)
Using sendto As New Net.WebClient
Dim param As New Specialized.NameValueCollection
param.Add("userfile", imgStr)
Dim response_bytes = sendto.UploadValues("http://www.directimg.eu/index.php", "POST", param)
Dim response_body = (New System.Text.UTF8Encoding).GetString(response_bytes)
End Using
End Sub
End Module
我正在尝试制作一个屏幕捕获程序,然后将其直接上传到其中一台服务器。不幸的是,我尝试的所有方法要么给我错误,要么根本没有解决。
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
Form2.Hide()
Me.Hide()
Dim bounds As Form2
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Form2
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(Form2.Bounds.X, Form2.Bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
Form2.BackgroundImage = screenshot
Using sendto As New Net.WebClient
Dim param As New Specialized.NameValueCollection
param.Add("userfile", screenshot)
Dim response_bytes = sendto.UploadValues("http://www.directimg.eu/index.php", "POST", param)
Dim response_body = (New Text.UTF8Encoding).GetString(response_bytes)
End Using
Me.Close()
End Sub
这是我当前的代码,不幸的是它不起作用,因为 "screenshot" 需要是一个字符串。 希望能帮到我!
谢谢。
要在 POST 数据中发送图像,您需要将它们转换为 Base64 字符串。但首先你必须得到图像字节。这是一个示例,我已根据需要进行了修改,因为我没有您的表格,但您应该可以遵循它:
Imports System.Drawing
Imports System.IO
Module Module1
Sub Main()
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
screenshot = New System.Drawing.Bitmap(400, 400, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(20, 500, 0, 0, New Size(400, 400), CopyPixelOperation.SourceCopy)
Dim ms As MemoryStream = New MemoryStream()
screenshot.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
'screenshot.Save("c:\deleteme\screenshot.png")
Dim imgBytes As Byte() = ms.ToArray()
Dim imgStr As String = System.Convert.ToBase64String(imgBytes)
Using sendto As New Net.WebClient
Dim param As New Specialized.NameValueCollection
param.Add("userfile", imgStr)
Dim response_bytes = sendto.UploadValues("http://www.directimg.eu/index.php", "POST", param)
Dim response_body = (New System.Text.UTF8Encoding).GetString(response_bytes)
End Using
End Sub
End Module