将图像添加到图片框时出现内存不足问题

时间:2017-03-30 15:20:59

标签: vb.net memory

论坛的新手 - 我已经对此进行了大量研究并尝试了各种不同的方法,但在尝试显示某些机器时,我不断收到某些机器上的内存不足问题,而不是所有机器图片框中的图像。我的代码如下,这是在Ultragrid_AfterSelectChange事件

上调用的
Dim myImagePath As String = ""

If ThumbNail.Selected.Rows.Count > 0 Then
    Dim rowSelected As UltraGridRow
    For Each rowSelected In ThumbNail.Selected.Rows
        Select Case folderType.ToLower
            Case "image", "scan"
                myImagePath = ThumbNail.Rows(rowSelected.Index).Cells(1).Value
                Try
                    If IO.File.Exists(myImagePath) Then
                        If Not IO.File.GetAttributes(myImagePath) = IO.FileAttributes.Normal Then
                            IO.File.SetAttributes(myImagePath, IO.FileAttributes.Normal)
                        End If
                        Dim fs As New System.IO.FileStream(myImagePath, IO.FileMode.Open, IO.FileAccess.Read)
                        Dim bm As Image

                        bm = Image.FromStream(fs).Clone

                        'bm1 = bm
                        Application.DoEvents()
                        fs.Close()

                        ' SBD(12 / 29 / 2014)
                        If PictureBox.Image IsNot Nothing Then
                            Try
                                PictureBox.Image.dispose()
                                'PictureBox.Image = Nothing
                            Catch ex As Exception

                            End Try

                        End If
                        'SBD 12/29/2014

                        PictureBox.Image = bm
                        Application.DoEvents()
                        fs.Dispose()
                        fs = Nothing ' July 16, 2013 - SM

                        bm.Dispose()
                        bm = Nothing ' July 16, 2013 - SM

                    End If

                Catch ex As Exception
                    If My.Settings.DebugMode = "True" Then
                        MessageBox.Show(ex.Message, "Show Image", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Else
                        MessageBox.Show("Unable to locate file: " & myImagePath & ". Contact your administrator.", "Show Image", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                End Try

            Case Else
                'myImagePath = Application.StartupPath & "\Music.bmp"
                If ThumbNail.Rows(rowSelected.Index).Cells(1).Value IsNot Nothing Then
                    If IO.File.Exists(ThumbNail.Rows(rowSelected.Index).Cells(1).Value) Then
                        Try
                            AxWindowsMediaPlayer.URL = ThumbNail.Rows(rowSelected.Index).Cells(1).Value
                        Catch ex As Exception
                            MessageBox.Show("Media player error.", "Windows Media Player", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        End Try

                    End If
                End If

        End Select
    Next
End If

0 个答案:

没有答案