Previous..
Next...
Here i will explain How to save image in database in window application.
Step(1): Form
In Tutorial 23 i shown you how to insert data into database so this is also in same way we will insert employee detail with his picture. Drag and down a textbox for image path. and this textbox name as "textBox_image_path"
Step(2): Code
Coding at saving button.Make a column a in database as "image"
we will use a namespace as "Using system . IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace First_Csharp_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String gender; //we have to define this
private void button1_Click(object sender, EventArgs e)
{
byte[] imageBt = null;
FileStream fstream = new FileStream ( this.textBox_image_path.Text , FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader (fstream );
imageBt = br.ReadBytes((int)fstream.Length);
SqlConnection con = new SqlConnection(str);
String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
String query = "insert into data (E.id,name,surname,age,gender,DOB,image) values ('"+this.eid_txt.text+"','"+this.nametxt.text+"','"+this.surname_txt.text+"','"+this.age_txt.text+"' , '"+this.gender+"' , '"+this.DateTimePicker1.Text+"' ,@IMG)";
SqlCommand cmd = new sqComamnd(query,con);
SqlDataReader dbr;
SqlDataReader dbr;
try
{
con.open();
cmd.Parameters.Add( new MySqlParameters( "@IMG" , imageBt));
dbr = cmd.ExecuteReader();
MessageBox.Show("saved");
while(dbr.read())
{
}
MessageBox.Show("saved");
while(dbr.read())
{
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "male";
}
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "female";
}
}
}
private void LoadImage_click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog ();
openfile .filter = "JPG Files(* . jpg) | *.jpg | PNG Files(* . png) | * . png | All Files( * . *) | * . * " ;
if(openfile .ShowDialog() ==System.Window.Forms.DialogResult.OK)
{
OpenFileDialog openfile = new OpenFileDialog ();
openfile .filter = "JPG Files(* . jpg) | *.jpg | PNG Files(* . png) | * . png | All Files( * . *) | * . * " ;
if(openfile .ShowDialog() ==System.Window.Forms.DialogResult.OK)
{
string picpath = openfile .FileName.ToString();
textBox_image_path.Text = picpath ;
pictureBox1.ImageLocation = picpath;
textBox_image_path.Text = picpath ;
pictureBox1.ImageLocation = picpath;
}
}
}
}
}
}
Step(3): Output
Now run your application then when you fill the data and you will see in database there image will be store. you will see database as "BLOB" which store your image.
I don't understand. This post is tutorial save file to database include images paths. But I want show images same "C:\images.jpg" in
ReplyDeleteuse this
ReplyDelete1
var imageBytes = File.ReadAllBytes("bitmap.bmp");
var image = imageBytes.ToImage();
image.Save("output.bmp");
2
from some file:
Image.Save(@"FilePath", ImageFormat.Jpeg);
3.
Image bitmap = Image.FromFile("C:\\MyFile.bmp");
bitmap.Save("C:\\MyFile2.bmp");
4 from pictureBox:
pictureBox1.Image.Save(@"path + imageName",ImageFormat.Jpeg);
5
byte[] img = Convert.FromBase64String(s);
System.IO.File.WriteAllBytes(@"C:\image.bmp", img);
it not active. I made with visual studio 2013 windows application C#.
DeleteYou want to save image at Computer except DataBase ??
ReplyDeleteHi,
ReplyDeleteI have a table whose entry like below:
idname
asp0362
klip632
hlpp632
gtr852a
And i have folder which having file with name as shown below:
asp0362_20150101.txt
hlpp632_20150101.txt
cfgo0602_20150101.txt
Now i want to compare filenames before date like(aapo6321) and idname from table and want to get the name of files which are not in table of DB.