小谈c#数据库存取图片的方式

第一种文件夹与数据库配合


近来做了不少关于这块的功能 ,随着网络的飞速发展,网络存取图片已不再是神话,而成为了一种时尚,如果是你 是用Asp.net开发的话,可能更多的人会考虑使用数据库存储图片的路经,而在文件夹是存储图片的方式。这种方式主要的方法有两个一个就是怎么样读取图片,怎么样存储图上,读取的话我就不多说的这个是最简单的了,只要大家把地址=给存储图片的对象就行了,在取的时候一般要使用相对地址也就是“~” 如下所示

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->ImageUrl="../CardDeal/SellCardZhi.jpg'
ImageUrl="~/CardDeal/SellCardZhi.jpg'

当然这前面要加上你自己的图片所在服务器的文件夹的名称

我们来看是一下是怎么存储的吧,我常用的一个方法是这样的

小谈c#数据库存取图片的方式小谈c#数据库存取图片的方式代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->///<summary>
///上传图片
///</summary>
///<paramname="FUSShopURL">FileUpload对象</param>
///<paramname="UpladURL">图片要放到的目录名称</param>
///<returns>如果FileUpload不为空则返回上传后的图片位置,否则返回为空字符</returns>
publicstaticstringuploadImage(FileUploadFUSShopURL,stringUpladURL)
{
if(FUSShopURL.HasFile)
{
//获取当前的时间,一当作图片的名字
stringfileName=DateTime.Now.ToString("yyyyMMddhhmmss")+DateTime.Now.Millisecond.ToString();
//获取图片的扩展名
stringExtent=System.IO.Path.GetExtension(FUSShopURL.PostedFile.FileName);
//重命名图片
fileName+=Extent;
//设置上传图片保存的文件夹
stringdir=System.Web.HttpContext.Current.Server.MapPath(UpladURL);
//指定图片的路径及文件名
stringpath=dir+"//"+fileName;
//把上传得图片保存到指定的文件加中
FUSShopURL.PostedFile.SaveAs(path);
returnfileName;
}
else
{
return"";
}
}

这个方法是与FileUpload控件 一起使用的,方法很简单大家一看就明白了。

方法返回的就是一个相对的路经可以直接存储的数据里,然后从前台调用就可以了

第二种直接把图片的Base64String码进行存取

这种方法很方便,直接转化一下就行了,不需要书写很麻烦的路经问题

先看一下是怎么存储到数据库的吧

小谈c#数据库存取图片的方式小谈c#数据库存取图片的方式代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->//选择图片
privatevoidbutton1_Click(objectsender,EventArgse)
{
OpenFileDialogopenfile
=newOpenFileDialog();
openfile.Title
="请选择客户端longin的图片";
openfile.Filter
="Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if(DialogResult.OK==openfile.ShowDialog())
{
try
{
Bitmapbmp
=newBitmap(openfile.FileName);
pictureBox1.Image
=bmp;
pictureBox1.SizeMode
=PictureBoxSizeMode.Zoom;
MemoryStreamms
=newMemoryStream();
bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
byte[]arr=newbyte[ms.Length];
ms.Position
=0;
ms.Read(arr,
0,(int)ms.Length);
ms.Close();
//直接返这个值放到数据就行了
pic=Convert.ToBase64String(arr);
}
catch{}
}
}

读取的方法也很简单,pic就是我们得到的图片字符串只要我们存储到数据库里,从下面的方法里读取就可以了

需要注意的地方我都加的有注释

小谈c#数据库存取图片的方式小谈c#数据库存取图片的方式代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->//加载图片
privatevoidForm1_Load(objectsender,EventArgse)
{
try
{
//pic=........这一句换成从数据库里读取就可以了
//判断是否为空,为空时的不执行
if(!string.IsNullOrEmpty(pic))
{
//直接返Base64码转成数组
byte[]imageBytes=Convert.FromBase64String(pic);
//读入MemoryStream对象
MemoryStreammemoryStream=newMemoryStream(imageBytes,0,imageBytes.Length);
memoryStream.Write(imageBytes,
0,imageBytes.Length);
//转成图片
Imageimage=Image.FromStream(memoryStream);

//memoryStream.Close();//不要加上这一句否则就不对了

//将图片放置在PictureBox中
this.pictureBox1.SizeMode=PictureBoxSizeMode.Zoom;
this.pictureBox1.Image=image;
}
}
catch{}
}

大家看一下效果吧

小谈c#数据库存取图片的方式

第三种方式 读成二进制后进行存取


先把图片读成二进制以后再做处理,这样快捷而且代码相对少很多,还有就是感谢下面几位网友的提醒和建议,在这里我把我简单写的代码贴一下,怎么样存储到数据库的方法还是大家自己写我只提供存取的方法

小谈c#数据库存取图片的方式小谈c#数据库存取图片的方式代码
privatevoidbutton1_Click(objectsender,EventArgse)
{
OpenFileDialogopenfile
=newOpenFileDialog();
openfile.Title
="请选择客户端longin的图片";
openfile.Filter
="Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if(DialogResult.OK==openfile.ShowDialog())
{
try
{
//读成二进制
byte[]bytes=File.ReadAllBytes(openfile.FileName);
//直接返这个存储到数据就行了cmd.Parameters.Add("@image",SqlDbType.Image).Value=bytes;

//输出二进制在这里把数据中取到的值放在这里byte[]bytes=(byte[])model.image;
pictureBox1.Image=System.Drawing.Image.FromStream(newMemoryStream(bytes));
this.pictureBox1.SizeMode=PictureBoxSizeMode.Zoom;

//如果保存成文件:
File.WriteAllBytes(@"d:/text.jpg",bytes);
}
catch{}
}
}

在这里我们只要单击选择图片直接就可以更换。这些很简单但是我个人感觉还是很常用的,而且网上关于这块的例子着实不少,不过真正能帮上忙的还真不多,因为我们的好几个项目里用到了这些方法,或多或少的还是有些员工不怎么会, 在这里贴一贴方便新手查看吧。呵呵

下面的本例子的所有代码

小谈c#数据库存取图片的方式小谈c#数据库存取图片的方式代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO;
usingSystem.Threading;

namespaceWindowsFormsApplication1
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}

stringpic="";
//加载图片
privatevoidForm1_Load(objectsender,EventArgse)
{
try
{
if(!string.IsNullOrEmpty(pic))
{
byte[]imageBytes=Convert.FromBase64String(pic);
MemoryStreammemoryStream
=newMemoryStream(imageBytes,0,imageBytes.Length);
memoryStream.Write(imageBytes,
0,imageBytes.Length);
Imageimage
=Image.FromStream(memoryStream);

//将图片放置在PictureBox中
this.pictureBox1.SizeMode=PictureBoxSizeMode.Zoom;
this.pictureBox1.Image=image;
}
}
catch{}
}

//选择图片
privatevoidbutton1_Click(objectsender,EventArgse)
{
OpenFileDialogopenfile
=newOpenFileDialog();
openfile.Title
="请选择客户端longin的图片";
openfile.Filter
="Login图片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if(DialogResult.OK==openfile.ShowDialog())
{
try
{
Bitmapbmp
=newBitmap(openfile.FileName);
pictureBox1.Image
=bmp;
pictureBox1.SizeMode
=PictureBoxSizeMode.Zoom;
MemoryStreamms
=newMemoryStream();
bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
byte[]arr=newbyte[ms.Length];
ms.Position
=0;
ms.Read(arr,
0,(int)ms.Length);
ms.Close();
pic
=Convert.ToBase64String(arr);
}
catch{}
}
}
}
}

欢迎大家转载,如有转载请注明文章来自:http://sufei.cnblogs.com/

签名:做一番一生引以为豪的事业;在有生之年报答帮过我的人;并有能力帮助需要帮助的人;

QQ:361983679 Email:[email protected] MSN:[email protected]