Amin Safaei
Amin Safaei

Reputation: 23

storing image in database

i show image in a input with type file then i wanna save this image to database .i know how to store varbinary or image to db but i dont know how to access to input file?

SqlConnection con = new SqlConnection(stcon);
        SqlCommand command = new SqlCommand();


            string path = "";
            System.Drawing.Image img = System.Drawing.Image.FromFile(path);
            MemoryStream tmpStream = new MemoryStream();
            img.Save(tmpStream, ImageFormat.Png); // change to other format
            tmpStream.Seek(0, SeekOrigin.Begin);
            byte[] imgBytes = new byte[100000];
            tmpStream.Read(imgBytes, 0, 100000);

            command.CommandText = "INSERT INTO image(image) VALUES (:image)";
            IDataParameter par = command.CreateParameter();
            par.ParameterName = "image";
            par.DbType = DbType.Binary;
            par.Value = imgBytes;
            command.Parameters.Add(par);
            command.ExecuteNonQuery();

Upvotes: 2

Views: 1516

Answers (3)

Regon
Regon

Reputation: 392

With your questions, i think you have inserted image into table and now want to retrive Inserted image. if so then, you can try as,

                string sql = "";
                string address = "";
                SqlConnection con = new SqlConnection(ConnectionString);
                sql = "SELECT Image from ImageTable where imageId=1";

                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = cmd;
                adp.Fill(ds,"Data");


                address = "C:\\Users\\CARDIT\\Desktop\\Imag1.jpeg";
                byte[] bytes = (byte[])ds.Tables["Data"].Rows[i][0];
               MemoryStream ms = new MemoryStream(bytes);


               System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

                    Bitmap bmSave = new Bitmap(returnImage);
                    Bitmap bmTemp = new Bitmap(bmSave);

                    Graphics grSave = Graphics.FromImage(bmTemp);
                    grSave.DrawImage(returnImage, 0, 0, returnImage.Width, returnImage.Height);

                    bmTemp.Save(address); //If You want to save in Specific location

                    pictureBox1.Image = bmSave; //if you want to use Image in Picturebox Control;

Upvotes: 0

Goran
Goran

Reputation: 93

You have example at http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html

Btw, do not save images inside DB, instead save path to the file in DB and save image to disk.

Upvotes: 0

Szymon
Szymon

Reputation: 43023

You can simply use System.IO.File.ReadAllBytes method to read a binary file:

byte[] imgBytes = System.IO.File.ReadAllBytes(@"c:\temp\capture.png");

So in your case you can use it like that (replace the path):

string path = @"c:\temp\capture.png";
byte[] imgBytes = System.IO.File.ReadAllBytes(path);

command.CommandText = "INSERT INTO image(image) VALUES (:image)";
IDataParameter par = command.CreateParameter();
par.ParameterName = "image";
par.DbType = DbType.Binary;
par.Value = imgBytes;
command.Parameters.Add(par);
command.ExecuteNonQuery();

Upvotes: 1

Related Questions