以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 SVG/GML/VRML/X3D/XAML 』  (http://bbs.xml.org.cn/list.asp?boardid=21)
----  怎样把存在数据库的图片数据<binary>转化成ImageBox可以有的  (http://bbs.xml.org.cn/dispbbs.asp?boardid=21&rootid=&id=18303)


--  作者:feiniao_21
--  发布时间:5/14/2005 5:07:00 PM

--  怎样把存在数据库的图片数据<binary>转化成ImageBox可以有的
SqlCommand cmd;
   cmd = new SqlCommand("INSERT INTO photodata (ID, data) " +
    "VALUES (@CustomerID, @CompanyName)", cnn);

   cmd.Parameters.Add("@CustomerID", SqlDbType.Int, 5);
   cmd.Parameters.Add("@CompanyName", SqlDbType.Image, 69812);
   cmd.Parameters[0].Value= 1;
   cmd.Parameters[1].Value=b;     

byte[]    b=(byte[])dr["data"];
怎样把 b 变成 bt=new Bitmap();bitmap()构造函数可以用的??
Stream 类型!

兄弟帮忙啊,小弟感激不尽!!


--  作者:edison1024
--  发布时间:5/16/2005 9:23:00 AM

--  
这是MSDN的例子。以后要养成查帮助的习惯哦。

SqlConnection pubsConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;");
SqlCommand logoCMD = new SqlCommand("SELECT pub_id, logo FROM pub_info", pubsConn);

FileStream fs;                          // Writes the BLOB to a file (*.bmp).
BinaryWriter bw;                        // Streams the BLOB to the FileStream object.

int bufferSize = 100;                   // Size of the BLOB buffer.
byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.
long retval;                            // The bytes returned from GetBytes.
long startIndex = 0;                    // The starting position in the BLOB output.

string pub_id = "";                     // The publisher id to use in the file name.

// Open the connection and read data into the DataReader.
pubsConn.Open();
SqlDataReader myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess);

while (myReader.Read())
{
  // Get the publisher id, which must occur before getting the logo.
  pub_id = myReader.GetString(0);  

  // Create a file to hold the output.
  fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
  bw = new BinaryWriter(fs);

  // Reset the starting byte for the new BLOB.
  startIndex = 0;

  // Read the bytes into outbyte[] and retain the number of bytes returned.
  retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

  // Continue reading and writing while there are bytes beyond the size of the buffer.
  while (retval == bufferSize)
  {
    bw.Write(outbyte);
    bw.Flush();

    // Reposition the start index to the end of the last buffer and fill the buffer.
    startIndex += bufferSize;
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
  }

  // Write the remaining buffer.
  bw.Write(outbyte, 0, (int)retval - 1);
  bw.Flush();

  // Close the output file.
  bw.Close();
  fs.Close();
}

// Close the reader and the connection.
myReader.Close();
pubsConn.Close();


--  作者:feiniao_21
--  发布时间:5/17/2005 3:57:00 PM

--  
很感谢楼上的回答!

我想从数据库里查到这些数据,让它直接在控件PictureBoxl里显示

但,我不想保存那些从数据库中的<binary>数据!。


PictureBox.Image,需要一个Image类型的数据,而,在数据库中定义的字段本来就为Image的类型!(应该说PictureBox.iImage=(Image)bytes是可以的)
我不知道什么直接利用数据库的数据可以让PictureBox.Image可以用的!

public void init(string url)
  {  
   try
   {
                  bmap=new Bitmap(url);
   }
   catch(Exception e)
   {
    MessageBox.Show(e.Message);

   }   
   pictureBox1.Image=(Image) bmap;
  }  


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms