串行化吧。
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatter.Binary;MemoryStream ms=new MemoryStream();
BinaryFormatter bf=new BinaryFormatter();Image a=pictureBox1.Image;
bf.Serialize(ms,a); // 串行化ing length=ms.Length; // 转化成byte[]
byte[] imagebyte=new byte[length];
imagebyte=ms.GetBuffers();
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatter.Binary;MemoryStream ms=new MemoryStream();
BinaryFormatter bf=new BinaryFormatter();Image a=pictureBox1.Image;
bf.Serialize(ms,a); // 串行化ing length=ms.Length; // 转化成byte[]
byte[] imagebyte=new byte[length];
imagebyte=ms.GetBuffers();
解决方案 »
- 如何终止指定名称的线程?
- 我用C# socekt的接受数据!!
- 请教一个关于ListViewItem的问题
- 请问现在有开放的qq开发接口吗?谁有相关资料?
- console程序如何获取页面源文件
- 不能用代码访问SQL 2000 sever报错
- BinaryWriter、BinaryReader 为何得不到相应的数据值??
- 跟操作系统有关否?
- 300分相送!怎么在WebBrowser中嵌入word!
- 在richtextbox控件中,怎样在上面显示文字,而下面显示图片???
- 请问DATAVIEW或TABLE中如何得到当前记录的索引(index)??
- (200分请教)datagrid中数据列的动态改变
http://www.aspcool.com/lanmu/browse1.asp?ID=830&bbsuser=asp
1,创建一个FileStream对象,通过该FileStream对象来读取指定的image文件;
2,根据生成FileStream对象,创建byte[]数据,通过FileStream对象的Read方法将数据写入byte[]数组中;
3,将上述byte[]数组赋予数据表的image字段,写入数据表中;下面提供一段示例程序,供您参考:
private void btnAdd_Click(object sender, System.EventArgs e)
{
// Fill a DataSet
DataSet ds = new DataSet();
string connString = "Server=SHA-RICKIE-01;DataBase=test;uid=user;pwd=user";
string sqlString = "Select * from Students";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlString,conn);
SqlCommandBuilder MyCB = new SqlCommandBuilder(sqlDataAdapter1); sqlDataAdapter1.Fill(ds, "students");
DataTable TheTable = ds.Tables[0];
// Create a new row in Memory
DataRow aRow = TheTable.NewRow();
// Insert the information from the dialog into the Table
aRow["name"] = txtboxname.Text;
aRow["age"] = txtboxage.Text;
// 创建一个FileStream对象用来读取image文件,其中FileName 为该image的完整文件名
string FileName = txtboxFilename.Text;
FileStream fs = new FileStream (FileName, FileMode.OpenOrCreate,
FileAccess.Read);
// Read the Data into the Byte Array
byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, (int)fs.Length);
fs.Close();
// Assign the DataRow Picture Column to the Byte Array to populate it in the DataRow
aRow["image"] = MyData;
// Add the DataRow to the DataTable
TheTable.Rows.Add(aRow);
// 写数据记录到数据库表中
sqlDataAdapter1.Update(ds, "students");
MessageBox.Show("A student added.");
}
关于FileStream类的更详细信息,请参考MSDN:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemIOFileStreamMembersTopic.htm — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
1,获取image字段的值,并转化为byte[]数组;
2,然后建立一个FileStream对象,并将byte[]数值写到一个临时的图象文件;
3,关闭FileStream对象,并在PictureBox控件中显示获得的临时图象文件。
下面提供一段示例代码,供您参考:
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
// Fill a DataSet
DataSet ds = new DataSet();
string connString = "Server=SHA-RICKIE-01;DataBase=test;uid=user;pwd=user";
string sqlString ="Select * from Students";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlString,conn); sqlDataAdapter1.Fill(ds, "students");
DataRow myRow;
myRow = ds.Tables["students"].Rows[0];
byte[] myData = new byte[0];
if(myRow["image"].ToString()!="")
myData = (byte[])myRow["image"];
else
{
MessageBox.Show("The image field is null.");
return;
}
int ArraySize = new int();
ArraySize = myData.GetUpperBound(0); // Create a Filestream for writing the byte array to a image file
FileStream fs = new FileStream("tmpImage", FileMode.OpenOrCreate, FileAccess.Write);
// Write the stream of data that we read in from the database to the filestream
// and close it.
// This will create the file tmpImage, which we can use to display in the picturebox
// Writes a block of bytes to this stream using data from a buffer
fs.Write(myData, 0,ArraySize+1);
fs.Close(); // Assign the temporary image file to the picture box
pictureBox1.Image = new Bitmap("tmpImage");
}
— 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
myData = (byte[])myRow["image"];
报告说:
指定的转换无效,为什么?
上述代码在如下的测试环境中运行通过:
数据库:MS SQL Server 2000
数据表students结构如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[students]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[students]
GOCREATE TABLE [dbo].[students] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[age] [int] NULL ,
[image] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
开发环境:
Windowx XP Professional
Visual Studio.Net 2002 Version 7.0.9466
Microsoft .NET Framework 1.0 Version 1.0.3705 — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。