For saving refer below code.
C#
SaveFileDialog s = new SaveFileDialog();
s.FileName = "Image";
s.DefaultExt = ".Jpg";
s.Filter = "Image (.jpg)|*.jpg";
// Save Image in Project Folder.
string filePath = Path.GetDirectoryName(Application.ExecutablePath) + @"\File\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string filename = filePath + DateTime.Now.ToString("yyyyMMddHHmmss")+"Image.jpg";
FileStream fstream = new FileStream(filename, FileMode.Create);
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg);
fstream.Close();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO Files VALUES(@Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(filename));
cmd.Parameters.AddWithValue("@Path", filename);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
VB.Net
Dim s As SaveFileDialog = New SaveFileDialog()
s.FileName = "Image"
s.DefaultExt = ".Jpg"
s.Filter = "Image (.jpg)|*.jpg"
Dim filePath As String = Path.GetDirectoryName(Application.ExecutablePath) & "\File\"
If Not Directory.Exists(filePath) Then
Directory.CreateDirectory(filePath)
End If
Dim filename As String = filePath & DateTime.Now.ToString("yyyyMMddHHmmss") & "Image.jpg"
Dim fstream As FileStream = New FileStream(filename, FileMode.Create)
image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg)
fstream.Close()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Dim sql As String = "INSERT INTO Files VALUES(@Name, @Path)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(filename))
cmd.Parameters.AddWithValue("@Path", filename)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using