OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "イメージファイル(*.gif,*.jpg,*.jpeg,*.bmp,*.wmf,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.wmf;*.png"; if (ofd.ShowDialog() == DialogResult.OK) { FileInfo f = new FileInfo(ofd.FileName); file = ofd.FileName; this.pictureBox1.Image = Image.FromFile(file); } 可以定义Image 类型 ,数据库中也有这个类型,就像其他类型一样操作就行了 private Image iMAGE; public Image IMAGE { get { return iMAGE; } set { iMAGE = value; } } obj.IMAGE = pictureBox1.Image;
1. image or binarySQL图片存取 using System; using System.Data; using System.Data.SqlClient; using System.IO;// 下面是从数据库取出图片 try { string getPhoto = @"select photo from gch_Customer_Photo where customerID=1234";
DataSet ds = new DataSet(); sqlDataAdapter1 = new SqlDataAdapter(getPhoto, sqlConnection1); sqlDataAdapter1.Fill(ds); DataRow row = ds.Tables[0].Rows[0]; byte[] bPhoto = new byte[0]; bPhoto = (byte[])row["photo"]; //int arraySize = bPhoto.GetUpperBound(0); MemoryStream memstr = new MemoryStream(bPhoto); pictureBox1.Image = Image.FromStream(memstr, true); existPhoto==true } catch(Exception ex) { Console.WriteLine(ex.ToString()); }// 下面是将图片存入数据库OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "*.bmp;*.jpg;*.gif|*.bmp;*.jpg;*.gif;*.jpeg"; if(ofd.ShowDialog()==DialogResult.OK) { string filePath = ofd.FileName; FileInfo imageFile = new FileInfo(filePath); if(imageFile.Length > 204800) { Console.WriteLine("图片大小最好不要过大!"); } pictureBox1.Image = Image.FromFile(filePath); if(existPhoto==true) { //如果之前已有图片,可以考虑删除了旧的先 string deleteOld = @"delete from gch_Customer_Photo where customerID=1234"; SqlCommand sqlcmmd = new SqlCommand(deleteOld,sqlConnection1); sqlcmmd.ExecuteNonQuery(); } string getAllPhotos = @"select customerID, photo from gch_Customer_Photo"; DataSet ds = new DataSet(); sqlDataAdapter1 = new SqlDataAdapter(getAllPhotos,sqlConnection1); sqlDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey; FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read); byte[] bPhoto= new byte[fs.Length]; fs.Read(bPhoto, 0, System.Convert.ToInt32(fs.Length)); fs.Close(); sqlDataAdapter1.Fill(ds); DataRow oneRow = ds.Tables[0].NewRow(); oneRow["customerID"] = 1234; oneRow["photo"] = bPhoto; ds.Tables[0].Rows.Add(oneRow); sqlDataAdapter1.Update(ds);
ofd.Filter = "イメージファイル(*.gif,*.jpg,*.jpeg,*.bmp,*.wmf,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.wmf;*.png";
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo f = new FileInfo(ofd.FileName);
file = ofd.FileName;
this.pictureBox1.Image = Image.FromFile(file);
}
可以定义Image 类型 ,数据库中也有这个类型,就像其他类型一样操作就行了
private Image iMAGE;
public Image IMAGE
{
get { return iMAGE; }
set { iMAGE = value; }
} obj.IMAGE = pictureBox1.Image;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;// 下面是从数据库取出图片
try
{
string getPhoto = @"select photo from gch_Customer_Photo where customerID=1234";
DataSet ds = new DataSet();
sqlDataAdapter1 = new SqlDataAdapter(getPhoto, sqlConnection1);
sqlDataAdapter1.Fill(ds); DataRow row = ds.Tables[0].Rows[0];
byte[] bPhoto = new byte[0];
bPhoto = (byte[])row["photo"];
//int arraySize = bPhoto.GetUpperBound(0); MemoryStream memstr = new MemoryStream(bPhoto);
pictureBox1.Image = Image.FromStream(memstr, true); existPhoto==true
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}// 下面是将图片存入数据库OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.bmp;*.jpg;*.gif|*.bmp;*.jpg;*.gif;*.jpeg";
if(ofd.ShowDialog()==DialogResult.OK)
{
string filePath = ofd.FileName; FileInfo imageFile = new FileInfo(filePath);
if(imageFile.Length > 204800)
{
Console.WriteLine("图片大小最好不要过大!");
} pictureBox1.Image = Image.FromFile(filePath); if(existPhoto==true)
{ //如果之前已有图片,可以考虑删除了旧的先
string deleteOld = @"delete from gch_Customer_Photo where customerID=1234";
SqlCommand sqlcmmd = new SqlCommand(deleteOld,sqlConnection1);
sqlcmmd.ExecuteNonQuery();
} string getAllPhotos = @"select customerID, photo from gch_Customer_Photo"; DataSet ds = new DataSet();
sqlDataAdapter1 = new SqlDataAdapter(getAllPhotos,sqlConnection1);
sqlDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey; FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read); byte[] bPhoto= new byte[fs.Length];
fs.Read(bPhoto, 0, System.Convert.ToInt32(fs.Length));
fs.Close(); sqlDataAdapter1.Fill(ds);
DataRow oneRow = ds.Tables[0].NewRow();
oneRow["customerID"] = 1234;
oneRow["photo"] = bPhoto;
ds.Tables[0].Rows.Add(oneRow); sqlDataAdapter1.Update(ds);
Console.WriteLine("图片入库成功!");
}
2. 用 byte[] data = File.ReadAllBytes(图片路径)
3. byte[] -> MemoryStream -> Bitmap.FromStream
你用的什么写的 C# winfrom程序???
猜它用的VS2008,如果是用VS2008跟“如果是考勤机和二代身份证,读出来的头像图片是怎样存储的,存储在本地的什么位置???”问题有多大关系吗? 对了,观望这个问题
本实例主要介绍如何将图片存入数据库。将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下:
/// <summary>
/// 将图片以二进制读入数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
byte[] imagebytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//打开数所
SqlConnection con = new SqlConnection("server=(local);database=zy_info;integrated security=true");
con.Open();
SqlCommand com = new SqlCommand("insert into tb_text values(@guatu)", con);
//"update yijing set guatu='"+
com.Parameters.Add("guatu", SqlDbType.Image);
com.Parameters["guatu"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
}
}
2、图片读出数据库
/// <summary>
/// 读出二进制图片数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
byte[] imagebytes = null;
//打开数据库
SqlConnection con = new SqlConnection("server=(local);database=zy_info;integrated security=true");
con.Open();
SqlCommand com = new SqlCommand("select top 1* from tb_text", con);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr["guatu"];
}
dr.Close();
com.Clone();
con.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
this.pictureBox1.Image = bmpt;
//dr.Close();
//com.Clone();
//con.Close();
}
这个可以成功的!
image2、把图像转化为二进制流存入数据库的方法?
byte[]数组 可以用File.ReadAllBytes()或FileStream的Read()等方法
然后Insert Into table value(@image)... 3、把二进制流转化图片的方法?
同样的读入byte[] 实例MemoryStream 然后用FromStream()
2.FileStream-fs-> BinaryReader br = new BinaryReader(fs)或者fs.Read(byt[],0,length)
3.MemoryStream ms=new MemoryStream(datatable["图片"])--Image im=Image.FromStream(ms);