串行化吧。
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();

解决方案 »

  1.   

    这篇文章肯定可以解决这个问题:
    http://www.aspcool.com/lanmu/browse1.asp?ID=830&bbsuser=asp
      

  2.   

    谢谢楼上的!现在的问题是:我把数据读取到DS中,该数据是ds.Tables[0].Rows[i]["照片"],我在WEB成功的应用了你上面的方法,但在WIN应用中却说不能实现转变数据类型。
      

  3.   

    感谢您使用微软产品。在C#中,可以通过如下的步骤来实现将image文件写入数据表的image字段:
    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))。
      

  4.   

    感谢您使用微软产品。C#里获取SQL数据表的IMAGE类型字段内容(到PictureBox控件):
    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))。
      

  5.   

    上面的:
    myData = (byte[])myRow["image"];
    报告说:
    指定的转换无效,为什么?
      

  6.   

    感谢您使用微软产品。问题可能是您的数据表中的image字段的数据内容存在问题,建议先通过上面的步骤:实现将image文件写入数据表的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))。
      

  7.   

    能保证该数据是存在的。我是通过ASPX文件把字段中图形输出的,如我所预期的,但使用C#就报告了错误。