将窗体图表控件作为图像保存到硬盘上?

时间:2015-05-16 08:51:58

标签: c# .net winforms

我使用图表绘制事件在图表点和线上绘图。 然后我有一个按钮点击事件:

private void button2_Click(object sender, EventArgs e)
{
    saveFileDialog1.DefaultExt = ".txt";
    saveFileDialog1.Filter = "Text files (.txt)|*.txt";
    chart1.SaveImage(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

我想将图表和所有图纸保存为图像,然后在另一个按钮点击事件中或在构造函数中将所有图纸加载回图表,我可以从同一个地方继续。

我也只尝试过:

private void button2_Click(object sender, EventArgs e)
{
    chart1.SaveImage(Path.GetDirectoryName(saveFileDialog1.FileName) + "\\chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

在这两种情况下,我在尝试保存时都会遇到异常:

ArgumentException路径不是合法格式

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=The path is not of a legal form.
  Source=mscorlib
  StackTrace:
       at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
       at System.IO.Path.GetDirectoryName(String path)
       at DietTracker.Form1.button2_Click(Object sender, EventArgs e) in d:\C-Sharp\test\Form1.cs:line 334
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at DietTracker.Program.Main() in d:\C-Sharp\test\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

1 个答案:

答案 0 :(得分:2)

与任何其他Chart一样,Control采用DrawToBitmap方法。

using (Bitmap bmp = new Bitmap(chart1.ClientSize.Width, chart1.ClientSize.Height))
{
    chart1.DrawToBitmap(bmp, chart1.ClientRectangle);
    bmp.Save("yourfilename", ImageFormat.Png);
}

但此外它还有一个Chart.SaveImage方法,选择了formats,包括一些wmf(Windows增强型图元文件)格式可供选择:

chart1.SaveImage("yourfilename", ChartImageFormat.Png);

请注意,两者都会保存当前视图,即缩放和滚动。

但是,当你在Chart事件的Paint表面上绘图时,SaveImage方法对你不起作用,因为它只使用图表数据,不是你在Paint事件中绘制的东西..所以去DrawToBitmap版本!

请注意,这两种方法都会保存图表的图像,就像您要求的那样,而不是数据值!

正如评论中所指出的,从用户那里获取文件名的正确方法是这样的:

SaveFileDialog sfd = new SaveFileDialog();
// set it up..but not as text!
// then use it only if ok:
if (sfd.ShowDialog() == DialogResult.OK) bmp.Save(sfd.FileName, ImageFormat.Png);

请注意,我选择了png以获得最佳效果,jpg在小字体周围会变得模糊。