c# store a picture to database -


i not able insert picture in database.this sample code.i able select image computer , display in picture box.once try store image displayed in picture box database says object reference not set instance of object.

this sample code.

          namespace picutre_storage      {         public partial class form1 : form         {          public form1()       {         initializecomponent();        }      private void button2_click(object sender, eventargs e)     {         try         {              sqlconnection con = new sqlconnection                            (@"user id=sa;password=password123;initial catalog=picuture;persist security info=true;data source=enmedia-ea6278e\enmedia");             //i have used table named "tblusers" , fill fields             sqlcommand cmd = new sqlcommand("insert blobtest (blobdata) values (@blobdata)", con);              //save image picturebox memorystream object.             memorystream ms = new memorystream();             picturebox1.image.save(ms,imageformat.bmp);              //read memorystream byte array.             byte[] bytblobdata = new byte[ms.length];             ms.position = 0;             ms.read(bytblobdata, 0, convert.toint32(ms.length));              //create parameter insert statement contains image.             sqlparameter prm = new sqlparameter("@blobdata", sqldbtype.varbinary,            bytblobdata.length, parameterdirection.input, false,                             0, 0, null, datarowversion.current, bytblobdata);             cmd.parameters.add(prm);             con.open();             cmd.executenonquery();             con.close();         }         catch (exception ex)         { messagebox.show(""+ex); }      }        private void button3_click(object sender, eventargs e)        {         try         {             //getting image system             openfiledialog open = new openfiledialog();             open.filter = "image files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";             if (open.showdialog() == dialogresult.ok)             {                 picturebox2.image = new bitmap(open.filename);             }         }         catch (exception)         {             throw new applicationexception("failed loading image");         }     } 

2.there way check size of image before displaying in picture box.thank in advance

on thing see save file bmp format , picture exist let jpeg or tiff format, record inserted rightly picture refering type bmp format , not exist. think can somehting this.

  1. when picture in format want, create thumbnail picture store in different folder*(like pictures\thumbnails).*
  2. get extension of picture,replace jpg during creation of thumbnail.
  3. store in database.
  4. retrieve thumbnails in picture box. want.

if want know how create thumbnails (saving file jpg format welcome.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -