简体   繁体   English

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

[英]Unable to save Bitmap Image with variable name

I am newbie in C#, Im trying to save a Bitmap image on button click. 我是C#的新手,我试图在单击按钮时保存位图图像。 I am using visual studio 2010 for this. 我为此使用Visual Studio 2010。 I am able to save my image with I pass a particular string as file name. 我可以通过传递特定的字符串作为文件名来保存图像。 here is my code for saving image:- 这是我保存图像的代码:

    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);
    }

But I want to save this image having name as time at which image was captured. 但我想保存此图像,其名称为捕获图像的时间。 So I add this in my code:- 所以我将其添加到我的代码中:

    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);

    }

This code build fine but when I click on my button, I got this exception:- 这段代码构建良好,但是当我单击按钮时,出现了以下异常:

NotSupportedException was handled

Can anyone tell me how to save my image with name as timecode. 谁能告诉我如何将名称保存为时间码的图像。

The name of the file should not consist : . 文件名不应包含: Try to change that into underscore or simply remove it: 尝试将其更改为下划线或将其删除:

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