在 Visual C# 中直接将一个图片从数据库复制到 PictureBox 控件
2007-03-31 08:52
375 查看
示例
loadTOCNode(2, 'summary');1. | 使用以下结构创建一个 SQL Server 或 Access 表:[code]CREATE TABLE BLOBTest ( BLOBID INT IDENTITY NOT NULL, BLOBData IMAGE NOT NULL ) [/code] |
2. | 打开 Visual Studio .NET,然后新建一个 Visual C# Windows 应用程序项目。 |
3. | 从工具箱向默认的 Form1 添加一个 PictureBox 和两个 Button 控件。将 Button1 的 Text 属性设置为 File to Database,并将 Button2 的 Text 属性设置为 Database to PictureBox。 |
4. | 在窗体的代码模块顶部插入 using 语句:[code]using System.Data.SqlClient; using System.IO; using System.Drawing.Imaging; [/code] |
5. | 将以下数据库连接字符串的声明添加到 public class Form1 :System.Windows.Forms.Form 类声明中,并根据需要调整连接字符串:[code] String strCn = "Data Source=localhost;integrated security=sspi;initial catalog=mydata"; [/code] |
6. | 将下面的代码插入 Button1 (File to Database) 的 Click 事件过程中。根据需要调整到一个可用示例图像文件的可用路径。此代码可将图像文件从磁盘(使用一个 FileStream 对象)读入 Byte 数组,然后使用一个参数化的 Command 对象将数据插入数据库。[code]try { SqlConnection cn = new SqlConnection(strCn); SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn); String strBLOBFilePath = @"C:/blue hills.jpg";//Modify this path as needed. //Read jpg into file stream, and from there into Byte array. FileStream fsBLOBFile = new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read); Byte[] bytBLOBData = new Byte[fsBLOBFile.Length]; fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length); fsBLOBFile.Close(); //Create parameter for insert command and add to SqlCommand object. SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, bytBLOBData); cmd.Parameters.Add(prm); //Open connection, execute query, and close connection. cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }catch(Exception ex) {MessageBox.Show(ex.Message);} [/code] |
7. | 将下面的代码插入 Button2 (Database to PictureBox) 的 Click 事件过程。此代码将行从数据库中的 BLOBTest 表检索到一个数据集,复制最新添加的图像到 Byte 数组,然后到 MemoryStream 对象,接着将 MemoryStream 加载到 PictureBox 控件的 Image 属性。[code]try { SqlConnection cn = new SqlConnection(strCn); cn.Open(); //Retrieve BLOB from database into DataSet. SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "BLOBTest"); int c = ds.Tables["BLOBTest"].Rows.Count; if(c>0) { //BLOB is read into Byte array, then used to construct MemoryStream, //then passed to PictureBox. Byte[] byteBLOBData = new Byte[0]; byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]); MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); pictureBox1.Image= Image.FromStream(stmBLOBData); } cn.Close(); } catch(Exception ex) {MessageBox.Show(ex.Message);} [/code] |
8. | 按 F5 键编译并运行该项目。 |
9. | 单击 File to Database 按钮将至少一个示例图像加载到数据库。 |
10. | 单击 Database to PictureBox 按钮将保存的图像显示在 PictureBox 控件中。 |
11. | 如果想能够直接将图像从 PictureBox 控件插入数据库,则请添加第三个 Button 控件,并将下面的代码插入其 Click 事件过程。此代码将图像数据从 PictureBox 控件检索到 MemoryStream 对象,将 MemoryStream 复制到一个 Byte 数组,然后使用一个参数化的 Command 对象将 Byte 数组保存到数据库。[code]try { SqlConnection cn = new SqlConnection(strCn); SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn); //Save image from PictureBox into MemoryStream object. MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); //Read from MemoryStream into Byte array. Byte[] bytBLOBData = new Byte[ms.Length]; ms.Position = 0; ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length)); //Create parameter for insert statement that contains image. SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 0, 0,null, DataRowVersion.Current, bytBLOBData); cmd.Parameters.Add(prm); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }catch(Exception ex) {MessageBox.Show(ex.Message);} [/code] |
12. | 运行该项目。单击 Database to PictureBox 按钮以显示刚才在 PictureBox 控件中保存过的图像。单击新添加的按钮将此图像从 PictureBox 保存到数据库。然后再次单击 Database to PictureBox 按钮以确认图像已正确保存。 |
相关文章推荐
- HOW TO:在 Visual C# 中直接将一个图片从数据库复制到 PictureBox 控件
- Flex中如何利用Bitmap和BitmapData类复制图片到一个TileList控件的例子
- 存在数据库里的图片直接显示到PictureBox中
- 一个简单的图片轮播功能(图片自动播放,点击控件可直接跳转对对应图片)
- C#将数据库图片显示在pictureBox
- 从数据库中读取一个图片并保存为一个图片文件[转]
- 把一个数据库中的表复制到另外一个数据库
- Android ListView控件显示数据库中图片
- TimesTen 数据库复制学习:16. 一个缓存组,复制,客户端自动切换的串烧实验
- 【MYSQL备份】利用mysqldump将一个数据库复制到另一个数据库
- SQL 2000中将一个数据库中的表复制到另一个数据库中去
- sqlHelper封装类,用于对数据库的操作,此类将查询操作中返回的结果集传递给了一个数组,直接将资源关闭
- 把一个数据库的一张表复制到另一个数据库
- C# 两PictureBox图片交换(ImageLacation) 选中边框 外部图片可直接拖入到PictureBox中显示
- mysql通过直接复制数据库文件夹来还原数据时需要注意的问题
- sqlserver -- 学习笔记(四)将一个数据库的表复制到另外一个数据库(备忘)
- LoadImage一个从网络访问图片,并存到缓存(内存,磁盘)直接可以用
- MS SQL中复制一张表到另外一个数据库中一张表
- MySQL复制一个数据库(实现方案)
- bootstrap-wysiwyg中JS控件富文本中的图片由本地上传到服务器(阿里云、七牛、自己的数据库)