第二个Bitmap上的GDI +发生了一般错误。保存方法

时间:2017-08-19 13:55:03

标签: c# bitmap gdi+

我编写了一个写入文件的函数,但每次调用它时都会重写旧文件并替换它。但是,它第二次运行时拒绝写入它。关闭程序会重置此循环。

我环顾四周,注意到它可能被阻止,因为它仍然在程序中使用。我猜文件没有正确处理。

我尝试过:添加使用和处理以清除内存以允许重写。但是,它仍然不想多次写入文件。

这就是我最终的结果:

System.Drawing.Image img = System.Drawing.Image.FromFile($"{ID}Image.png");
using (var newBitmap = new Bitmap(System.Drawing.Image.FromFile("Template.png")))
{
    using (Graphics graphics = Graphics.FromImage(newBitmap))
    {
        using (Font arialFont = new Font("Arial", 18))
        {
            Font arialFont2 = new Font("Arial", 28);
            RectangleF Rectangle1 = new RectangleF(0, 350, 858, 40);
            RectangleF Rectangle2 = new RectangleF(0, 390, 858, 30);
            RectangleF Rectangle3 = new RectangleF(0, 420, 858, 30);
            RectangleF Rectangle4 = new RectangleF(0, 450, 858, 30);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            graphics.DrawString(UserName, arialFont2, Brushes.Black, Rectangle1);
            graphics.DrawString("Info: " + LoadData(ID, "Info"), arialFont, Brushes.Black, Rectangle2);
            graphics.DrawString("Total: " + LoadData(ID, "Total"), arialFont, Brushes.Black, Rectangle3);
            graphics.DrawString("Other: " + LoadData(ID, "Other"), arialFont, Brushes.Black, Rectangle4);
            graphics.DrawImage(img, 525, 250);
            newBitmap.Save($"{ID}Stats.gif");
            arialFont.Dispose();
            arialFont2.Dispose();
            graphics.Dispose();
            newBitmap.Dispose();
        }
    }
}

0 个答案:

没有答案