Introduction

This article is to demonstrate how to load images into a SQLite database and retrieve them for viewing. It is written in VS2010, C#, .NET4.0, and uses an ADO.NET provider System.Data.SQLite to connect to the SQLite database. And this all in a Windows XP environment.

Background

First of all, one has to obtain a few files and install them according to the rules:

SQLite ADO.NET provider: I installed the package into my "C:\" directory and chose not to register the DLL files, due to only wanting to include the DLL files to my project.

Using the code

SQLite

First, I created a new database named ImageLib.s3db and added a table and required fields.

Collapse | Copy Code

  1. CREATE TABLE [ImageStore] (
  2. [ImageStore_Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  3. [ImageFile] NVARCHAR(20) NULL,
  4. [ImageBlob] BLOB NULL
  5. );
VS2010 - C# - .NET 4.0

Next, I created a VS2010 project named StoringImages, changed the default namespace, and added a few folders and files.

  • folder: Database

    • file: StoringImages.s3db

      • Property: Copy to Output Directory => Copy Always
  • folder: Model
    • dBFunctions.cs
    • dBHelper.cs
    • Image.cs
    • ImageHelper.cs
  • file: System.Data.SQLite.dll
    • Property: Copy to Output Directory => Copy Always
  • file: SQLite.Interop.dll
    • Property: Copy to Output Directory => Copy Always
  • form: DisplayImages
    • This is the startup form of the project

Both System.Data.SQLite.dll and SQLite.Interop.dll need to be placed just beneath the root (project) StoringImages. This ensures that both files are installed into the same directory as the the project's "*.exe" file.

Solution Explorer

Model

Within the folder Model, there are a few classes, two for handling all database transactions and two for handling image transactions. The two for handling database transactions, dBFunctions and dBHelper, I've used before in my previous article C# & SQLite. So next, I'll be explaining how to use the remaining two classes, Image andImageHelper.

The class Image I'll be using as a custom made variable, which will be used to store the data of an imported image file, so it can be passed along between methods.

The class that will be doing all the hard work is ImageHelper. Within this class, you'll find various methods for handling the Insert, Delete, and SaveAs of an image. Insert uses another method called LoadImage which handles the binary reading of an image. Delete is for the removal of the data from the database. SaveAs is for saving the image back to a directory of choice. After every transaction, a transaction state is generated in the form ofisSucces. The view (form) DisplayImages requires this state in order to or not to update itself.

ImageHelper - Assigning of references

I try never to use more references than needed, but sometimes forget to remove the ones VS2010 automatically adds to every new class.

Collapse | Copy Code

  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Data;
  5. using System.Data.SQLite;
ImageHelper - Declairation of variables

MaxImageSize is used to declare the maximum number of bytes allowed when importing an image, which in this example is overridden in the LoadImage method.

Collapse | Copy Code

  1. private dBHelper helper = null;
  2. private string fileLocation = string.Empty;
  3. private bool isSucces = false;
  4. private int maxImageSize = 2097152;
  5.  
  6. //2MB - 2097152
  7. //5MB - 5242880
  8. //10MB - 10485760
  9.  
  10. /* Conversion
  11. * 1 Byte = 8 Bit
  12. * 1 Kilobyte = 1024 Bytes
  13. * 1 Megabyte = 1048576 Bytes
  14. * 1 Gigabyte = 1073741824 Bytes
  15. * */

dBHelper is the class that handles transactions to the database. maxImageSize is for the default maximum number of bytes allowed during upload. isSucces lets the view know that a transaction [Insert, Delete, SaveAs] was a success or not.

ImageHelper - Properties

Collapse | Copy Code

  1. private string FileLocation
  2. {
  3. get { return fileLocation; }
  4. set
  5. {
  6. fileLocation = value;
  7. }
  8. }
ImageHelper - Method GetSucces

This method is used by the form DisplayImage to find if a transaction [Insert, Delete, SaveAs] was a success or not.

Collapse | Copy Code

  1. public Boolean GetSucces()
  2. {
  3. return isSucces;
  4. }
ImageHelper - Method LoadImage

First we ask the user for the selected image file location [path] so that we can use this in our FileStream. Once theFilestream is open, we read the image as binary and store the acquired data in an instance of the Image class, which we'll be sending to the caller of the method LoadImage, the InsertImage method.

Collapse | Copy Code

  1. private Image LoadImage()
  2. {
  3. //Create an instance of the Image Class/Object
  4. //so that we can store the information
  5. //about the picture an send it back for
  6. //processing into the database.
  7. Image image = null;
  8.  
  9. //Ask user to select Image
  10. OpenFileDialog dlg = new OpenFileDialog();
  11. dlg.InitialDirectory = @"C:\\";
  12. dlg.Title = "Select Image File";
  13. //dlg.Filter = "Tag Image File Format (*.tiff)|*.tiff";
  14. //dlg.Filter += "|Graphics Interchange Format (*.gif)|*.gif";
  15. //dlg.Filter += "|Portable Network Graphic Format (*.png)|*.png";
  16. //dlg.Filter += "|Joint Photographic Experts Group Format (*.jpg)|*.jpg";
  17. //dlg.Filter += "|Joint Photographic Experts Group Format (*.jpeg)|*.jpeg";
  18. //dlg.Filter += "|Nikon Electronic Format (*.nef)|*.nef";
  19. //dlg.Filter += "|All files (*.*)|*.*";
  20. dlg.Filter = "Image Files (*.jpg ; *.jpeg ; *.png ; *.gif ; *.tiff ; *.nef)
  21. |*.jpg;*.jpeg;*.png;*.gif;*.tiff;*.nef";
  22. dlg.ShowDialog();
  23.  
  24. this.FileLocation = dlg.FileName;
  25.  
  26. if (fileLocation == null || fileLocation == string.Empty)
  27. return image;
  28.  
  29. if (FileLocation != string.Empty && fileLocation != null)
  30. {
  31. Cursor.Current = Cursors.WaitCursor;
  32.  
  33. //Get file information and calculate the filesize
  34. FileInfo info = new FileInfo(FileLocation);
  35. long fileSize = info.Length;
  36.  
  37. //reasign the filesize to calculated filesize
  38. maxImageSize = (Int32)fileSize;
  39.  
  40. if (File.Exists(FileLocation))
  41. {
  42. //Retreave image from file and binary it to Object image
  43. using (FileStream stream = File.Open(FileLocation, FileMode.Open))
  44. {
  45. BinaryReader br = new BinaryReader(stream);
  46. byte[] data = br.ReadBytes(maxImageSize);
  47. image = new Image(dlg.SafeFileName, data, fileSize);
  48. }
  49. }
  50. Cursor.Current = Cursors.Default;
  51. }
  52. return image;
  53. }
ImageHelper - Method InsertImage

InsertImage is called from the view (form) DisplayImages via the NewPicture method. Once the insert is successfully completed, it will return the newly obtained image_id back to the view.

As you'll notice, an instance of the class Image is used between the methods InsertImage and LoadImage.

Collapse | Copy Code

  1. public Int32 InsertImage()
  2. {
  3. DataRow dataRow = null;
  4. isSucces = false;
  5.  
  6. Image image = LoadImage();
  7.  
  8. //if no file was selected and no image was created return 0
  9. if (image == null) return 0;
  10.  
  11. if (image != null)
  12. {
  13. // Determin the ConnectionString
  14. string connectionString = dBFunctions.ConnectionStringSQLite;
  15.  
  16. // Determin the DataAdapter = CommandText + Connection
  17. string commandText = "SELECT * FROM ImageStore WHERE 1=0";
  18.  
  19. // Make a new object
  20. helper = new dBHelper(connectionString);
  21. {
  22. // Load Data
  23. if (helper.Load(commandText, "image_id") == true)
  24. {
  25. // Add a row and determin the row
  26. helper.DataSet.Tables[0].Rows.Add(
  27. helper.DataSet.Tables[0].NewRow());
  28. dataRow = helper.DataSet.Tables[0].Rows[0];
  29.  
  30. // Enter the given values
  31. dataRow["imageFileName"] = image.FileName;
  32. dataRow["imageBlob"] = image.ImageData;
  33. dataRow["imageFileSizeBytes"] = image.FileSize;
  34.  
  35. try
  36. {
  37. // Save -> determin succes
  38. if (helper.Save() == true)
  39. {
  40. isSucces = true;
  41.  
  42. }
  43. else
  44. {
  45. isSucces = false;
  46. MessageBox.Show("Error during Insertion");
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. // Show the Exception --> Dubbel Id/Name ?
  52. MessageBox.Show(ex.Message);
  53. }
  54.  
  55. }//END IF
  56. }
  57. }
  58. //return the new image_id
  59. return Convert.ToInt32(dataRow[0].ToString());
  60. }
ImageHelper - Method DeleteImage

DeleteImage executes the removal of an image from the database. The method requires an integer, the row number of the dataset, given by the view (form) DisplayImages via the method DeletePicture. And after processing,DeleteImage returns the "state" back to DeletePicture.

Collapse | Copy Code

  1. public void DeleteImage(Int32 imageID)
  2. {
  3. //Set variables
  4. isSucces = false;
  5.  
  6. // Determin the ConnectionString
  7. string connectionString = dBFunctions.ConnectionStringSQLite;
  8.  
  9. // Determin the DataAdapter = CommandText + Connection
  10. string commandText = "SELECT * FROM ImageStore WHERE image_id=" + imageID;
  11.  
  12. // Make a new object
  13. helper = new dBHelper(connectionString);
  14. {
  15. // Load Data
  16. if (helper.Load(commandText, "image_id") == true)
  17. {
  18. // Determin if the row was found
  19. if (helper.DataSet.Tables[0].Rows.Count == 1)
  20. {
  21. // Found, delete row
  22. helper.DataSet.Tables[0].Rows[0].Delete();
  23. try
  24. {
  25. // Save -> determin succes
  26. if (helper.Save() == true)
  27. {
  28. isSucces = true;
  29. }
  30. else
  31. {
  32. isSucces = false;
  33. MessageBox.Show("Delete failed");
  34. }
  35. }
  36. catch (Exception ex)
  37. {
  38. // Show the Exception --> Dubbel ContactId/Name ?
  39. MessageBox.Show(ex.Message);
  40. }
  41. }
  42. }
  43. }
  44. }
ImageHelper - Method SaveAsImage

To top it all off, I've added a SaveAs method. Save the binary data back to an image file, to an allocated directory of the user's choice.

Once again, we need to know which row of the dataset needs to be saved to file, thus our method requires an integer as parameter.

First, we set the local variables to the default values, a C# - .NET requirement and good standard programming practice.

Then we ask the user, via a SaveDialog, for the directory location and file name for the new image. A dialog.Filterrange is set, that we allow, and a check is executed accordingly.

The binary data is retrieved from the database with the use of dBHelper, once again using an instance of the Imageclass. If dBHelper.Load returns the value "true", the FileStream is executed and writing the binary to image processed. To end the process the "state" isSucces is returned to the view (form) DisplayImages.

Collapse | Copy Code

  1. public void SaveAsImage(Int32 imageID)
  2. {
  3. //set variables
  4. DataRow dataRow = null;
  5. Image image = null;
  6. isSucces = false;
  7.  
  8. // Displays a SaveFileDialog so the user can save the Image
  9. SaveFileDialog dlg = new SaveFileDialog();
  10. dlg.InitialDirectory = @"C:\\";
  11. dlg.Title = "Save Image File";
  12. //1
  13. dlg.Filter = "Tag Image File Format (*.tiff)|*.tiff";
  14. //2
  15. dlg.Filter += "|Graphics Interchange Format (*.gif)|*.gif";
  16. //3
  17. dlg.Filter += "|Portable Network Graphic Format (*.png)|*.png";
  18. //4
  19. dlg.Filter += "|Joint Photographic Experts Group Format (*.jpg)|*.jpg";
  20. //5
  21. dlg.Filter += "|Joint Photographic Experts Group Format (*.jpeg)|*.jpeg";
  22. //6
  23. dlg.Filter += "|Bitmap Image File Format (*.bmp)|*.bmp";
  24. //7
  25. dlg.Filter += "|Nikon Electronic Format (*.nef)|*.nef";
  26. dlg.ShowDialog();
  27.  
  28. // If the file name is not an empty string open it for saving.
  29. if (dlg.FileName != "")
  30. {
  31. Cursor.Current = Cursors.WaitCursor;
  32. //making shore only one of the 7 is being used.
  33. //if not added the default extention to the filename
  34. string defaultExt = ".png";
  35. int pos = -1;
  36. string[] ext = new string[7] {".tiff", ".gif", ".png",
  37. ".jpg", ".jpeg", ".bmp", ".nef"};
  38. string extFound = string.Empty;
  39. string filename = dlg.FileName.Trim();
  40. for (int i = 0; i < ext.Length; i++)
  41. {
  42. pos = filename.IndexOf(ext[i], pos + 1);
  43. if (pos > -1)
  44. {
  45. extFound = ext[i];
  46. break;
  47. }
  48. }
  49. if (extFound == string.Empty) filename = filename + defaultExt;
  50.  
  51. // Determin the ConnectionString
  52. string connectionString = dBFunctions.ConnectionStringSQLite;
  53.  
  54. // Determin the DataAdapter = CommandText + Connection
  55. string commandText = "SELECT * FROM ImageStore WHERE image_id=" + imageID;
  56.  
  57. // Make a new object
  58. helper = new dBHelper(connectionString);
  59.  
  60. // Load the data
  61. if (helper.Load(commandText, "") == true)
  62. {
  63. // Show the data in the datagridview
  64. dataRow = helper.DataSet.Tables[0].Rows[0];
  65. image = new Image(
  66. (string)dataRow["imageFileName"],
  67. (byte[])dataRow["imageBlob"],
  68. (long)dataRow["imageFileSizeBytes"]
  69. );
  70.  
  71. // Saves the Image via a FileStream created by the OpenFile method.
  72. using (FileStream stream = new FileStream(filename, FileMode.Create))
  73. {
  74. BinaryWriter bw = new BinaryWriter(stream);
  75. bw.Write(image.ImageData);
  76. isSucces = true;
  77. }
  78. }
  79. Cursor.Current = Cursors.Default;
  80. }
  81.  
  82. if (isSucces)
  83. {
  84. MessageBox.Show("Save succesfull");
  85. }
  86. else
  87. {
  88. MessageBox.Show("Save failed");
  89. }
  90. }
View - (form) DisplayImages

The form contains a splitpanel with a picture box on one side (left) + a label on the other side (right) of aDataGridView. It also contains a ContextMenuStrip which is linked to the DataGridView. TheContextMenuStrip contains the three commands for this little project, the commands being New, Delete, andSaveAs.

The form itself contains a few extra methods for handling the commands, retrieving the data from the database, and filling up the DataGridView. The filling up of the DataGridView is only executed at the start of the application and after every execution of a command if the command was a success.

Remark

I know that the class ImageHelper and its methods need refactoring but I specially left it like this so that all its functionalities are contained; this makes it easier to read.

I hate reading articles about code and it's all over the place, jumping in and out methods to get a grip on things.

Points of Interest

Those who have read my previous article C# & SQLite will recognize the two database classes for handling all database transactions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

kribo

Software Developer

Belgium

Developer within C#, Dynamics NAV (Navision), Php environments.

C# & SQLite - Storing Images的更多相关文章

  1. what are Datatypes in SQLite supporting android

    As said at Datatypes In SQLite Version 3: Datatypes In SQLite Version 3 Most SQL database engines (e ...

  2. SQLite数据库连接方式

    http://blog.csdn.net/ZF101201/archive/2010/05/26/5626365.aspx SQLite.NET Type:    .NET Framework Cla ...

  3. SQLite/嵌入式数据库

    SQLite/嵌入式数据库 的项目要么不使用数据库(一两个文配置文件就可以搞定),要么就会有很多的数据,用到 postgresql,操练sqlite的还没有.现在我有个自己的小测试例子,写个数据库对比 ...

  4. SQLITE WITH ENTITY FRAMEWORK CODE FIRST AND MIGRATION

    Last month I’ve a chance to develop an app using Sqlite and Entity Framework Code First. Before I st ...

  5. php读取sqlite数据库入门实例

    php读取sqlite数据库的例子,php编程中操作sqlite入门实例.原文参考:http://www.jbxue.com/article/php/22383.html在使用SQLite前,要确保p ...

  6. Persisting iOS Application Data in SQLite Database Using FMDB

    In previous articles we have utilized NSUserDefaults and .NET web services to persist iPhone data. N ...

  7. SQLite connection strings

    Basic Data Source=c:\mydb.db;Version=3; Version 2 is not supported by this class library. SQLite In- ...

  8. 提高sqlite 的运行性能(转载)

    原文地址: https://blog.devart.com/increasing-sqlite-performance.html One the major issues a developer en ...

  9. SQLite is 35% Faster Than The Filesystem

    比方说你要在C++/PHP里实现一个函数Image get_image(string id),不同的图片有1万张(用户头像),你可以把它们存在一个目录/文件夹里,然后fopen()再fread. 你也 ...

随机推荐

  1. HashMap源代码深入剖析

    ..

  2. Git教程(4)常用小技巧之标签

    Git 使用两种主要类型的标签:轻量标签(lightweight)与附注标签(annotated). 一个轻量标签很像一个不会改变的分支 - 它只是一个特定提交的引用. 然而,附注标签是存储在 Git ...

  3. Missing artifact com.sun:tools:jar:1.5.0的解决方案

    今天在用maven的时候遇到一个问题pom.xml提示Missing artifact com.sun:tools:jar:1.5.0 试过改eclipse的eclipse.ini文件,也试过在ecl ...

  4. Warning: Name is nonexistent or not a directory

    Every time I start up MATLAB, I receive this message: Warning: Name is nonexistent or not a director ...

  5. .Net课程体系

    .Net课程体系

  6. MEF技术

    MEF 是一个使开发人员能够创建“插件式应用程序”的技术,这里的“插件”是指在应用程序部署后开发人员能够通过开发“插件”来扩展应用程序功能.但不同之处是使用MEF框架您在系统设计阶段不需要考虑在应用程 ...

  7. 在windows10+word2010中完美使用北大方正word公式输入法总结

    如果在安装输入法时遇到了无法安装的情况,请首先百度下VC运行库安装下(32位64位都安就是),然后安装北大方正word公式输入法,此时不会再提示无法安装,接着(关键)在word应用程序图标右键-> ...

  8. POJ3414 Pots BFS搜素

    题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...

  9. Mac OS 终端常用命令【搜藏】

    基础概念 OS X 采用的Unix文件系统,所有文件都挂在跟目录“ /” 下面,所以不在要有Windows 下的盘符概念.比如什么“C:”你在桌面上看到的硬盘都挂在 /Volumes 下.比如接上个叫 ...

  10. activemq 一个不错的ppt

    http://people.apache.org/~jstrachan/talks/ActiveMQ-Dublin07.pdf