感谢您使用微软产品。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 with existing Flashcards
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))。
1,获取image字段的值,并转化为byte[]数组;
2,然后建立一个FileStream对象,并将byte[]数值写到一个临时的图象文件;
3,关闭FileStream对象,并在PictureBox控件中显示获得的临时图象文件。
下面提供一段示例代码,供您参考:
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
// Fill a DataSet with existing Flashcards
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))。
解决方案 »
- c#窗体应用程序如何通过trackbar控制麦克风音量和系统音量?
- C#中多线程的问题
- 受保护的构造函数有什么用
- 怎样才能吧ListBox里的值提取 放到数组里啊
- panel上添加了一些控件 ,如何做panel的点击事件
- Session超时后,能否在服务器端执行一些相关的程序?
- 如何用c#获取QQ登录登框中的密码。善意,不是想偷QQ,是公司做一保护程序用,兄弟帮忙,谢谢了。
- 有Entity Framework的书籍推荐吗?
- 求大神解答这2个字符是如何转换出来的
- VB.net语句转成C#语句问题,白给分啦。
- 如何返回SqlDataReader中某一个字段的值
- 关于crystalreport的问题
谢谢acptvb(微软全球技术中心 VB技术支持),
同时谢谢 vod_sys (VOD_Sys) 你提出了,
现在解决了。
OK!
SqlCommand logoCMD = new SqlCommand("SELECT picture FROM jmdbf where clipid='111'", pubsConn); 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. pubsConn.Open();
SqlDataReader myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess);
myReader.Read();
startIndex = 0;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
ArrayList ar=new ArrayList();
ar.AddRange(outbyte);
while (retval == bufferSize)
{
startIndex+= bufferSize;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
ar.AddRange(outbyte);
}
byte[] mm=new Byte[retval];
for(int ii=0;ii<retval;ii++)
mm[ii]=outbyte[ii];
ar.AddRange(mm);
byte[] m_b=new byte[ar.Count];
ar.CopyTo(0,m_b,0,ar.Count);
System.IO.MemoryStream stream = new System.IO.MemoryStream(m_b, true);
stream.Write(m_b, 0, m_b.Length);
DrawToScale(new Bitmap(stream)); myReader.Close();
pubsConn.Close();
{
// The client rectangle
Rectangle rc = pictureBox1.ClientRectangle; // From Programming Windows with C#, by Charles Petzold
// Figure out the scaling necessary for the image
SizeF size = new SizeF( bmp.Width / bmp.HorizontalResolution, bmp.Height / bmp.VerticalResolution);
float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height); size.Width *= fScale;
size.Height *= fScale; // Create a new bitmap of the proper size for the existing bitmap
// and assign it to the picture box
pictureBox1.Image = new Bitmap(bmp, size.ToSize());
}