使用HTML POST将图像上传到Web服务器

时间:2015-02-02 20:26:23

标签: html vb.net 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”需要是一个字符串。 我希望我可以得到帮助!

感谢。

1 个答案:

答案 0 :(得分:0)

要在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