GDI +中的一般错误

时间:2015-05-21 11:40:48

标签: c# gdi+ bmp

我正在循环中保存来自摄像机的图像,但有时候在某些系统中它会在gdi +中出现一般错误,而我使用的是i7 4gb ram并且当我使用i3 4gnb ram系统时它会出现

for (int i = 1; i <= frameno; i++)
{
    Bitmap bm = new Bitmap(1024,1280);
    Int32 s32MemID;
    Camera.Memory.GetActive(out s32MemID);
    Camera.Memory.CopyToBitmap(s32MemID, out bm);
    bm.RotateFlip(RotateFlipType.Rotate90FlipNone);
    String str = "";
    if (i < 10)
    {
        str = "00";
    }
    else if (i < 100)
    {
        str = "0";
    }
    bm.Save(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\capture" + str + i + ".bmp", ImageFormat.Bmp); 
    Thread.Sleep(delay);
}

请有人给我解决方案,这对我来说非常不利

1 个答案:

答案 0 :(得分:0)

我想通过使用,例如:

,通过一些重构可以使内存更友好
Int32 s32MemID;
Bitmap bm;
string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
for (int i = 1; i <= frameno; i++)
{
    try
    {
        Camera.Memory.GetActive(out s32MemID);
        Camera.Memory.CopyToBitmap(s32MemID, out bm);
        bm.RotateFlip(RotateFlipType.Rotate90FlipNone);
        bm.Save(Path.Combine(targetPath, "capture" + (i.ToString().PadLeft('0', 3)) + ".bmp"), ImageFormat.Bmp); 
    }
    finally 
    {
        if (bm != null) 
        {
            bm.Dispose();
            bm = null;
        }
    }
    Thread.Sleep(delay);
}

通常我会建议使用模式,但由于你没有显示Camera.Memory.CopyToBitmap方法如何处理out Bitmap参数,很难说它应该如何完成,如果它是你自己的lib,我不知道不知道为什么你需要out参数,因为无论如何都会通过Reference传递位图