You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chart控件保存图片提示路径格式不支持的问题排查与解决

解决Chart控件保存图片时的System.NotSupportedException错误

问题描述

使用Chart控件绘制图表后,调用SaveImage方法保存图片时抛出System.NotSupportedException,错误提示“给定的路径格式不支持”。相关代码如下:

string dir = $"{System.IO.Directory.GetCurrentDirectory()}\\Charts\\{DateTime.Now.ToString()}.png";
chart1.SaveImage(dir, ChartImageFormat.Png);

生成的路径示例:"C:\Users\Hendawi\Desktop\ESI-USB-API\Examples\C#\bin\Debug\Charts\2023-04-03 5:18:41 PM.png",直接保存到桌面可正常运行。

问题原因

Windows系统文件名不允许包含冒号(:),而代码中DateTime.Now.ToString()生成的时间字符串包含冒号(如示例中的5:18:41),导致路径格式非法。

解决方案

修改DateTime的格式化字符串,将时间中的冒号替换为合法字符(如下划线),确保生成的文件名符合Windows规范。

修正后的代码:

// 生成不含冒号的时间字符串
string validTimeStr = DateTime.Now.ToString("yyyy-MM-dd hh_mm_ss tt");
string savePath = $"{System.IO.Directory.GetCurrentDirectory()}\\Charts\\{validTimeStr}.png";
chart1.SaveImage(savePath, ChartImageFormat.Png);

补充说明

Windows文件名禁止使用的字符包括:<, >, :, ", /, \, |, ?, *,生成文件名时需避免包含这些字符。

内容的提问来源于stack exchange,提问作者hendoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:00:10