繁体   English   中英

无法保存具有变量名称的位图图像

[英]Unable to save Bitmap Image with variable name

我是C#的新手,我试图在单击按钮时保存位图图像。 我为此使用Visual Studio 2010。 我可以通过传递特定的字符串作为文件名来保存图像。 这是我保存图像的代码:

    private void button1_Click(object sender, EventArgs e)
    { 

        bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
        gfxScreenshot = Graphics.FromImage(bmpScreenshot);
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        bmpScreenshot.Save("img.jpg", ImageFormat.Jpeg);
    }

但我想保存此图像,其名称为捕获图像的时间。 所以我将其添加到我的代码中:

    private void button1_Click(object sender, EventArgs e)
    { 
        string time = DateTime.Now.ToString("hh:mm:ss");
        string img = time + ".jpg";
        bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
        gfxScreenshot = Graphics.FromImage(bmpScreenshot);
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        bmpScreenshot.Save(img, ImageFormat.Jpeg);

    }

这段代码构建良好,但是当我单击按钮时,出现了以下异常:

NotSupportedException was handled

谁能告诉我如何将名称保存为时间码的图像。

文件名不应包含: 尝试将其更改为下划线或将其删除:

private void button1_Click(object sender, EventArgs e)
{ 
    string time = DateTime.Now.ToString("HHmmss");
    string img = time + ".jpg";
    bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
    gfxScreenshot = Graphics.FromImage(bmpScreenshot);
    gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
    bmpScreenshot.Save(img, ImageFormat.Jpeg);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM