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.Drawing.Imaging;
using System.Data.Sql;
using System.Data.SqlClient;
private void sql_insert_pic(string path,string ID)
{
conn = null;
cmd = null;
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
pictureBox2.Image = img;
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffbyt = new byte[fs.Length];
fs.Read(buffbyt, 0, (int)fs.Length);
fs.Close();
fs = null;
string comm = @\"update [combox_store_base].[dbo].[picture] set picture=@img,pathpic='\"+path+\"' where
ID='\"+ID+\"';\";//////////////////////////////////////////////////////数据库命令path是图片绝对地址,ID是与本人数据库结构有关可以不参考
cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = comm;
conn = new SqlConnection(st);
cmd.Connection = conn;
cmd.Parameters.Add(\"@img\", System.Data.SqlDbType.Image);
cmd.Parameters[0].Value = buffbyt;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
buffbyt = null;
MessageBox.Show(\"上¦?传ä?成¨¦功|\");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
从SQL2008读取图片并放到picturebox
private void pic(string ID)
{
string sql = \" select pathpic,picture from [combox_store_base].[dbo].[picture] where ID='\" + ID + \"';\";
if (File.Exists(order_sql_search(sql, \"pathpic\")))/////////////////////////查¨¦看¡ä文?件t是º?否¤?存ä?在¨²
{
pictureBox1.ImageLocation = @\"\" + order_sql_search(sql,
\"pathpic\") + \"\";
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
else
{
string sql_pic = \" select picture from [combox_store_base].[dbo].[picture] where ID='\" + ID + \"';\";
pic_read_sql(ID, sql_pic);
}
}
private void pic_read_sql(string ID, string sql)
{
byte[] buffByte = null;
cmd = null;
conn = null;
conn = new SqlConnection(st);
conn.Open();
cmd = new SqlCommand(sql, conn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
buffByte = ((byte[])dr[\"picture\"]);
}
dr.Close();
conn.Close();
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
System.Drawing.Bitmap bmp = new Bitmap(ms);
pictureBox1.Image = bmp;
}
程序全部代码
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.Drawing.Imaging;
using System.Data.Sql;
using System.Data.SqlClient;
namespace combox_store
{
public partial class picture_update : Form
{
Rectangle theRecetangle = new Rectangle(new Point(0, 0), new Size(0, 0));
int[] wh = new int[4];
string pic2_path = \"\";
SqlConnection conn = null;
SqlCommand cmd = null;
public bool begin = false; //是º?否¤?开a始º?截?屏¨¢
public bool isDoubleClick = false;
public Point firstPoint = new Point(0, 0); //鼠º¨®标À¨º第̨²一°?点Ì?
public Point secondPoint = new Point(0, 0); //鼠º¨®标À¨º第̨²二t点Ì?
public Image cachImage = null; //用®?来¤¡ä缓o存ä?截?获?的Ì?屏¨¢幕?
public int halfWidth = 0;//保À¡ê存ä?屏¨¢幕?一°?半ã?的Ì?宽¨ª度¨¨
public int halfHeight = 0;//保À¡ê存ä?屏¨¢幕?一°?般ã?的Ì?高?度¨¨
int minX = 0;
int minY = 0;
int maxX = 0;
int maxY = 0;
string ID = \"\";
string st = \"Data Source=PC-20120305ECVO\\\\MSSQLSERVER_CHU;Initial Catalog=combox_store_base;Integrated Security=True;Asynchronous Processing=true\";
public picture_update(string id)
{
InitializeComponent();
label1.Location = new Point(0, 0);
label1.Visible = false;
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
ID = id;
}
private void picture_update_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string aa = folderBrowserDialog1.SelectedPath.ToString();
listView1.Items.Clear();
add_pic(aa);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (pic2_path != \"\")
{
sql_insert_pic(pic2_path, ID);
pic2_path = \"\";
}
else
{
MessageBox.Show(\"请?先¨¨保À¡ê存ä?图ª?片?\");
}
}
private void sql_insert_pic(string path,string ID)
{
conn = null;
cmd = null;
try
{
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
pictureBox2.Image = img;
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffbyt = new byte[fs.Length];
fs.Read(buffbyt, 0, (int)fs.Length);
fs.Close();
fs = null;
string comm = @\"update [combox_store_base].[dbo].[picture] set picture=@img,pathpic='\"+path+\"' where ID='\"+ID+\"';\";
cmd = new SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = comm;
conn = new SqlConnection(st);
cmd.Connection = conn;
cmd.Parameters.Add(\"@img\", System.Data.SqlDbType.Image);
cmd.Parameters[0].Value = buffbyt;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
buffbyt = null;
MessageBox.Show(\"上¦?传ä?成¨¦功|\");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private string getTime()
{
string date = \"\";
string date_Y = DateTime.Now.Year.ToString();
date_mi;
}
string date_M = DateTime.Now.Month.ToString();
string date_D = DateTime.Now.Day.ToString();
string date_h = DateTime.Now.Hour.ToString();
string date_m = DateTime.Now.Minute.ToString();
string date_s = DateTime.Now.Second.ToString();
string date_mi = DateTime.Now.Millisecond.ToString();
date = date_Y + date_M + date_D + date_h + date_m + date_s + return date;
private void add_pic(string path)
{
string[] arrFileNames=getipg(path);
ImageList imgList = new ImageList();
Image img = null;
foreach (string name in arrFileNames)
{
img = Image.FromFile(name);
imgList.Images.Add(img);
imgList.ImageSize = new Size(150, 150);
}
listView1.View = View.LargeIcon;
listView1.LargeImageList = imgList;
int j = imgList.Images.Count;
for (int i = 0; i < j; i++)
{
ListViewItem li = new ListViewItem();
li.Text = arrFileNames[i].ToString();
li.ImageIndex = i;
listView1.Items.Add(li);
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容