主题:[讨论]紧急求教各位高手SaveFileDialog另存图片问题!!!
各位编程高手:
大家好!
最近我在使用Visual Studio 2005(c#)+SQL Server 2005开发一个图像数据库管理系统,其他功能窗体在《Visual Studio 2005+SQL Server 2005数据库应用系统开发》一书帮助下很快实现了,而在实现图像数据导出窗体,在点击图片导出命令按钮后,总是提示如下错误,这两天我换用了另一种实现方式,还是提示下面的错误:
未处理NullReferenceException
未将对象引用设置到对象的实例。
我真的不知如何修正这错误了,希望各位高手能抽点时间给予指教,不甚感激!!!
附
图像数据导出窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ImageadminProject.ImageManage
{
public partial class ImageOutput : Form
{
//定义一个数据集对象
private DataSet MyDataSet = new DataSet();
//定义查询使用的字符串
private string MySql = "Select ImageNo AS 图像编号,Imagename AS 图像名称,Imagephoto AS 图像图片,BelongproNo AS 所属项目工程,"
+ "Imageheight AS 图像高度,Imagewidth AS 图像宽度,ROIHeight AS ROI高度,ROIWidth AS ROI宽度,"
+ "ROIX AS ROI起点X坐标,ROIY AS ROI起点Y坐标,Imageposition AS 图像位置,Imageassess AS 图像评定,Memory AS 图像备注,"
+ "Isgivereport AS 是否已出报告,IsRecCD AS 是否已刻光盘 From ImageInfo";
//定义数据表名称
private string MyTable = "ImageInfo";
//定义一个数据适配器对象
private SqlDataAdapter MySqlDataAdapter;
//定义数据连接对象
private SqlConnection connection;
//定义命令绑定对象
private SqlCommandBuilder MySqlCommandBulider;
//定义一个公用方法所在的对象
ImageadminProject.PublicFunction MyFunction = new ImageadminProject.PublicFunction();
public ImageOutput()
{
InitializeComponent();
}
private void ImageOutput_Load(object sender, EventArgs e)
{
//获得连接对象
connection = MyFunction.GetSqlConnection();
//获得数据适配器对象
MySqlDataAdapter = MyFunction.GetSqlDataAdapter(MySql);
//给数据适配器对象添加表映射
MySqlDataAdapter.TableMappings.Add("Table", MyTable);
//设置数据适配器对象的查询语句
MySqlDataAdapter.SelectCommand = new SqlCommand(MySql, connection);
//绑定数据适配器对象
MySqlCommandBulider = new SqlCommandBuilder(MySqlDataAdapter);
//填充数据集
MySqlDataAdapter.Fill(MyDataSet);
//赋予数据绑定对象的数据源属性
this.bindingSource1.DataSource = this.MyDataSet;
//赋予数据绑定对象的数据成员属性
this.bindingSource1.DataMember = MyTable;
//绑定显示和编辑数据的各种对象的数据源属性
this.dataGridView1.DataSource = this.bindingSource1;
this.textBoxImageNo.DataBindings.Add("Text", this.bindingSource1, "图像编号");
this.textBoxImagename.DataBindings.Add("Text", this.bindingSource1, "图像名称");
this.pictureBoxImagephoto.DataBindings.Add("Text", this.bindingSource1, "图像图片");
this.textBoxBelongproNo.DataBindings.Add("Text", this.bindingSource1, "所属项目工程");
this.textBoxImageheight.DataBindings.Add("Text", this.bindingSource1, "图像高度");
this.textBoxImagewidth.DataBindings.Add("Text", this.bindingSource1, "图像宽度");
this.textBoxROIHeight.DataBindings.Add("Text", this.bindingSource1, "ROI高度");
this.textBoxROIWidth.DataBindings.Add("Text", this.bindingSource1, "ROI宽度");
this.textBoxROIX.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxROIY.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxImageSaveas.DataBindings.Add("Text", this.bindingSource1, "图像位置");
this.textBoxImageassess.DataBindings.Add("Text", this.bindingSource1, "图像评定");
this.textBoxMemory.DataBindings.Add("Text", this.bindingSource1, "图像备注");
this.comboBoxIsgivereport.DataBindings.Add("Text", this.bindingSource1, "是否已出报告");
this.comboBoxIsRecCD.DataBindings.Add("Text", this.bindingSource1, "是否已刻光盘");
this.DisPlayNumber();
}
private void buttonSaveAS_Click(object sender, EventArgs e)
{
//定义一个保存文件的对话框
SaveFileDialog MySaveFileDialog = new SaveFileDialog();
//定义保存文件的类型
MySaveFileDialog.Filter = "JPeg Image(*.jpg)|*.jpg|Bitmap Image(*.bmp)|*.bmp|Gif Image(*.gif)|*.gif|All Image(*.*)|*.*";
MySaveFileDialog.Title = "影像图像另存为";
MySaveFileDialog.FilterIndex = 4;
//显示保存文本框
MySaveFileDialog.ShowDialog();
this.textBoxImageSaveas.Text = MySaveFileDialog.FileName;
this.pictureBoxImagephoto.Image.Save(MySaveFileDialog.FileName); //若将此句程序换为以下程序段,还是提示:未处理NullReferenceException;未将对象引用设置到对象的实例。请各位编程高手指正一下!谢谢!
//* if (MySaveFileDialog.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs = (System.IO.FileStream)MySaveFileDialog.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (MySaveFileDialog.FilterIndex)
{
case 1:
this.buttonSaveAS.Image.Save(fs,System.Drawing.Imaging. ImageFormat.Jpeg);
break;
case 2:
this.buttonSaveAS.Image.Save(fs, System.Drawing.Imaging. ImageFormat.Bmp);
break;
case 3:
this.buttonSaveAS.Image.Save(fs, System.Drawing.Imaging. ImageFormat.Gif);
break;
default:
this.buttonSaveAS.Image.Save(fs, System.Drawing.Imaging. ImageFormat.MemoryBmp);
break;
}
fs.Close();
} *//
}
//单击“首记录”按钮的执行代码
private void buttonMoveFirst_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
//单击“下一条”按钮的执行代码
private void buttonMoveNext_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position + 1 < this.bindingSource1.Count)
{
this.bindingSource1.MoveNext();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“上一条”按钮的执行代码
private void buttonMovePrevious_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position > 0)
{
this.bindingSource1.MovePrevious();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“尾记录”按钮的执行代码
private void buttonMoveLast_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
//显示记录数、总数
private void DisPlayNumber()
{
this.labelPosition.Text = "当前记录:" + (this.bindingSource1.Position + 1).ToString();
this.labelCount.Text = "总记录数:" + this.bindingSource1.Count.ToString();
}
//单击数据网格控件的记录时更新记录数和总数的显示
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.DisPlayNumber();
}
private void buttonExit_Click(object sender, EventArgs e)
{
//释放窗体
this.Close();
//释放资源
this.Dispose();
}
}
}
大家好!
最近我在使用Visual Studio 2005(c#)+SQL Server 2005开发一个图像数据库管理系统,其他功能窗体在《Visual Studio 2005+SQL Server 2005数据库应用系统开发》一书帮助下很快实现了,而在实现图像数据导出窗体,在点击图片导出命令按钮后,总是提示如下错误,这两天我换用了另一种实现方式,还是提示下面的错误:
未处理NullReferenceException
未将对象引用设置到对象的实例。
我真的不知如何修正这错误了,希望各位高手能抽点时间给予指教,不甚感激!!!
附
图像数据导出窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ImageadminProject.ImageManage
{
public partial class ImageOutput : Form
{
//定义一个数据集对象
private DataSet MyDataSet = new DataSet();
//定义查询使用的字符串
private string MySql = "Select ImageNo AS 图像编号,Imagename AS 图像名称,Imagephoto AS 图像图片,BelongproNo AS 所属项目工程,"
+ "Imageheight AS 图像高度,Imagewidth AS 图像宽度,ROIHeight AS ROI高度,ROIWidth AS ROI宽度,"
+ "ROIX AS ROI起点X坐标,ROIY AS ROI起点Y坐标,Imageposition AS 图像位置,Imageassess AS 图像评定,Memory AS 图像备注,"
+ "Isgivereport AS 是否已出报告,IsRecCD AS 是否已刻光盘 From ImageInfo";
//定义数据表名称
private string MyTable = "ImageInfo";
//定义一个数据适配器对象
private SqlDataAdapter MySqlDataAdapter;
//定义数据连接对象
private SqlConnection connection;
//定义命令绑定对象
private SqlCommandBuilder MySqlCommandBulider;
//定义一个公用方法所在的对象
ImageadminProject.PublicFunction MyFunction = new ImageadminProject.PublicFunction();
public ImageOutput()
{
InitializeComponent();
}
private void ImageOutput_Load(object sender, EventArgs e)
{
//获得连接对象
connection = MyFunction.GetSqlConnection();
//获得数据适配器对象
MySqlDataAdapter = MyFunction.GetSqlDataAdapter(MySql);
//给数据适配器对象添加表映射
MySqlDataAdapter.TableMappings.Add("Table", MyTable);
//设置数据适配器对象的查询语句
MySqlDataAdapter.SelectCommand = new SqlCommand(MySql, connection);
//绑定数据适配器对象
MySqlCommandBulider = new SqlCommandBuilder(MySqlDataAdapter);
//填充数据集
MySqlDataAdapter.Fill(MyDataSet);
//赋予数据绑定对象的数据源属性
this.bindingSource1.DataSource = this.MyDataSet;
//赋予数据绑定对象的数据成员属性
this.bindingSource1.DataMember = MyTable;
//绑定显示和编辑数据的各种对象的数据源属性
this.dataGridView1.DataSource = this.bindingSource1;
this.textBoxImageNo.DataBindings.Add("Text", this.bindingSource1, "图像编号");
this.textBoxImagename.DataBindings.Add("Text", this.bindingSource1, "图像名称");
this.pictureBoxImagephoto.DataBindings.Add("Text", this.bindingSource1, "图像图片");
this.textBoxBelongproNo.DataBindings.Add("Text", this.bindingSource1, "所属项目工程");
this.textBoxImageheight.DataBindings.Add("Text", this.bindingSource1, "图像高度");
this.textBoxImagewidth.DataBindings.Add("Text", this.bindingSource1, "图像宽度");
this.textBoxROIHeight.DataBindings.Add("Text", this.bindingSource1, "ROI高度");
this.textBoxROIWidth.DataBindings.Add("Text", this.bindingSource1, "ROI宽度");
this.textBoxROIX.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxROIY.DataBindings.Add("Text", this.bindingSource1, "ROI起点X坐标");
this.textBoxImageSaveas.DataBindings.Add("Text", this.bindingSource1, "图像位置");
this.textBoxImageassess.DataBindings.Add("Text", this.bindingSource1, "图像评定");
this.textBoxMemory.DataBindings.Add("Text", this.bindingSource1, "图像备注");
this.comboBoxIsgivereport.DataBindings.Add("Text", this.bindingSource1, "是否已出报告");
this.comboBoxIsRecCD.DataBindings.Add("Text", this.bindingSource1, "是否已刻光盘");
this.DisPlayNumber();
}
private void buttonSaveAS_Click(object sender, EventArgs e)
{
//定义一个保存文件的对话框
SaveFileDialog MySaveFileDialog = new SaveFileDialog();
//定义保存文件的类型
MySaveFileDialog.Filter = "JPeg Image(*.jpg)|*.jpg|Bitmap Image(*.bmp)|*.bmp|Gif Image(*.gif)|*.gif|All Image(*.*)|*.*";
MySaveFileDialog.Title = "影像图像另存为";
MySaveFileDialog.FilterIndex = 4;
//显示保存文本框
MySaveFileDialog.ShowDialog();
this.textBoxImageSaveas.Text = MySaveFileDialog.FileName;
this.pictureBoxImagephoto.Image.Save(MySaveFileDialog.FileName); //若将此句程序换为以下程序段,还是提示:未处理NullReferenceException;未将对象引用设置到对象的实例。请各位编程高手指正一下!谢谢!
//* if (MySaveFileDialog.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs = (System.IO.FileStream)MySaveFileDialog.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (MySaveFileDialog.FilterIndex)
{
case 1:
this.buttonSaveAS.Image.Save(fs,System.Drawing.Imaging. ImageFormat.Jpeg);
break;
case 2:
this.buttonSaveAS.Image.Save(fs, System.Drawing.Imaging. ImageFormat.Bmp);
break;
case 3:
this.buttonSaveAS.Image.Save(fs, System.Drawing.Imaging. ImageFormat.Gif);
break;
default:
this.buttonSaveAS.Image.Save(fs, System.Drawing.Imaging. ImageFormat.MemoryBmp);
break;
}
fs.Close();
} *//
}
//单击“首记录”按钮的执行代码
private void buttonMoveFirst_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
//单击“下一条”按钮的执行代码
private void buttonMoveNext_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position + 1 < this.bindingSource1.Count)
{
this.bindingSource1.MoveNext();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveFirst();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“上一条”按钮的执行代码
private void buttonMovePrevious_Click(object sender, EventArgs e)
{
if (this.bindingSource1.Position > 0)
{
this.bindingSource1.MovePrevious();
this.DisPlayNumber();
}
else
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
}
//单击“尾记录”按钮的执行代码
private void buttonMoveLast_Click(object sender, EventArgs e)
{
this.bindingSource1.MoveLast();
this.Invalidate();
this.DisPlayNumber();
}
//显示记录数、总数
private void DisPlayNumber()
{
this.labelPosition.Text = "当前记录:" + (this.bindingSource1.Position + 1).ToString();
this.labelCount.Text = "总记录数:" + this.bindingSource1.Count.ToString();
}
//单击数据网格控件的记录时更新记录数和总数的显示
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.DisPlayNumber();
}
private void buttonExit_Click(object sender, EventArgs e)
{
//释放窗体
this.Close();
//释放资源
this.Dispose();
}
}
}