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方法将文 ...
随机推荐
- windows API 实现截图
參考:http://bbs.csdn.net/topics/330154355 #include "stdio.h" #include "windows.h" ...
- 从源码角度深入分析ant
Ant的基本概念 首先是ant的基本概念:Project,Target,Tasks,Properties,Paths 1.Project <project> build.xml文件最顶层的 ...
- PowerDesigner使用详解
PowerDesign高级应用编写相关的VBS脚本在PowerDesign里自定义一些命令与操作等,具体的可以参考C:\Program Files\Sybase\PowerDesigner 9\VB ...
- Spring MVC中如何传递对象参数
springController: @Controller @RequestMapping("/user") public UserController extends BaseC ...
- 移动端 touch 实现 拖动元素
var homeMove = (function () { //touch自适应 var k = "ontouchend" in window ? "touchend&q ...
- onitemcommand 的作用以及onitemdatabound的具体作用
Repeater控件.DataList控件的属性:OnItemCommand 当在ItemTemplate 中所宣告的Button 或LinkButton 控件触发事件时,如果该控件CommandNa ...
- Es6 之箭头函数 初学
不积跬步,无以至千里;不积小流,无以成江海! // ES5 var selected = allJobs.filter(function (job) { return job.isSelected() ...
- UNIX线程之间的关系
我们在一个线程中经常会创建另外的新线程,如果主线程退出,会不会影响它所创建的新线程呢?下面就来讨论一下. 1. 主线程等待新线程先结束退出,主线程后退出.正常执行. 示例代码: #include & ...
- 图灵API
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine( ...
- android .9文件的一点处理
Android上面有很多平台,造成比较严重的碎片问题,适配比较困难,作为应用,一般都需要图文并茂,图片又是比较占资源的.面对缩放的问题,于是出来了矢量图片文件,作一点矢量处理,于是就是.9图片,IOS ...