【问题标题】:How to avoid running out of memory with dynamic text in openTK如何避免openTK中的动态文本内存不足
【发布时间】:2015-07-05 13:28:48
【问题描述】:

我一直在尝试在 openTK(在 c# 中)中在屏幕上绘制一个计时器,为此我一直在生成新纹理并删除旧纹理,但是我的程序仍然占用内存直到它崩溃,因为没有有足够的空间放置另一个位图。

这就是我正在做的事情:

        text_bmp = new Bitmap(width, height);
        text_bmp_gfx = Graphics.FromImage(text_bmp);
        text_bmp_gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        text_bmp_gfx.Clear(Color.Transparent);
        text_bmp_gfx.DrawString(music.getCurrentTime(), new Font("Exo 2", 12), drawBrush, new PointF(0.0F, 0.0F));
        text_bmp_gfx.DrawString(timer.Elapsed.ToString(), new Font("Exo 2", 12), drawBrush, new PointF(0.0F, 18.0F));
        GL.DeleteTexture(TextTexture);
        TextTexture = ContentPipe.LoadTextureFromBitmap(text_bmp);
        GL.BindTexture(TextureTarget.Texture2D, TextTexture);

content pipe.loadtexturefrombitmap 是这个函数在哪里:

public static int LoadTextureFromBitmap(Bitmap bmp)
    {
        int id = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, id);
        BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
        bmp.UnlockBits(data);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        bmp = null;
        return id;
    }

说实话,第二部分是从 youtube 教程中复制的,所以我不确定它是如何工作的。

我认为问题在于,在我不需要 openTK 纹理之后,我没有正确地从它们中释放内存,因此我生成了大量图像,但我不知道如何解决这个问题。

【问题讨论】:

    标签: c# opengl memory visual-studio-2013 opentk


    【解决方案1】:

    对于那些发现这一点的人来说,有两个重要的部分:

    在位图和图形对象上调用 dispose()

    手动垃圾收集。

    每秒制作 60 张大图像显然对于自动垃圾收集来说有点多,所以我每帧调用一次垃圾收集,效果很好!

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多