C# 在SQLite数据库中存储图像

更多 0
 

建表语句

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的更多相关文章

  1. 将 flask 中的 session 存储到 SQLite 数据库中

    将 flask 中的 session 存储到 SQLite 数据库中 使用 flask 构建服务器后端时,常需要在浏览器端存储 cookie 用于识别不同用户,根据不同的 cookie 判断出当前请求 ...

  2. SQLite数据库如何存储和读取二进制数据

    SQLite数据库如何存储和读取二进制数据 1. 存储二进制数据 SQLite提供的绑定二进制参数接口函数为: int sqlite3_bind_blob(sqlite3_stmt*, int, co ...

  3. QQ群消息监听并将消息存储到SQLite数据库中

    目录 一.前言 二.效果图 1.插件界面 2.SQLite数据库 3.QQ群消息 三.准备工作 1.CQA软件 2.CQA-SDK易语言版本 3.易语言破解版 4.使用到的相关模块 四.开始撸代码 五 ...

  4. 数据库中存储日期的字段类型究竟应该用varchar还是datetime ?

    背景: 前段时间在百度经验看到一篇文章<如何在电脑右下角显示你(爱人)的名字>,之前也听过这个小技巧,但没真正动手设置过.所以出于好奇就实践了一下. 设置完成后的效果例如以下.右下角的时间 ...

  5. 数据库中存储日期的字段类型到底应该用varchar还是datetime

    将数据库中存储时间的数据类型改为varchar(),这时最好让这些时间是数据库中自动生成的(一个没有格式的输入也可能会导致输出错误),因为存储类型为varchar(),所以获取到的值也就被认为是一个字 ...

  6. Delphi实现在数据库中存取图像

    向窗体上添加一个TListBox组件.一个TImage组件和一个TTable组件,设计完成的主界面如图1所示. 图1 主界面 本系统中需要设计一个新的基于Paradox 7的数据库Image.db,图 ...

  7. 网络采集软件核心技术剖析系列(6)---将任意博主的全部博文下载到SQLite数据库中并通过Webbrower显示(将之前的内容综合到一起)

    一 本系列随笔目录及本节代码下载 自己开发的豆约翰博客备份专家软件工具问世3年多以来,深受广大博客写作和阅读爱好者的喜爱.同时也不乏一些技术爱好者咨询我,这个软件里面各种实用的功能是如何实现的. 该软 ...

  8. SQLite数据库中rowid使用

    SQLite数据库中rowid使用   SQLite中每个表都默认包含一个隐藏列rowid,使用WITHOUT ROWID定义的表除外.通常情况下,rowid可以唯一的标记表中的每个记录.表中插入的第 ...

  9. SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置

    在模拟器中: SharedPreferences将XML文件保存在/data/data/<package name>/shared_prefs目录下, openFileOutput方法将文 ...

随机推荐

  1. 深入浅出JMS(一)——JMS简单介绍

    假设手机仅仅能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户须要查看信息的时候再去获得信息呢?伴 ...

  2. 进程环境之main函数

    C程序总是从main函数开始执行.main函数的原型是: int main( int argc, char *argv[] ); 其中,argc是命令行参数的数目,argv是指向参数的各个指针所构成的 ...

  3. QT显示输出及其桌面

      /* 应用程序桌面 */ QDesktopWidget* desktop = QApplication::desktop(); /* 指定显示器的屏幕尺寸(1024 * 768) */ QRect ...

  4. 【转】补充说明:关于Beaglebone black上debian无图形界面的问题及QT的窗口示例

    有个兄弟发了一个站内的私信给我,内容如下: 时间:2014-03-05 09:08:19 大哥,debian 的BBB版本没有图形界面吧 我安装后只有文本界面 我突然意识到,我前面有没有说清楚的地方, ...

  5. selenium中定位iframe框

    这是使用谷歌看到的源码.想要往里面输入内容,需要使用js. 这个iframe没有id,不能通过id直接定位到.但可以通用TagName来定位到iframe. WebDriver中定位的代码如下: St ...

  6. dependencies与devDependencies的区别

    npm install在安装node模块时,有两种命令参数可以把它们的信息写入package.json文件: –save –save-dev 但它的文档里1,只提到一个小区别,–save会把依赖包名称 ...

  7. sklearn两种保存模型的方式

    作者:卢嘉颖 链接:https://www.zhihu.com/question/27187105/answer/97334347 来源:知乎 著作权归作者所有,转载请联系作者获得授权. 1. pic ...

  8. 安卓项目-利用Sqlite数据库,开发新闻发布系统

    本教程致力于程序员可以快速的学习安卓移动端手机开发. 适合于已经习得一种编程语言的同仁. 更多志同道合,想要学习更多编程技术的大神们. 小弟不才,麻烦关注一下我的今日头条号-做全栈攻城狮. 本文章是基 ...

  9. java 从String中匹配数字,并提取数字

    方法如下: private List<FieldList> GetTmpFieldsList(List<String> FieldsList,String tmptableNa ...

  10. Ubuntu 12.04安装PPTP

    1.安装软件 sudo apt-get install pptpd ufw 2.编辑/etc/ppp/pptpd-options 找到 refuse-pap refuse-chap refuse-ms ...