UPLOADING IMAGE IN WINDOWS C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace stream
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BROWSE IMAGE CODE
private void button1_Click(object sender, EventArgs e){
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
//pictureBox1.Image = new Bitmap(open.FileName);
// image file path
textBox3.Text = open.FileName;
}
}
UPLOADING IMAGE IN DATABASE
private void button2_Click(object sender, EventArgs e){
FileStream fs = new FileStream(textBox3.Text,FileMode.Open);
Byte[] arr = new Byte[fs.Length];
fs.Read(arr, 0, Convert.ToInt32(fs.Length));
SqlConnection con = new SqlConnection("Data Source=INDIA;Initial Catalog=directory;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into imagetable values(@id,@image)", con);
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(textBox1.Text));
cmd.Parameters.AddWithValue("@image", arr);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("data saved");
}
}
}