DrAddCahier[0] = PicureBox1.Image ;
引用对象好像不能直接赋值吧!试试这个吧!
//图片字段的赋值
if(pictureBox.Image !=null)
{
//新建一个内存流
MemoryStream ms = new MemoryStream ();
//把图片内容存到内存流中去
pictureBox.Image .Save (ms,System.Drawing .Imaging.ImageFormat .Bmp );
//把内存流中的内容按字节读入数组
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
myRow["图片"] = myData;
ms.Close ();
}
引用对象好像不能直接赋值吧!试试这个吧!
//图片字段的赋值
if(pictureBox.Image !=null)
{
//新建一个内存流
MemoryStream ms = new MemoryStream ();
//把图片内容存到内存流中去
pictureBox.Image .Save (ms,System.Drawing .Imaging.ImageFormat .Bmp );
//把内存流中的内容按字节读入数组
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
myRow["图片"] = myData;
ms.Close ();
}
解决方案 »
- 给类或借口添加什么特性能让他们在VS的智能感知中是隐藏的
- 还有一道题!真的不知道该怎么下手!请各位大牛们帮帮忙!
- 想寻找PDM软件,来管理公司的图纸
- 请问Process.Close和Process.Kill有什么区别
- 红外线编程---写一个自定义的红外线传输程序将PC上的文件通过红处线传送到j2me手机设备当中
- 大家好:两个主从表,怎样用datagrid显示出来,并带有编辑,删除按钮
- delphi做的程序A使用sendmessage向C#做的程序B发消息,b没反应
- 请开发C/S的高手指教,客户端链接数据库的字符串大家都怎么保存?
- System.Data.ConstraintException:未能启用约束,一行或多行违反非空唯一或外键约束如何解决!
- 急!请问?
- 数据库问题!弄了一天了!不得不来这里请教!
- VC#中的一个小问题
string ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Password="""";User ID=Admin;Data Source=" + Application.StartupPath +
@"\db.mdb";
string SQL="select * from 文件";
OleDbConnection conn=new OleDbConnection(ConnectionString);
OleDbDataAdapter da=new OleDbDataAdapter(SQL,conn);
OleDbCommandBuilder cmd=new OleDbCommandBuilder(da);
DataSet ds=new DataSet(); conn.Open();
da.Fill(ds,"文件");
DataTable dt=ds.Tables[0];
DataRow dr;
if(dt.Rows.Count>0)
dr=dt.Rows[0];
else
dr=dt.NewRow(); dr[1]=txtFile.Text;
FileStream fs=File.Open(txtFile.Text,FileMode.Open,FileAccess.Read);
byte[] by=new byte[fs.Length];
fs.Read(by,0,(int)fs.Length);
dr[2]=by;
if(dt.Rows.Count<=0)
dt.Rows.Add(dr);
da.Update(ds, "文件"); conn.Close();
fs.Close();
MessageBox.Show("保存成功!");
}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message);
}
从数据库中读出文件并保存到磁盘:
string ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Password="""";User ID=Admin;Data Source=" + Application.StartupPath +
@"\db.mdb";
string SQL="select * from 文件";
OleDbConnection conn=new OleDbConnection(ConnectionString);
OleDbCommand cmd=conn.CreateCommand();
cmd.CommandText=SQL;
conn.Open();
OleDbDataReader dr=cmd.ExecuteReader(); if(dr.Read())
{
string strFile=string.Empty;
strFile=dr.GetString(1);
byte[] by=(byte[])dr.GetValue(2); FileStream fs=File.Open(strFile,FileMode.Create,FileAccess.Write);
fs.Write(by,0,by.Length);
fs.Close();
MessageBox.Show("读取成功!");
}
dr.Close();
conn.Close();
以MS自带的数据库Northwnd为例,其中有个表是Categories,有四个四段,其中有一个是Image类型的Picture字段.我们首先添加一张bmp图片到最后一行的Picture中,然后在读出来显示到Image控件中.添加一个SqlDataAdapter1,用向导设置联接数据库为Northwnd,SQL语句为SELECT [Category ID], [Category Name], Description, Picture FROM Categories.生成一个数据集为dataset1. 然后添加两个按钮分别表示写图片到数据库和读数据库,还有一个Image控件用于显示图片.添加以下代码Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
End Sub'从数据库读取图片暂时存储为monkey.bmp,然后加载到image控件里面.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPicFromDb.Click
Try
Dim data As Byte() = DataSet11.Tables(0).Rows(7).Item(3)
Dim myfilestream As New System.IO.FileStream(Application.StartupPath & "\monkey.bmp", IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
PictureBox1.Image = New Bitmap(Application.StartupPath & "\monkey.bmp")
Catch
End Try
End Sub
'把C:\6.bmp写入库中,你可以改为自己的图片.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertPicToDb.Click
Dim myfilestream As New System.IO.FileStream("c:\6.bmp", IO.FileMode.Open)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
DataSet11.Tables(0).Rows(7).Item(3) = data
SqlDataAdapter1.Update(DataSet11.GetChanges())
End Sub