How to Convert image file to binary format

I want to convert any image file from my system to binary format by giving the path of the image from the system. After converting I want to store the converted binary format in a folder. How can i do it? It should be a Windows Form application.

Jon Skeet
people
quotationmark

If you're really just trying to store a file in a database, you don't need to worry about it being an image at all. The image file will already be in a "binary format" - you just need to store the file data. There may well be ways of doing this in a streaming way, but unless the files are huge it's probably just simpler to load them into byte arrays:

byte[] image = File.ReadAllBytes(imagePath);

Then use image as the parameter value in a parameterized SQL statement, e.g.

string sql = "INSERT INTO Foo (Path, ImageData) VALUES (@Path, @ImageData");
using (var connection = new SqlConnection(...))
{
    connection.Open();
    using (var command = new SqlCommand(sql, connection))
    {
        command.Parameters.Add("@Path", SqlDbType.NVarChar).Value = imagePath;
        command.Parameters.Add("@ImageData", SqlDbType.Image).Value = imageData;
        command.ExecuteNonQuery();
    }
}

people

See more on this question at Stackoverflow