基本方法就是读写流文件,源程序见下,读取添加到一个按钮上:
private void button_Click(object sender, System.EventArgs e)
{
byte[] buff;
string sCommand="SELECT * FROM 表名";
string sConn="server=localhost;uid=sa;pwd=;database=数据库名";
SqlConnection myConn = new SqlConnection(sConn);
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.SelectCommand = new SqlCommand(sCommand, myConn);
SqlCommandBuilder custCB = new SqlCommandBuilder(myDataAdapter);
myConn.Open();
DataSet dst= new DataSet();
myDataAdapter.Fill(dst,"testpic");
DataTable dt=dst.Tables["testpic"];
DataRow sRow=dt.NewRow();
sRow=dt.Rows[dt.Rows.Count-1];
buff=(byte[])sRow["pic"];//从存picture的列中读取
picstream=new MemoryStream(buff);
}
private void button_Click(object sender, System.EventArgs e)
{
byte[] buff;
string sCommand="SELECT * FROM 表名";
string sConn="server=localhost;uid=sa;pwd=;database=数据库名";
SqlConnection myConn = new SqlConnection(sConn);
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
myDataAdapter.SelectCommand = new SqlCommand(sCommand, myConn);
SqlCommandBuilder custCB = new SqlCommandBuilder(myDataAdapter);
myConn.Open();
DataSet dst= new DataSet();
myDataAdapter.Fill(dst,"testpic");
DataTable dt=dst.Tables["testpic"];
DataRow sRow=dt.NewRow();
sRow=dt.Rows[dt.Rows.Count-1];
buff=(byte[])sRow["pic"];//从存picture的列中读取
picstream=new MemoryStream(buff);
}
xxx就是你Image类型的图像。可惜,没有数据Bind的PictureBox
private void button_Click(object sender, System.EventArgs e)
{
Image im=Image.FromStream(picstream);
pictureBox1.Image=im;
}
private void button1_Click(object sender, System.EventArgs e)
{
byte[] buff;
DataRow dr=ds.Tables [0].Rows[dataGrid1.CurrentCell.RowNumber];
buff=(byte[])dr["Photo"];
MemoryStream pms=new MemoryStream(buff);
Image im=Image.FromStream(pms);
this.pictureBox1.Image=im;
}但出现错误,如下:未处理的“System.ArgumentException”类型的异常出现在 system.drawing.dll 中其他信息:使用了无效参数。
private void button1_Click(object sender, System.EventArgs e)
{
byte[] buff;
DataRow dr=ds.Tables [0].Rows[dataGrid1.CurrentCell.RowNumber];
buff=(byte[])dr["Photo"];
MemoryStream pms=new MemoryStream(buff);
Image im=Image.FromStream(pms);
this.pictureBox1.Image=im;
}但出现错误,如下:未处理的“System.ArgumentException”类型的异常出现在 system.drawing.dll 中其他信息:使用了无效参数。
不对,因该为:
pms=new MemoryStream(buff);在你的
public class Form1
{
private MemoryStream pws;
}
我用的是northwind中的photo字段
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace DataGrid1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private DataSet ds=new DataSet ();
private MemoryStream pms;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=ANDY;initial catalog=Northwind;persist security info=False;user id=sa" +
";workstation id=ANDY;packet size=4096";
this.sqlDataAdapter1.Connection=this.sqlConnection1;
this.sqlDataAdapter1.SelectCommand = "SELECT EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDa" +
"te, Address, City, Region, PostalCode, Country, HomePhone, Extension, Photo, Not" +
"es, ReportsTo, PhotoPath FROM Employees";
;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(408, 256);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(440, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(432, 112);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(176, 144);
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(616, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.pictureBox1,
this.button1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
this.sqlDataAdapter1 .Fill(ds,"Employees");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="Employees";
}
private void button1_Click(object sender, System.EventArgs e)
{
byte[] buff;
DataRow dr=ds.Tables [0].Rows[dataGrid1.CurrentCell.RowNumber];
buff=(byte[])dr["Photo"];
pms=new MemoryStream(buff);
Image im=Image.FromStream(pms);
this.pictureBox1.Image=im;
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace DataGrid1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private DataSet ds=new DataSet ();
private MemoryStream pms;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=ANDY;initial catalog=Northwind;persist security info=False;user id=sa" +
";workstation id=ANDY;packet size=4096";
this.sqlDataAdapter1.Connection=this.sqlConnection1;
this.sqlDataAdapter1.SelectCommand = "SELECT EmployeeID, LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDa" +
"te, Address, City, Region, PostalCode, Country, HomePhone, Extension, Photo, Not" +
"es, ReportsTo, PhotoPath FROM Employees";
;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(408, 256);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(440, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(432, 112);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(176, 144);
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(616, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.pictureBox1,
this.button1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
this.sqlDataAdapter1 .Fill(ds,"Employees");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="Employees";
}
private void button1_Click(object sender, System.EventArgs e)
{
byte[] buff;
DataRow dr=ds.Tables [0].Rows[dataGrid1.CurrentCell.RowNumber];
buff=(byte[])dr["Photo"];
pms=new MemoryStream(buff);
Image im=Image.FromStream(pms);
this.pictureBox1.Image=im;
}
}
}
private void button1_Click(object sender, System.EventArgs e)
里吧
我可以取到buff值,转换成image时就出问题了
但换为northwind 库中的employees表中的photo就会出现上面的问题,
好奇怪
在pub_info中存的是jpg的格式