试图在VB 2012中的图片框中显示2个叠加层

时间:2012-10-05 19:12:07

标签: vb.net

我正试图在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

1 个答案:

答案 0 :(得分:1)

您不能将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