将windows窗体图表控件保存为硬盘上的图像



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

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

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

I tried also only:

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

在这两种情况下,我得到异常时,试图保存:

路径不是合法的
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-SharptestForm1.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-SharptestProgram.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: 

与其他Control一样,Chart也具有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事件中绘制到Chart的表面时,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倾向于在小字体周围变得模糊。

最新更新