System.OutOfMemoryException问题在Visual Studio的VB.net表单文件中。

huangapple go评论65阅读模式
英文:

System.OutOfMemoryException issue in visual studio VB.net forms file

问题

I get hit with this System.OutOfMemoryException every time I try the project.

This was supposed to change the picture box to different pictures (ads). I tried changing the picture formats, they were .png files and now .jpg files.

Here is the code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim gen As Random = New Random()

    Dim choice As Integer = gen.Next(0, 4)
    If choice = 0 Then
        PictureBox1.Image = Image.FromFile("Screenshot 2023-03-25 102759.jpg")
    ElseIf choice = 1 Then
        PictureBox1.Image = Image.FromFile("ad2.jpg")
    ElseIf choice = 2 Then
        PictureBox1.Image = Image.FromFile("ad4.jpg")
    Else
        PictureBox1.Image = Image.FromFile("ad5.jpg")
    End If
End Sub

and here is the stack trace:

StackTrace
   at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
   at System.Drawing.Image.FromFile(String filename)
   at Word_game_prototype.Form1.Timer1_Tick(Object sender, EventArgs e) in C:\Visual studio (delete this junk)\Word game prototype\Word game prototype\Form1.vb:line 20
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Word_game_prototype.My.MyApplication.Main(String[] Args) in :line 83

(Note: The code and stack trace have been provided without translation, as requested.)

英文:

I get hit with this System.OutOfMemoryException every time I try the project.

This was supposed to change the picture box to different pictures (ads). I tried changing the picture formats, they were .png files and now .jpg files.

Here is the code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim gen As Random = New Random()

    Dim choice As Integer = gen.Next(0, 4)
    If choice = 0 Then
        PictureBox1.Image = Image.FromFile("Screenshot 2023-03-25 102759.jpg")
    ElseIf choice = 1 Then
        PictureBox1.Image = Image.FromFile("ad2.jpg")
    ElseIf choice = 2 Then
        PictureBox1.Image = Image.FromFile("ad4.jpg")
    Else
        PictureBox1.Image = Image.FromFile("ad5.jpg")
    End If
End Sub

and here is the stack trace:

StackTrace
   at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
   at System.Drawing.Image.FromFile(String filename)
   at Word_game_prototype.Form1.Timer1_Tick(Object sender, EventArgs e) in C:\Visual studio (delete this junk)\Word game prototype\Word game prototype\Form1.vb:line 20
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at Word_game_prototype.My.MyApplication.Main(String[] Args) in :line 83

答案1

得分: 1

在每个时钟周期避免从磁盘重新加载文件,可按如下方式处理:

' 将以下内容定义在类级别,而不是在方法中
Private Images As New List(Of Image) From {
      Image.FromFile("Screenshot 2023-03-25 102759.jpg"),
      Image.FromFile("ad2.jpg"),
      Image.FromFile("ad4.jpg"),
      Image.FromFile("ad5.jpg")
}

' 随机对象应该具有长寿命
Private gen As New Random()

' 突然间,这也可以成为一行代码
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    PictureBox1.Image = Images(gen.Next(0, 4))
End Sub
英文:

Do this, to avoid re-loading the file from disk on every tick:

' Define these at the class level, not in the method
Private Images As New List(Of Image) From {
      Image.FromFile("Screenshot 2023-03-25 102759.jpg"),
      Image.FromFile("ad2.jpg"),
      Image.FromFile("ad4.jpg"),
      Image.FromFile("ad5.jpg")
}

' Random objects should be Long-lived
Private gen As New Random()

' And suddenly this can be a one-liner, too
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    PictureBox1.Image = Images(gen.Next(0, 4))
End Sub

答案2

得分: 1

根据Image.FromFile文档,如果发生以下情况,将引发OutOfMemoryException异常:

  • 文件没有有效的图像格式。
  • GDI+不支持文件的像素格式。

还有多种其他原因。其中最有可能的一个原因是文件被多次打开,此时最好使用Image.FromStream()而不是Image.FromFile,请参阅https://stackoverflow.com/a/2216338/217666。

因此,作为建议缓存图像对象的替代方法,您可以定义并使用一个安全的加载图像文件的函数:

Private Shared Function OpenImage(imageFile As String) As Image
    Using fs As FileStream = New FileStream(imageFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Return Image.FromStream(fs)
    End Using
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim gen As Random = New Random()

    Dim choice As Integer = gen.Next(0, 4)
    If choice = 0 Then
        PictureBox1.Image = OpenImage("Screenshot 2023-03-25 102759.jpg")
    ElseIf choice = 1 Then
        PictureBox1.Image = OpenImage("ad2.jpg")
    ElseIf choice = 2 Then
        PictureBox1.Image = OpenImage("ad4.jpg")
    Else
        PictureBox1.Image = OpenImage("ad5.jpg")
    End If
End Sub

这是加载图像文件的安全方法。

英文:

According to the Image.FromFile documentation, a OutOfMemoryException is thrown if:

> The file does not have a valid image format.

or
> GDI+ does not support the pixel format of the file.

And there are multiple other reasons. One of the most likely is that the file is open multiple times, in which case it's better to use Image.FromStream() instead of Image.FromFile, see https://stackoverflow.com/a/2216338/217666.

So as an alternative to the answer suggesting caching the image objects, you can define and use a safe function for loading image files:

Private Shared Function OpenImage(imageFile As String) As Image
    Using fs As FileStream = New FileStream(imageFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Return Image.FromStream(fs)
    End Using
End Function


Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim gen As Random = New Random()

    Dim choice As Integer = gen.Next(0, 4)
    If choice = 0 Then
        PictureBox1.Image = OpenImage("Screenshot 2023-03-25 102759.jpg")
    ElseIf choice = 1 Then
        PictureBox1.Image = OpenImage("ad2.jpg")
    ElseIf choice = 2 Then
        PictureBox1.Image = OpenImage("ad4.jpg")
    Else
        PictureBox1.Image = OpenImage("ad5.jpg")
    End If
End Sub

答案3

得分: 0

已解决的问题似乎是图像没有在转换中保存下来,所以我不得不获取图像的新副本,现在它们可以工作。尽管如此,还是感谢所有的有用提示。

英文:

Fixed the issues looked like the images didn't survive the conversion so i had to get a fresh copy of the images and now they work. thanks for all of the helpful tips tho.

huangapple
  • 本文由 发表于 2023年4月17日 19:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定