How to save image as a blobs(Binary format) in database

All you have to do is refer comment lines and use the code same way .............

using (Bitmap bitmap = new Bitmap(934, 441))
            {

                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // Get the paramters to call g.CopyFromScreen and get the image

                    Rectangle rectanglePanelVideoPreview = Soilgrapcircle1.Bounds;
                    Point sourcePoints = Soilgrapcircle1.PointToScreen(new Point(Soilgrapcircle1.ClientRectangle.X, Soilgrapcircle1.ClientRectangle.Y));
                    g.CopyFromScreen(sourcePoints, Point.Empty, rectanglePanelVideoPreview.Size);

                }
                try
                {
                    string strGrabFileName = String.Format("E:1.jpg", DateTime.Now);
                    bitmap.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Jpeg);

                    //  String a = "insert into soilgraphs values('"+cmdSTC.SelectedValue.ToString()+"',)";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            string img1 = "E:1.jpg";


            //Save Graph in Database
            //A stream of bytes that represnts the binary file
            FileStream fs = new FileStream(img1, FileMode.Open, FileAccess.Read);
            //The reader reads the binary data from the file stream
            BinaryReader reader = new BinaryReader(fs);
            int fileLength = (int)fs.Length;

            //Bytes from the binary reader stored in BlobValue array

            byte[] BlobValue = new byte[fileLength];
            fs.Read(BlobValue, 0, (int)fileLength);
            //Create mysql connection
            MySqlConnection conn = new MySqlConnection("server=localhost;User Id=root;password=12345;database=cecb;Persist Security Info=True");
            MySqlCommand SaveBlobeCommand = new MySqlCommand();//create command object
            SaveBlobeCommand.Connection = conn;//set connection
            //sql insert quaery
            String sql = "UPDATE `soilgraphs` SET `circular`=@circular WHERE  `testId`=@testId;";
            //save the sql command in created command object
            SaveBlobeCommand.CommandText = sql;
            //values pass as parameters into command
            SaveBlobeCommand.Parameters.AddWithValue("@circular", BlobValue);
            SaveBlobeCommand.Parameters.AddWithValue("@testId", cmbCircleGraph.SelectedValue.ToString());
            try
            {
                //open the connecyion
                SaveBlobeCommand.Connection.Open();
                //execute the command
                SaveBlobeCommand.ExecuteNonQuery();
                conn.Close();
                SaveBlobeCommand.Connection.Close();
                reader.Close();
                fs.Close();
                this.Refresh();
                MessageBox.Show("Successfully Saved!!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);// where cusNic ='"+txtbApp2Nic.Text+"'

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Save Failed", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            finally
            {

            }

Comments

Popular posts from this blog

Find Country from URI

Encrypt and Decrypt text using alphabet shifting method

How to Call Stored Procedure