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");}}