다음은 공통 기능을 사용하여 각 파일에 대해 사용자에게 메시지를 보내는 방법입니다. 당신은 당신의 폼의 코드 숨김 섹션에서이 라이브러리에 대한 참조를 추가해야합니다
private void btnSave_Click(object sender, EventArgs e)
{
string imageFilePath = null;
string dataFilePath = null;
ImageFormat format = System.Drawing.Imaging.ImageFormat.Png;
try
{
// Image data
imageFilePath = promptForFilePath("Save Chart image", "PNG Files (*.png)|*.png|JPEG Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*");
if (!String.IsNullOrWhiteSpace(imageFilePath))
{
switch (Path.GetExtension(imageFilePath).Replace(".", "").ToLower())
{
case "jpeg":
case "jpg":
format = ImageFormat.Jpeg;
break;
case "png": format = ImageFormat.Png;
break;
case "bmp": format = ImageFormat.Bmp;
break;
}
this.graph.SaveImage(imageFilePath, format);
}
// XML data
dataFilePath = promptForFilePath("Save Chart data","XML Files (*.xml)|*.xml|All Files (*.*)|*.*");
if (!String.IsNullOrWhiteSpace(dataFilePath)) this.graph.Serializer.Save(dataFilePath);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "btnSave_Click Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
string promptForFilePath(string title = "Save File", string fileFilter = "All Files (*.*)|*.*")
{
string result = null;
SaveFileDialog saveFileDialog = null;
try
{
saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = title;
saveFileDialog.Filter = fileFilter;
if(saveFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
result = saveFileDialog.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "saveChartData Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (saveFileDialog != null)
{
saveFileDialog.Dispose();
saveFileDialog = null;
}
}
return result;
}
:
using System.Drawing.Imaging;
using System.IO;
그런 다음 저장 버튼을 전체 파일 경로에 대한 사용자에게 메시지를 표시하기위한 공통 기능에 대한 이벤트 처리기를 추가
경로를 지정하여 다른 폴더에 저장할 수 있어야합니다. 'chart1.SaveImage ("C : \\ myfolder \\ mychart.png", ChartImageFormat.Png);' – derloopkat