English 中文(简体)
视觉C#, 过程在我收到与用户群有关的记忆错误之前不断发展,该机利用一个编程绘制图[封闭]
原标题:Visual C#, process grows until I get memory errors relating to a UserControl using a loop to draw graphics [closed]
  • 时间:2012-05-07 00:59:23
  •  标签:
  • c#
Closed. This question needs details or clarity. It is not currently accepting answers.

我利用视觉C号2008年版,注意到,由于我的项目负荷,这一过程消耗了约70 000克记忆。 几个小时之后,这架建筑在大约50万K。

此时此刻, 载有<代码>PictureBox(在Panel上)的图像C# Express显示一个记忆错误。 照片箱中包含一个照相图和grid形的网格,用<条码>System.Drawing.Graphics。

Here s the code:

仅在《>的启动时,才发生这种争执。

Bitmap myBitmap = new Bitmap(a, b);
Graphics g = null;
g = Graphics.FromImage(myBitmap);
g.FillRectangle(Brushes.SteelBlue, 0, 0, c, d);

//Paint Rows & Columns
for (int x = 0; x <= e - 1; x++)
{
    for (int y = 0; y <= f - 1; y++)
    {
        g.DrawRectangle(Pens.LightBlue, g, h, i);
    }
}
//Release Resources
g.Dispose();
//Add bitmap with grid to BG
ScorePictureBox.Image = myBitmap;

该法典非常频繁:

for (int EventIndex = 0; EventIndex <= MidiNoteDownArray.Length - 1; EventIndex++)
{
    //Paint notes to grid

    e.Graphics.FillRectangle(Brushes.LightBlue, j, k, l, m);
    e.Graphics.DrawRectangle(Pens.Purple, o, p, q, r);
}
e.Dispose();

我没有适当释放资源吗? 我如何能够正确这样做? rrect

最佳回答

It s probably the bitmaps you re creating - when you replace the image on your ScorePictureBox, you need to dispose of the old one, i.e:

var oldImage = ScorePictureBox.Image;
//Add bitmap with grid to BG
ScorePictureBox.Image = myBitmap;
// Dispose of previous score image if necessary
if (oldImage != null) oldImage.Dispose();

Note the double-handling - it s generally best not to dispose of a GDI+ object while other items are still referencing them.

在一般的同学说明中,你可能比照using的声明,而不是明确要求Dispose(,即:

using (Graphics g = Graphics.FromImage(myBitmap))
{
   ...
}
问题回答

检查你的项目。 也许你没有提及。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签