c#中关于当前上下文不存在名称的问题
我是新手,用c#复制网上的代码总提示上下文不存名称XXX,能帮忙解释一下是哪的原因吗?代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPlot;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void plot_Click(object sender, EventArgs e)
{
// --- Plotting ---
plot.Clear(); //对象名称为plot
// --- Grid Code ---
Grid mygrid = new Grid();
mygrid.HorizontalGridType = Grid.GridType.None;
mygrid.VerticalGridType = Grid.GridType.Fine;
plot.Add(mygrid);
plot.Title = "chulai啊";
StepPlot line = new StepPlot();
line.Pen = new Pen(Color.Blue, 1);
line.OrdinateData = new int[] { 0, 1, 0, 1, 1, 0 };
line.HideVerticalSegments = false;
plot.Add(line);
plot.Refresh();
return;
}
private void button1_Click(object sender, EventArgs e)
{
plot_Click(sender, e);
}
private void button2_Click(object sender, EventArgs e)
{
// --- Plotting ---
plot.Clear();
plot.Refresh();
}
////////对所绘的图进行打印与保存//////////
private void print()
{
myPlot.Print(true);
}
private void save()
{
saveFileDialog1.Filter = "位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg;*.jpeg;*,jpe|Gif(*.gif)|*.gif|Tiff(*.tiff)|*.tiff|Png(*.png)|*.png|Exif(*.exif)|*.exif|所有文件(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
int h = myPlot.Size.Height;
int w = myPlot.Size.Width;
Bitmap bm = new Bitmap(w, h);
Bitmap bm1 = new Bitmap(w, h);
Rectangle rt = new Rectangle(1, 1, w, h);
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.CreatePrompt = true;
myPlot.DrawToBitmap(bm, rt);
if (saveFileDialog1.FilterIndex == 1)
{
bm.Save(saveFileDialog1.FileName);
}
if (saveFileDialog1.FilterIndex == 2)
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
}
if (saveFileDialog1.FilterIndex == 3)
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Gif);
}
if (saveFileDialog1.FilterIndex == 4)
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Tiff);
}
if (saveFileDialog1.FilterIndex == 5)
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Png);
}
if (saveFileDialog1.FilterIndex == 6)
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Exif);
}
}
catch (Exception MyEx)
{
MessageBox.Show(MyEx.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
}
错误报告:当前上下文中不存在"saveFileDialog1"
当前上下文中不存在"myplot"
当前上下文中不存在"ImageFormat"