C# 在SQLite数据库中存储图像 z
C# 在SQLite数据库中存储图像
建表语句
CREATE TABLE [ImageStore]([ImageStore_Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,[ImageFile] NVARCHAR(20) NULL,[ImageBlob] BLOB NULL
);
加载图像
privateImageLoadImage(){//Create an instance of the Image Class/Object//so that we can store the information about the picture an send it back for//processing into the database.Image image =null;//Ask user to select ImageOpenFileDialog dlg =newOpenFileDialog();
dlg.InitialDirectory=@"C:\\";
dlg.Title="Select Image File";//dlg.Filter = "Tag Image File Format (*.tiff)|*.tiff";//dlg.Filter += "|Graphics Interchange Format (*.gif)|*.gif";//dlg.Filter += "|Portable Network Graphic Format (*.png)|*.png";//dlg.Filter += "|Joint Photographic Experts Group Format (*.jpg)|*.jpg";//dlg.Filter += "|Joint Photographic Experts Group Format (*.jpeg)|*.jpeg";//dlg.Filter += "|Nikon Electronic Format (*.nef)|*.nef";//dlg.Filter += "|All files (*.*)|*.*";
dlg.Filter="Image Files (*.jpg ; *.jpeg ; *.png ; *.gif ; *.tiff ; *.nef)
|*.jpg;*.jpeg;*.png;*.gif;*.tiff;*.nef";
dlg.ShowDialog();this.FileLocation= dlg.FileName;if(fileLocation ==null|| fileLocation ==string.Empty)return image;if(FileLocation!=string.Empty&& fileLocation !=null){Cursor.Current=Cursors.WaitCursor;//Get file information and calculate the filesizeFileInfo info =newFileInfo(FileLocation);long fileSize = info.Length;//reasign the filesize to calculated filesize
maxImageSize =(Int32)fileSize;if(File.Exists(FileLocation)){//Retreave image from file and binary it to Object imageusing(FileStream stream =File.Open(FileLocation,FileMode.Open)){BinaryReader br =newBinaryReader(stream);byte[] data = br.ReadBytes(maxImageSize);
image =newImage(dlg.SafeFileName, data, fileSize);}}Cursor.Current=Cursors.Default;}return image;}
存储图像
publicInt32InsertImage(){DataRow dataRow =null;
isSucces =false;Image image =LoadImage();//if no file was selected and no image was created return 0if(image ==null)return0;if(image !=null){// Determin the ConnectionStringstring connectionString = dBFunctions.ConnectionStringSQLite;// Determin the DataAdapter = CommandText + Connectionstring commandText ="SELECT * FROM ImageStore WHERE 1=0";// Make a new object
helper =new dBHelper(connectionString);{// Load Dataif(helper.Load(commandText,"image_id")==true){// Add a row and determin the row
helper.DataSet.Tables[0].Rows.Add(
helper.DataSet.Tables[0].NewRow());
dataRow = helper.DataSet.Tables[0].Rows[0];// Enter the given values
dataRow["imageFileName"]= image.FileName;
dataRow["imageBlob"]= image.ImageData;
dataRow["imageFileSizeBytes"]= image.FileSize;try{// Save -> determin succesif(helper.Save()==true){
isSucces =true;}else{
isSucces =false;MessageBox.Show("Error during Insertion");}}catch(Exception ex){// Show the Exception --> Dubbel Id/Name ?MessageBox.Show(ex.Message);}}//END IF}}//return the new image_idreturnConvert.ToInt32(dataRow[0].ToString());}
另存为图像文件
publicvoidSaveAsImage(Int32 imageID){//set variablesDataRow dataRow =null;Image image =null;
isSucces =false;// Displays a SaveFileDialog so the user can save the ImageSaveFileDialog dlg =newSaveFileDialog();
dlg.InitialDirectory=@"C:\\";
dlg.Title="Save Image File";//1
dlg.Filter="Tag Image File Format (*.tiff)|*.tiff";//2
dlg.Filter+="|Graphics Interchange Format (*.gif)|*.gif";//3
dlg.Filter+="|Portable Network Graphic Format (*.png)|*.png";//4
dlg.Filter+="|Joint Photographic Experts Group Format (*.jpg)|*.jpg";//5
dlg.Filter+="|Joint Photographic Experts Group Format (*.jpeg)|*.jpeg";//6
dlg.Filter+="|Bitmap Image File Format (*.bmp)|*.bmp";//7
dlg.Filter+="|Nikon Electronic Format (*.nef)|*.nef";
dlg.ShowDialog();// If the file name is not an empty string open it for saving.if(dlg.FileName!=""){Cursor.Current=Cursors.WaitCursor;//making shore only one of the 7 is being used.//if not added the default extention to the filenamestring defaultExt =".png";int pos =-1;string[] ext =newstring[7]{".tiff",".gif",".png",".jpg",".jpeg",".bmp",".nef"};string extFound =string.Empty;string filename = dlg.FileName.Trim();for(int i =0; i < ext.Length; i++){
pos = filename.IndexOf(ext[i], pos +1);if(pos >-1){
extFound = ext[i];break;}}if(extFound ==string.Empty) filename = filename + defaultExt;// Determin the ConnectionStringstring connectionString = dBFunctions.ConnectionStringSQLite;// Determin the DataAdapter = CommandText + Connectionstring commandText ="SELECT * FROM ImageStore WHERE image_id="+ imageID;// Make a new object
helper =new dBHelper(connectionString);// Load the dataif(helper.Load(commandText,"")==true){// Show the data in the datagridview
dataRow = helper.DataSet.Tables[0].Rows[0];
image =newImage((string)dataRow["imageFileName"],(byte[])dataRow["imageBlob"],(long)dataRow["imageFileSizeBytes"]);// Saves the Image via a FileStream created by the OpenFile method.using(FileStream stream =newFileStream(filename,FileMode.Create)){BinaryWriter bw =newBinaryWriter(stream);
bw.Write(image.ImageData);
isSucces =true;}}Cursor.Current=Cursors.Default;}if(isSucces){MessageBox.Show("Save succesfull");}else{MessageBox.Show("Save failed");}}
C# 在SQLite数据库中存储图像 z的更多相关文章
- 将 flask 中的 session 存储到 SQLite 数据库中
将 flask 中的 session 存储到 SQLite 数据库中 使用 flask 构建服务器后端时,常需要在浏览器端存储 cookie 用于识别不同用户,根据不同的 cookie 判断出当前请求 ...
- SQLite数据库如何存储和读取二进制数据
SQLite数据库如何存储和读取二进制数据 1. 存储二进制数据 SQLite提供的绑定二进制参数接口函数为: int sqlite3_bind_blob(sqlite3_stmt*, int, co ...
- QQ群消息监听并将消息存储到SQLite数据库中
目录 一.前言 二.效果图 1.插件界面 2.SQLite数据库 3.QQ群消息 三.准备工作 1.CQA软件 2.CQA-SDK易语言版本 3.易语言破解版 4.使用到的相关模块 四.开始撸代码 五 ...
- 数据库中存储日期的字段类型究竟应该用varchar还是datetime ?
背景: 前段时间在百度经验看到一篇文章<如何在电脑右下角显示你(爱人)的名字>,之前也听过这个小技巧,但没真正动手设置过.所以出于好奇就实践了一下. 设置完成后的效果例如以下.右下角的时间 ...
- 数据库中存储日期的字段类型到底应该用varchar还是datetime
将数据库中存储时间的数据类型改为varchar(),这时最好让这些时间是数据库中自动生成的(一个没有格式的输入也可能会导致输出错误),因为存储类型为varchar(),所以获取到的值也就被认为是一个字 ...
- Delphi实现在数据库中存取图像
向窗体上添加一个TListBox组件.一个TImage组件和一个TTable组件,设计完成的主界面如图1所示. 图1 主界面 本系统中需要设计一个新的基于Paradox 7的数据库Image.db,图 ...
- 网络采集软件核心技术剖析系列(6)---将任意博主的全部博文下载到SQLite数据库中并通过Webbrower显示(将之前的内容综合到一起)
一 本系列随笔目录及本节代码下载 自己开发的豆约翰博客备份专家软件工具问世3年多以来,深受广大博客写作和阅读爱好者的喜爱.同时也不乏一些技术爱好者咨询我,这个软件里面各种实用的功能是如何实现的. 该软 ...
- SQLite数据库中rowid使用
SQLite数据库中rowid使用 SQLite中每个表都默认包含一个隐藏列rowid,使用WITHOUT ROWID定义的表除外.通常情况下,rowid可以唯一的标记表中的每个记录.表中插入的第 ...
- SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置
在模拟器中: SharedPreferences将XML文件保存在/data/data/<package name>/shared_prefs目录下, openFileOutput方法将文 ...
随机推荐
- 扯谈网络编程之Tcp SYN flood洪水攻击
简单介绍 TCP协议要经过三次握手才干建立连接: (from wiki) 于是出现了对于握手过程进行的攻击.攻击者发送大量的SYN包,server回应(SYN+ACK)包,可是攻击者不回应ACK包,这 ...
- careercup-树与图 4.5
4.5 实现一个函数,检查一棵二叉树是否为二叉查找树. 参考:http://blog.csdn.net/sgbfblog/article/details/7771096 C++实现代码: #inclu ...
- careercup-树与图 4.3
4.3 给定一个有序整数数组,元素各不相同按升序排列,编写一个算法,创建一棵高度最小的二叉查找树. 解答 想要使构建出来的二叉树高度最小,那么对于任意结点, 它的左子树和右子树的结点数量应该相当.比如 ...
- linux监控程序
IP内部地址: 方法一:ifconfig |grep 'inet addr'|grep '192.168.1' | awk '{print $2}'| tr -d "addr:" ...
- 使用DrawerLayout实现QQ5.0侧拉菜单效果
在上一篇文章中,我们介绍了怎么使用DrawerLayout来实现一个简单的侧拉菜单(使用DrawerLayout实现侧拉菜单),也就是我们常说的抽屉效果,GitHub上类似效果的实现方式非常多,实现出 ...
- 构建本地yum源之rpmbuild问题汇总
- check-rpaths的问题 今天在编译varnish的时候,遇到几个问题,其中一个就是出现如下错误: ...+ /usr/lib/rpm/check-rpaths /usr/lib/rpm/c ...
- Java List中的一个List选择选择移除方法
记录: 第一个参数:传入需要处理的List 第二个参数:需要处理的参数在List中的标识符 第三个参数:在需要处理的参数中的开始位置 第三个参数:在需要处理的参数中的个数 List<String ...
- postgresql 字符串拼接"||“的使用
不过注意这里的只有postgresql可以使用,其他数据库不明. select name || remark as newname from mytable where name like '%tes ...
- 造成win2008 500内部错误不能显示详细错误的原因和解决方法
造成500错误常见原因有:ASP语法出错.ACCESS数据库连接语句出错.文件引用与包含路径出错.使用了服务器不支持的组件如FSO等. 另外,对于win2008的IIS默认不显示详细出错信息的问题以下 ...
- JavaScript高级程序设计(四): 关键字With的使用
一.关键字with 1.含义 with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象. 简单的说,with为一个或一 ...