Ivonne Aspilcueta

Ivonne Aspilcueta

  • 1.1k
  • 601
  • 8.4k

How to read Image file/path in C#

Jul 10 2024 11:25 PM

Hello,

I am trying to read a image path to save variables: acctnumber, chknumber and date and the image into a database, I  have created the connection with those variables and to read the file to a some extent but I am totally lost on how to substract each variable to save them plus the image into database when I hit the save button.

This is the file to read/image example:

1991|4145|42551|1015737750|321173483|04-03-2017|11000201|0|1991-4145-1015737750-04032017.png

where 1991 = acctNumber,  4145= chkNumber,  04-03-2017 = ImgDate and png = Image extension

 

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;


namespace ImageToBinary
{
    public partial class Conversion : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            
                // Load file meta data with FileInfo
                FileInfo fileInfo = new FileInfo(path);

                // The byte[] to save the data in
                byte[] data = new byte[fileInfo.Length];

                // Load a filestream and put its content into the byte[]
                using (FileStream fs = fileInfo.OpenRead())
                {
                    fs.Read(data, 0, data.Length);
                }
            
            //convert the image into the byte  


            //Insert the Data in the Table  

            using (SqlConnection connection = new SqlConnection())
                {
                    connection.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ToString();

                    connection.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = connection;

                    string commandText = "Insert into chkImages values (@acctNumber,@chknumber,@imgdate)";

                    cmd.CommandText = commandText;
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@acctNumber", SqlDbType.Int);
                    cmd.Parameters.Add("@chknumber", SqlDbType.Int);
                    cmd.Parameters.Add("@imgdate", SqlDbType.VarChar);
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    connection.Close();

                    Response.Write("Image has been Added");
                }
        }
    }
}

 


Answers (2)