C#如何实现图片上传 如何实现在C#WinForm程序中实现图片的上传功能,不是将图片保存到服务端指定的文件夹,而是将图片通过转换保存到数据库的表中,数据库的表应该如何建立,字段使用什么类型;对已经上传的图片,如何下载下来,在PictureBox中显示。求指教,谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 google 数据库二进制保存图片 ado.net 存入数据库用 byte[]对应的数据类型 ,sql里面有好几种,貌似image类型也是有的读取就是System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);Image.FromStream(ms); 用二进制存取,在数据库用image存放 http://zhidao.baidu.com/question/528267717.html 最近刚实现了这个功能先添加控件OpenFIleDialog,按钮,文本框按钮事件openFIleDialog.InitialDirectory = "";openFIleDialog.Filter = "图片文件(*.jpg)|8.jpg|所有文件(*.*)|*.*";openFIleDialog.RestoreDirectory = true;openFIleDialog.Multiselect = true;if(openFIleDialog.ShowDialog() == DialogResult.OK){ txtPicPath.Text = openFileDialog.FileName;}将图片转换成二进制的方法peivate byte[] ChangePicType(string picPath){FileStrem fs = new FileStream(picPath,FileMode.Open,FileAccess.Read);byte[] buffByte = new byte[fs.length];fs.Read(buffByte ,0,Convert.ToInt32(fs.length));fs.Flush();fs.Close();return buffByte;}数据库的字段类型Bolb数据库保存方法ForOracle forOracle = new ForOracle();forOracle.AddParameters("ZP",zp,DataType.Blob);在PictureBox中显示的方法pictureBox.Image = GetIamge(dr["ZP"]);GetIamge()方法代码private Iamge GetIamge(byte[] buffer){MemoryStream ms = new MemoryStream(buffer);Iamge img = Iamge.FromStream(ms);return img;}希望对楼主有用啦! 记得给分哦,绝对亲自手打的! public class ImgCovert { //根据图片文件的路径使用文件流打开,并保存为byte[] public static byte[] GetPictureData(string imagepath) { FileStream fs = new FileStream(imagepath, FileMode.Open); byte[] byData = new byte[fs.Length]; fs.Read(byData, 0, byData.Length); fs.Close(); return byData; } //参数是Byte[]类型,返回值是Image对象: public static System.Drawing.Image ReturnPhoto(byte[] streamByte) { System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte); System.Drawing.Image img = System.Drawing.Image.FromStream(ms); return img; } } 不同的数据库建立不同的字段,oracl 是blog,ms 是image。然后将图片转化为二进制流存入字段即可 扩展方法怎么找 打印机问题 求一些工具软件? 请教一个数据库连接的问题! 一个很菜的问题 请教思归,孟子等大侠,为什么Enterprise Library与NUNIT的GUI运行,装配件老出错误?? 下载文件时路径被拒绝访问!!高手们来帮忙看一看啊!!! 大虾帮看一下,用C#写的 ,出错了。 {}有没有winform局域网下点到点文件传输的程序源码下载? 如何用DateGrid传递两个参数 c# 获取直连设备IP地址 c#方法中如何获取参数的原始属性名?
读取就是
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
Image.FromStream(ms);
先添加控件OpenFIleDialog,按钮,文本框
按钮事件
openFIleDialog.InitialDirectory = "";
openFIleDialog.Filter = "图片文件(*.jpg)|8.jpg|所有文件(*.*)|*.*";
openFIleDialog.RestoreDirectory = true;
openFIleDialog.Multiselect = true;
if(openFIleDialog.ShowDialog() == DialogResult.OK)
{
txtPicPath.Text = openFileDialog.FileName;
}
将图片转换成二进制的方法
peivate byte[] ChangePicType(string picPath)
{
FileStrem fs = new FileStream(picPath,FileMode.Open,FileAccess.Read);
byte[] buffByte = new byte[fs.length];
fs.Read(buffByte ,0,Convert.ToInt32(fs.length));
fs.Flush();
fs.Close();
return buffByte;
}数据库的字段类型Bolb
数据库保存方法
ForOracle forOracle = new ForOracle();
forOracle.AddParameters("ZP",zp,DataType.Blob);在PictureBox中显示的方法
pictureBox.Image = GetIamge(dr["ZP"]);GetIamge()方法代码
private Iamge GetIamge(byte[] buffer)
{
MemoryStream ms = new MemoryStream(buffer);
Iamge img = Iamge.FromStream(ms);
return img;
}希望对楼主有用啦! 记得给分哦,绝对亲自手打的!
{
//根据图片文件的路径使用文件流打开,并保存为byte[]
public static byte[] GetPictureData(string imagepath)
{
FileStream fs = new FileStream(imagepath, FileMode.Open);
byte[] byData = new byte[fs.Length];
fs.Read(byData, 0, byData.Length);
fs.Close();
return byData;
}
//参数是Byte[]类型,返回值是Image对象:
public static System.Drawing.Image ReturnPhoto(byte[] streamByte)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
return img;
}
}