C# Out of Memory when Creating Bitmap

前端 未结 6 1209
旧时难觅i
旧时难觅i 2020-12-09 19:41

I\'m creating an application (Windows Form) that allows the user to take a screenshot based on the locations they choose (drag to select area). I wanted to add a little \"pr

相关标签:
6条回答
  • 2020-12-09 20:12

    If you are creating new bitmaps over and over, you might need to call GC.Collect(); which will force C# to garbage collect

    0 讨论(0)
  • 2020-12-09 20:24

    you should check if curLocation.X is larger than 50, otherwise your rectangle will start in the negative area (and of course curLocation.Y)

    0 讨论(0)
  • 2020-12-09 20:26

    Out of memory in C# imaging, is usually sign of wrong rect or point - a bit of red herring. I bet start has negative X or Y when error happens or the Size.Hight + Y or Size.Width + X is bigger than Hight or width of the image.

    0 讨论(0)
  • 2020-12-09 20:28

    Use the Bitmap object like this:

    using (Bitmap bmpImage = new Bitmap(img))
    {
        // Do something with the Bitmap object
    }
    
    0 讨论(0)
  • 2020-12-09 20:35

    If the zoom box goes off the edge of the desktop area, then when you try to crop, you are asking the system to make a new image that includes pixels outside of the video memory area. Make sure to limit your zoom box so that none of its extents is less than 0 or greater than the screen edges.

    0 讨论(0)
  • 2020-12-09 20:36

    MSDN explains that an OutOfMemoryException means

    rect is outside of the source bitmap bounds

    where rect is the first parameter to the Bitmap.Clone method.

    So check that the cropArea parameter is not larger than your image.

    In GDI+ an OutOfMemoryException does not really mean "out of memory"; the GDI+ error code OufOfMemory has been overloaded to mean different things. The reasons for this are historic and a well described by Hans Passant in another answer.

    0 讨论(0)
提交回复
热议问题