例子上不是很清楚吗? // Get bytes return from stored proc byte[] b = (byte[])cmd.ExecuteScalar(); if(b.Length > 0) { // Open a stream for the image and write the bytes into it System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true); stream.Write(b, 0, b.Length);
// Create a bitmap from the stream Bitmap bmp = new Bitmap(stream); // Check for scaling and assign the bitmap to the Picturebox if( bmp.Width > 500 && bmp.Height > 300) { Bitmap bmp1 = new Bitmap(bmp, new Size(500,300)); pictureBox.Image = bmp1; } else pictureBox.Image = bmp;
能不能把原代码发过来 共享一下啊
程序里头用byte[]类型存入图片数据;
显示在picturebox时候用Bitmap类型
这一步能具体点说吗?
// Get bytes return from stored proc
byte[] b = (byte[])cmd.ExecuteScalar();
if(b.Length > 0)
{
// Open a stream for the image and write the bytes into it
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(b, 0, b.Length);
// Create a bitmap from the stream
Bitmap bmp = new Bitmap(stream); // Check for scaling and assign the bitmap to the Picturebox
if( bmp.Width > 500 && bmp.Height > 300)
{
Bitmap bmp1 = new Bitmap(bmp, new Size(500,300));
pictureBox.Image = bmp1;
}
else
pictureBox.Image = bmp;
// Close the stream
stream.Close();
}
//图片保存
//ImageToByte(Image img) 将图片转换成二进制代码,然后存储在数据库中
public static byte[] ImageToByte(Image img)
{
byte[] byt = null;
ImageConverter imgCvt = new ImageConverter();
object obj = imgCvt.ConvertTo(img, typeof(byte[]));
byt = (byte[])obj;
return byt;
}
将返回的二进制流用参数的形式存入数据库
comm.Parameters.Add("@Photo", SqlDbType.Image);
comm.Parameters["@Photo"].Value = byt;读取图片:先将后台数据库中的图象二进制流转换成图象文件,然后再赋值给图片框
private void ShowPic()
{
string strSql = "Select GdsPhoto from AchGoods where GdsID = '"+ txtID.Text +"'";
comm.CommandText = strSql;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
//判断读取是否成功
if(reader.Read())
{
if (reader["GdsPhoto"] == DBNull.Value)
{
reader.Close();
picGoods.Image = null;
return;
}
byte[] data = ((byte[])reader["Photo"]);
picGoods.Image = CommonClass.CommonFun.ByteToImage(data);
picGoods.Refresh(); reader.Close();
}
else
{
MessageBox.Show("没有成功读入数据!") ;
}
conn.Close();
}
//ByteToImage(byte[] byt)读取数据库中的二进制文件将其转换为Image
public static Image ByteToImage(byte[] bytImage)
{
Image img = null;
ImageConverter imgCvt = new ImageConverter(); object obj = imgCvt.ConvertFrom(bytImage);
img = (Image)obj;
return img;
}