Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GDI+;中发生一般错误;将日期和时间添加到保存的图片中时_C#_Image_Winforms - Fatal编程技术网

C# GDI+;中发生一般错误;将日期和时间添加到保存的图片中时

C# GDI+;中发生一般错误;将日期和时间添加到保存的图片中时,c#,image,winforms,C#,Image,Winforms,我想创建一个简单的捕获活动窗口。因此,我按照下面的代码创建它: string _dateTime = DateTime.Now.ToString("dd/MM/yyyy"); Rectangle bounds = this.Bounds; try { using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) {

我想创建一个简单的捕获活动窗口。因此,我按照下面的代码创建它:

string _dateTime = DateTime.Now.ToString("dd/MM/yyyy");

        Rectangle bounds = this.Bounds;

        try
        {
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }

                bitmap.Save("D:/Test/Screenshot - " + _dateTime + " -.jpg", ImageFormat.Jpeg);

                SystemManager.ShowMessageBox("Success!", "Success", 1);
            }
        }

        catch (Exception ex)
        {
            SystemManager.ShowMessageBox("There is an unexpected error: " + ex.Message, "Error", 3);
        }
    }
但是上面的代码给出了一个错误:

GDI+中发生一般错误。

但一旦我将保存的图像更改为:

bitmap.Save("D:/Test/Screenshot.jpg", ImageFormat.Jpeg);
它成功地捕获了图像

我的问题是:它是否不能将日期和时间添加到保存的图像中

因此,格式如下所示:

截图-21/01/2015-.jpg

任何帮助都将不胜感激

谢谢

位图。Save()
不构造子目录,这就是为什么会出现错误

当您将位图保存为“Screenshot-21/01/2015-.jpg”时,它将假定
/
是一个目录分隔符,并将尝试相应地构造路径


一种简单的方法是使用其他字符(例如
-
)来构建日期时间。

“Screenshot-21/01/2015-.jpg”表示您正在尝试将文件“2015-.jpg”保存在文件夹“Screenshot-21”中,子文件夹为“01”…谢谢@DanByström,已经解决了。