1

我正在尝试在 Picturebox 控件中将 2 个天气雷达图像叠加在一起。图形的尺寸都是 600、550。我的代码没有给我任何错误,但是当我运行它时,它只是给了我一个空白表格。我究竟做错了什么?

这是我的代码:

Dim Image1 As New Bitmap("http://radar.weather.gov/ridge/Overlays/Topo/Short/FWS_Topo_Short.jpg")
    Dim Image2 As New Bitmap("http://radar.weather.gov/ridge/RadarImg/N0R/FWS_N0R_0.gif")
    Dim Image3 As New Bitmap(600, 550)
    Dim g As Graphics = Graphics.FromImage(Image3)
    g.DrawImage(Image1, New Point(0, 0))
    g.DrawImage(Image2, New Point(0, 0))
    PictureBox1.Image = Image3
    g.Dispose()
    g = Nothing
4

1 回答 1

1

Bitmap 不能使用 URI,需要指定本地路径。但是,您可以使用带有位图的流,因此首先打开站点图像的流并通过它加载它。

例如:

        Dim Image1 As Bitmap = GetStreamBitmap("http://radar.weather.gov/ridge/Overlays/Topo/Short/FWS_Topo_Short.jpg")
        Dim Image2 As Bitmap = GetStreamBitmap("http://radar.weather.gov/ridge/RadarImg/N0R/FWS_N0R_0.gif")

        'TODO implement error checking, "if image1 is nothing" etc..

        Dim Image3 As New Bitmap(600, 550)
        Dim g As Graphics = Graphics.FromImage(Image3)
        g.DrawImage(Image1, New Point(0, 0))
        g.DrawImage(Image2, New Point(0, 0))

        PictureBox1.Image = Image3

        g.Dispose()
        g = Nothing

        Image2.Dispose()
        Image1.Dispose()

'...

    Private Function GetStreamBitmap(url As String) As Bitmap

        Dim bmp As Bitmap = Nothing

        Try
            Dim request As System.Net.WebRequest = _
                System.Net.WebRequest.Create(url)

            Dim response As System.Net.WebResponse = request.GetResponse()
            Dim responseStream As System.IO.Stream = response.GetResponseStream()
            bmp = New Bitmap(responseStream)

        Catch ex As System.Net.WebException
            ' handle error here
        End Try

        Return bmp

    End Function
于 2012-10-05T19:45:22.097 回答