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. 关于WinRT中c++和c#相互调用的问题

    先说结论(不见得是最终正确的结论,不过google了一晚上也没有查出个所以然来,即便有解决方法我也认为是微软傻x): 首先c#和c++理所应当的不应该在同一个工程中,而只能是同一个工程的两个项目.只能 ...

  2. [React Native] Passing data when changing routes

    The way you make HTTP requests in React Native is with the Fetch API. In this video we'll talk about ...

  3. POJ 1201 Intervals(图论-差分约束)

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 Descri ...

  4. C# - 系统类 - 系统接口

    ICloneable接口 ns:System 此接口只定义了一个名为Clone的方法 该方法返回一个Object对象 表示调用该方法的对象的拷贝版 IComparable接口 ns:System 此接 ...

  5. Java基础知识强化之IO流笔记40:字符流缓冲流之特殊功能 [ newLine() / readLine() ]

    1. 字符缓冲流的特殊方法 BufferedWriter: public void newLine():根据系统来决定换行符 BufferedReader: public String readLin ...

  6. Linux启动遇到的问题

    双系统装的Ubuntu,在一次意外关机后无法进入图形界面,每次输入完密码进入桌面后又会退到密码输入界面.使用命令行df -hl查看发现根目录使用率达到100%.推测是因为意外关机导致的,但是找不到应该 ...

  7. [Form Builer]Locking Mode and LOCK_RECORD

    Locking Mode Property Description Specifies when Oracle Forms tries to obtain database locks on rows ...

  8. IEnumerable 和 IQueryable

    共有两组 LINQ 标准查询运算符,一组在类型为 IEnumerable<T> 的对象上运行,另一组在类型为 IQueryable<T> 的对象上运行.构成每组运算符的方法分别 ...

  9. Unity3D 关于运动模型

    首先,要让一个物体在3D世界里面运动起来. 要在一个具有碰撞器的面和一个具有碰撞器的体. 要具有碰撞器属性,不然其他物体会穿过的. 默认的碰撞器是一个具有阻碍效果的,如果让碰撞器具有了一个触发器属性I ...

  10. asterisk webrtc使用SIPML5初体验

    一直尝试,web呼叫xlite终端没有,主要是配置问题: 其中sip.conf配置如下: [general] context=public ; Default context for incoming ...