Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否将windows窗体图表控件另存为映像到硬盘?_C#_.net_Winforms - Fatal编程技术网

C# 是否将windows窗体图表控件另存为映像到硬盘?

C# 是否将windows窗体图表控件另存为映像到硬盘?,c#,.net,winforms,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.Fil

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

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: 

图表
与任何其他
控件一样
具有
DrawToBitmap
方法

using (Bitmap bmp = new Bitmap(chart1.ClientSize.Width, chart1.ClientSize.Height))
{
    chart1.DrawToBitmap(bmp, chart1.ClientRectangle);
    bmp.Save("yourfilename", ImageFormat.Png);
}
但除此之外,它还有一个
Chart.SaveImage
方法,可以从中选择相当丰富的格式,包括一些wmf(Windows增强型图元文件)格式:

chart1.SaveImage("yourfilename", ChartImageFormat.Png);
请注意,两者都将保存当前视图,即缩放和滚动

但是,当您在
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
在小字体周围会变得模糊。

代码相当奇怪。为什么要使用.txt作为图像文件的过滤器?为什么不使用用户提供的文件名?直接使用saveFileDialog1.FileName即可。