在数据库中添加一个image列 我的只有一列 所以是下边代码 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.Runtime.Serialization.Formatters.Binary; using System.IO; namespace WindowsApplication1 { public partial class Form1 : Form { string conStr = "Data Source=.;Initial Catalog=text;Integrated Security=True"; SqlConnection connection; public Form1() { InitializeComponent(); } //显示 private void button1_Click(object sender, EventArgs e) { connection = new SqlConnection(conStr); //二进制格式化器 BinaryFormatter bf = new BinaryFormatter(); string sql = "select image from image";//自己加条件啊 SqlCommand command = new SqlCommand(sql,connection); connection.Open(); byte[] arr = (byte[])command.ExecuteScalar(); MemoryStream s = new MemoryStream(arr); //反序列化 this.pictureBox2.Image = (Image)bf.Deserialize(s); connection.Close(); } private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e) { DialogResult result = openFileDialog1.ShowDialog(); if (result==DialogResult.OK) { this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); } } //保存 private void button2_Click(object sender, EventArgs e) { connection = new SqlConnection(conStr); //二进制格式化器 BinaryFormatter bf = new BinaryFormatter(); //内存流 MemoryStream ms=new MemoryStream(); //把图片格式化成二进制保存在内存流中 bf.Serialize(ms, this.pictureBox1.Image); //转换成字节数组 byte[] arr = ms.ToArray(); //@image变量 string sql = "insert into image(image) values(@image) "; SqlCommand command = new SqlCommand(sql,connection); //给变量赋值 command.Parameters.Add("@image", SqlDbType.Image).Value = arr; connection.Open(); command.ExecuteNonQuery(); connection.Close(); } } }
获取图片路径path FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read); byte [] b=new byte[fs.Length]; fs.Read(b, 0, fs.Length); 往数据库存,直接把字节数组Insert进去就行 如果要建立Image,可以先讲字节数组放入内存流中,再生成Image MemoryStream ms = new MemoryStream(b); Image image = Image.FromStream(ms);这种方法就很好了
估计楼主是要传到web服务器上 httpRequest?
Using wc As New Net.WebClient wc.UploadFile("http://www.baidu.com", "post", "c:\boot.ini") End Using 就完事了,或用webclient的几个重载方法,比较方便啦. WebClient,也有异步方式 如果还不爽,用httpwebrequest,再不爽就tcpclient,还不过瘾用socket.
如果是数据库
获取图片路径path
FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read);
byte [] b=new byte[fs.Length];
fs.Read(b, 0, fs.Length);
往数据库存,直接把字节数组Insert进去就行
如果要建立Image,可以先讲字节数组放入内存流中,再生成Image
MemoryStream ms = new MemoryStream(b);
Image image = Image.FromStream(ms);
思路把图片上传比较简单,用image控件直接可以上传。显示就要把读出来二进制转化成图片
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.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string conStr = "Data Source=.;Initial Catalog=text;Integrated Security=True";
SqlConnection connection;
public Form1()
{
InitializeComponent();
}
//显示
private void button1_Click(object sender, EventArgs e)
{
connection = new SqlConnection(conStr);
//二进制格式化器
BinaryFormatter bf = new BinaryFormatter(); string sql = "select image from image";//自己加条件啊
SqlCommand command = new SqlCommand(sql,connection);
connection.Open(); byte[] arr = (byte[])command.ExecuteScalar(); MemoryStream s = new MemoryStream(arr);
//反序列化
this.pictureBox2.Image = (Image)bf.Deserialize(s);
connection.Close();
} private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result==DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
} }
//保存
private void button2_Click(object sender, EventArgs e)
{
connection = new SqlConnection(conStr);
//二进制格式化器
BinaryFormatter bf = new BinaryFormatter();
//内存流
MemoryStream ms=new MemoryStream();
//把图片格式化成二进制保存在内存流中
bf.Serialize(ms, this.pictureBox1.Image);
//转换成字节数组
byte[] arr = ms.ToArray();
//@image变量
string sql = "insert into image(image) values(@image) ";
SqlCommand command = new SqlCommand(sql,connection);
//给变量赋值
command.Parameters.Add("@image", SqlDbType.Image).Value = arr;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}
获取图片路径path
FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read);
byte [] b=new byte[fs.Length];
fs.Read(b, 0, fs.Length);
往数据库存,直接把字节数组Insert进去就行
如果要建立Image,可以先讲字节数组放入内存流中,再生成Image
MemoryStream ms = new MemoryStream(b);
Image image = Image.FromStream(ms);这种方法就很好了
估计楼主是要传到web服务器上
httpRequest?
Using wc As New Net.WebClient
wc.UploadFile("http://www.baidu.com", "post", "c:\boot.ini")
End Using
就完事了,或用webclient的几个重载方法,比较方便啦.
WebClient,也有异步方式
如果还不爽,用httpwebrequest,再不爽就tcpclient,还不过瘾用socket.
还有就是使用最基本的http://模式模拟http方式去提交数据。
取出时记得也要用MemoryStream
//插入
private void button6_Click(object sender, EventArgs e)
{
count = objFinfo.GetScalar(_hID);
lb_imageCount.Text = count.ToString();
FilesData files = new FilesData();
files.Hid = _hID;
files.FName = imageName.Text.ToString();
files.FDate = Convert.ToDateTime(imageDate.Text.ToString());
byte[] imageFile;
imageFile = File.ReadAllBytes(ThumbnailPhotoFilePath);
files.Files = imageFile;
if (count == 0)
{
files.FGroup = 1;
}
else
{
count++;
files.FGroup = count;
} DataTable dt = objFinfo.GetSelectFiles(files.FName, _hID);
if (dt.Rows.Count == 0)
{
int result = objFinfo.InsertFiles(files);
if (result > 0)
{
MessageBox.Show("图像数据已经成功录入数据库", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
count = objFinfo.GetScalar(_hID);
lb_imageCount.Text = count.ToString();
lb_imageNow.Text = count.ToString();
}
else
MessageBox.Show("图像数据录入失败", "失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
MessageBox.Show("该图片已存在,请重新插入!");
}
FilesData files = new FilesData(); //显示图片方法
private void PicShow()
{
IDataReader dr = objFinfo.GetReader(_hID, index);
if (dr.Read())
{
files.Files = dr["files"] as byte[];
files.FName = dr["FName"].ToString();
files.FDate = Convert.ToDateTime(dr["FDate"]);
imageName.Text = files.FName;
imageDate.Text = files.FDate.ToString();
MemoryStream myStream = new MemoryStream();
foreach (byte p in files.Files)
{
myStream.WriteByte(p);
}
Image myImage = Image.FromStream(myStream);
pic_h.Image = myImage;
myStream.Close();
}
}
if (this.fupFile.HasFile)
{
string extension = Path.GetExtension(fupFile.FileName);
if (".gif,.jpg,.pjpeg,.jpeg".Contains(extension.ToLower()))
{
string RecordID = b.Guid;
string fileName = RecordID + extension;
string path = Winstar.Framework.WebUI.AppConfig.SYS_Attachment + "Avatar" + "\\"; if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
fupFile.SaveAs(path + fileName);
b.Person_Image = fileName;
}
else
{
Response.Write("<script>alert('只能上传图片格式,请重新选择!');</script>");
return false;
}
}