{"id":306,"date":"2017-12-27T23:03:14","date_gmt":"2017-12-27T23:03:14","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=306"},"modified":"2022-02-17T06:24:14","modified_gmt":"2022-02-17T06:24:14","slug":"copy-chart","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/charts\/copy-chart","title":{"rendered":"Copy Chart"},"content":{"rendered":"<h4>\n\tCopy Chart To Clipboard<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tMemoryStream MemoryStream1 = new MemoryStream();\r\n\tChart1.SaveImage(MemoryStream1, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Bmp);\r\n\tSystem.Drawing.Bitmap Bitmap1 = new System.Drawing.Bitmap(MemoryStream1);\r\n\tSystem.Windows.Forms.Clipboard.SetImage(Bitmap1);\r\n<\/code>\r\n<\/pre>\n<h4>\n\tSave Chart To File<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tconst string MY_FILE_EXTENSION = &quot;png&quot;;\r\n\tString SaveAsFilename;\r\n\r\n\t\/\/If last directory is not valid then default to My Documents\r\n\tif (!Directory.Exists(Path.GetDirectoryName(ApMain.LastFileDirectory)))\r\n\t\tApMain.LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\r\n\r\n\tSaveFileDialog SelectFileDialog = new SaveFileDialog();\r\n\r\n\tSelectFileDialog.Filter = &quot;Image File (*.&quot; + MY_FILE_EXTENSION + &quot;)|*.&quot; + MY_FILE_EXTENSION;       \/\/&quot;txt files (*.txt)|*.txt|All files (*.*)|*.*&quot;\r\n\tSelectFileDialog.FilterIndex = 1;              \/\/(First = 1, not 0)\r\n\tSelectFileDialog.Title = &quot;Save Chart Image As&quot;;\r\n\tSelectFileDialog.OverwritePrompt = true;\r\n\ttry\r\n\t{\r\n\t\tSelectFileDialog.InitialDirectory = ApMain.LastFileDirectory;\r\n\t}\r\n\tcatch (Exception)\r\n\t{\r\n\t\tSelectFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\r\n\t}\r\n\t\/\/Display dialog box\r\n\tif (SelectFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)     \/\/This ensures correct filename extension is used\r\n\t{\r\n\t\t\/\/----- SAVE THE FILE -----\r\n\t\tSaveAsFilename = SelectFileDialog.FileName;\r\n\r\n\t\t\/\/STORE LAST USED DIRECTORY\r\n\t\tif (SaveAsFilename.LastIndexOf(&quot;\\\\&quot;) &gt;= 0)\r\n\t\t\tApMain.LastFileDirectory = SaveAsFilename.Substring(0, (SaveAsFilename.LastIndexOf(&quot;\\\\&quot;) + 1));\r\n\t\telse\r\n\t\t\tApMain.LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);\r\n\r\n\t\tChart1.SaveImage(SaveAsFilename, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);\r\n\t}\r\n<\/code><\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Copy Chart To Clipboard MemoryStream MemoryStream1 = new MemoryStream(); Chart1.SaveImage(MemoryStream1, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Bmp); System.Drawing.Bitmap Bitmap1 = new System.Drawing.Bitmap(MemoryStream1); System.Windows.Forms.Clipboard.SetImage(Bitmap1); Save Chart To File const string MY_FILE_EXTENSION = &quot;png&quot;; String SaveAsFilename; \/\/If last directory is not valid then default to My Documents if (!Directory.Exists(Path.GetDirectoryName(ApMain.LastFileDirectory))) ApMain.LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); SaveFileDialog SelectFileDialog = new SaveFileDialog(); SelectFileDialog.Filter = &quot;Image File (*.&quot; + [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-charts"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=306"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/306\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}