Sunday 18 November 2012

create binary image converstion and stores in database


//on button click(browse button)

private void btnProductVariantBrowse_Click(object sender, EventArgs e)
        {
  //it stores the file name in textbox
            txtProductImage.Text = BrowseFile();
         
        }

//it open window and return the file name

public string BrowseFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return ofd.FileName;
            }
            return "";
        }
//picture details


 if (File.Exists(txtProductImage.Text))
            {
                FileInfo fi = new FileInfo(txtProductImage.Text);
                pvDetails.picture = new Picture();
                pvDetails.picture.PictureBinary = DbHelper.FileToByteArray(txtProductImage.Text);
                pvDetails .picture .MimeType="Image/" + fi.Extension.Substring(1, fi.Extension.Length - 1);
                pvDetails .picture.IsNew =true;       
               

            }
           

//file to byte array class


 public static byte[] FileToByteArray(string _FileName)
        {
            byte[] bytArrData = null;
            try
            {
                // Open file for reading
                System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                // attach filestream to binary reader
                System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
                // get total byte length of the file
                long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
                // read entire file into buffer
                bytArrData = _BinaryReader.ReadBytes((Int32)_TotalBytes);

                // close file reader
                _FileStream.Close();
                _FileStream.Dispose();
                _BinaryReader.Close();
            }
            catch (Exception ex)
            {
                // Error
                //Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
            }
            return bytArrData;
        }



No comments:

Post a Comment