我是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
任何人都可以告诉我如何使用名称作为时间码保存我的图像。
答案 0 :(得分:2)
文件名不应包含:
。尝试将其更改为下划线或只是删除它:
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);
}