在Visual Basic中最多加载32768个图片框

问题描述 投票:0回答:1

我有一个应用程序正在加载32 ^ 2至32768 8x8 px图片框。所有图片框都在屏幕上,因此我需要全部加载它们,而不能只加载其中的一部分。

就目前而言,我的程序甚至无法运行。是否有更好的方法来加载这么多图片框?

我想与您分享我的项目,但我不知道如何.............

虽然谢谢!

vb.net picturebox
1个回答
0
投票

您可能会在这种设计下遇到MemoryOverflowException。从它的声音来看,如果是这种情况,那么您可能正在尝试渲染某种地图,那么这个答案就适合您(否则就忽略它)。

在较高的级别,您应该只创建可以在任何给定时间显示在屏幕上的PictureBox控件。您可以使用以下函数进行计算:

Private Function CalculateSizeToFitParent(ByVal parent As Control, ByVal childSize As Size) As Size
    Return New Size(parent.Width \ childSize.Width, parent.Height \ childSize.Height)
End Sub

您将实现它来创建一个PictureBox来填充当前Form的可见区域:

Dim pictureBoxSize As Size = New Size(8, 8)
Dim visibleArea(pictureBoxSize.Width - 1, pictureBoxSize.Height - 1) As PictureBox
Dim numberOfPictureBoxes As Size = CalculateSizeToFitParent(Me, pictureBoxSize)

For x As Integer = 0 To numberOfPictureBoxes.Width - 1
    For y As Integer = 0 To numberOfPictureBoxes.Height - 1
        visibleArea(x, y) = New PictureBox() With {
            .Location = New Point(x * pictureBoxSize.Width, y * pictureBoxSize.Height)
            .Size = pictureBoxSize
        }
        Me.Controls.Add(visibleArea(x, y))
    Next
Next

下一部分是双重的:

  1. 您需要跟踪当前可见的左上角的位置
  2. 您将需要在地图的相应可视区域中重新加载图像。

这是假设您有一个2D阵列来存储图像。并且请注意,您不会重新创建PictureBox控件,而只是重新加载现有控件的图像:

Private _currentLocation As Point = New Point(0, 0) ' If you're starting somewhere else change it here
Public Property CurrentLocation As Point
    Get
        Return _currentLocation
    End Get
    Set(ByVal value As Point)
        If (value <> _currentLocation) Then
            _currentLocation = value
            Me.OnCurrentLocationChanged()
        End If
    End Set
End Property

Protected Overridable Sub OnCurrentLocationChanged()
    RaiseEvent CurrentLocationChanged(Me, EventArgs.Empty)
End Sub

Public Event CurrentLocationChanged(ByVal sender As Object, ByVal e As EventArgs)

Private Sub MyForm_CurrentLocationChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Me.CurrentLocationChanged
    If (visibleArea Is Nothing) Then
        Throw New Exception("The visible area has not been generated yet.")
    End If
    If (_currentLocation Is Nothing) Then
        Throw New Exception("The CurrentLocation cannot be null.")
    End If

    Dim widthUpperBounds As Integer = My2DArrayOfImageLocations.GetUpperBounds(0) - 1
    Dim heightUpperBounds As Integer = My2DArrayOfImageLocations.GetUpperBounds(1) - 1
    For x As Integer = 0 To visibleArea.GetUpperBounds(0) - 1
        For y As Integer = 0 To visibleArea.GetUpperBounds(1) - 1
            If (x + _currentLocation.Width > widthUpperBounds OrElse y + _currentLocation.Height) Then
                'This "block" is outside the view area (display a blank tile?)
            Else
                visibleArea(x, y).Load(My2DArrayOfImageLocations(x + _currentLocation.Width, y + _currentLocation.Height))
            End If
        Next
    Next
End Sub

现在,无论何时重置CurrentLocation属性(无论如何,例如,箭头键,asdw等),它都会重新绘制地图的可见区域。

© www.soinside.com 2019 - 2024. All rights reserved.