数据库存储图片

摘要:
第二种方式直接把图片的Base64String码进行存取这种方法很方便,直接转化一下就行了,不需要书写很麻烦的路经问题,先看一下是怎么存储到数据库的吧://选择图片privatevoidbutton1_Click{OpenFileDialogopenfile=newOpenFileDialog();openfile.Title="请选择客户端longin的图片";openfile.Filter="Login图片|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";if{try{Bitmapbmp=newBitmap;pictureBox1.Image=bmp;pictureBox1.SizeMode=PictureBoxSizeMode.Zoom;MemoryStreamms=newMemoryStream();bmp.Save;byte[]arr=newbyte[ms.Length];ms.Position=0;ms.Read;ms.Close();//直接返这个值放到数据就行了pic=Convert.ToBase64String;}catch{}}}读取的方法也很简单,pic就是我们得到的图片字符串只要我们存储到数据库里,从下面的方法里读取就可以了。

数据库存储图片第1张

///<summary>///上传图片
///</summary>///<param name="FUSShopURL"& gt;FileUpload对象</param>///<param name="UpladURL">图片要放到的目录名称</param>///<returns>如果FileUpload不为空则返回上传后的图片位置,否则返回为空字符</returns>publicstaticstringuploadImage(FileUpload FUSShopURL, 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码进行存取

这种方法很方便,直接转化一下就行了,不需要书写很麻烦的路经问题,先看一下是怎么存储到数据库的吧:

//选择图片privatevoidbutton1_Click(objectsender, EventArgs e)
{
OpenFileDialog openfile
=newOpenFileDialog();
openfile.Title
="请选择客户端longin的图片";
openfile.Filter
="Login图片 (*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if(DialogResult.OK ==openfile.ShowDialog())
{
try
{
Bitmap bmp
=newBitmap(openfile.FileName);
pictureBox1.Image
=bmp;
pictureBox1.SizeMode
=PictureBoxSizeMode.Zoom;
MemoryStream ms
=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就是我们得到的图片字符串只要我们存储到数据库里,从下面的方法里读取就可以了。需要注意的地方我都加的有注释:

//加载图片privatevoidForm1_Load(objectsender, EventArgs e)
{
try
{
//pic=........这一句换成从数据库里读取就可以了
//判断是否为空,为空时的不执行if(!string.IsNullOrEmpty(pic))
{
//直接返Base64码转成数组byte[] imageBytes =Convert.FromBase64String(pic);
//读入MemoryStream对象MemoryStream memoryStream =newMemoryStream(imageBytes, 0,imageBytes.Length);
memoryStream.Write(imageBytes,
0, imageBytes.Length);
//转成图片Image image =Image.FromStream(memoryStream);
//memoryStream.Close(); //不要加上这一句否则就不对了
//将图片放置在 PictureBox 中this.pictureBox1.SizeMode =PictureBoxSizeMode.Zoom;
this.pictureBox1.Image =image;
}
}
catch{ }
}
大家看一下效果吧:

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

下面的本例子的所有代码:

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, EventArgs e)
{
try
{
if(!string.IsNullOrEmpty(pic))
{
byte[] imageBytes =Convert.FromBase64String(pic);
MemoryStream memoryStream
=newMemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes,
0, imageBytes.Length);
Image image
=Image.FromStream(memoryStream);
//将图片放置在 PictureBox 中this.pictureBox1.SizeMode =PictureBoxSizeMode.Zoom;
this.pictureBox1.Image =image;
}
}
catch{ }
}
//选择图片privatevoidbutton1_Click(objectsender, EventArgs e)
{
OpenFileDialog openfile
=newOpenFileDialog();
openfile.Title
="请选择客户端longin的图片";
openfile.Filter
="Login图片 (*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
if(DialogResult.OK ==openfile.ShowDialog())
{
try
{
Bitmap bmp
=newBitmap(openfile.FileName);
pictureBox1.Image
=bmp;
pictureBox1.SizeMode
=PictureBoxSizeMode.Zoom;
MemoryStream ms
=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{ }
}
}
}
}

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

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

privatevoidbutton1_Click(objectsender, EventArgs e)
{
OpenFileDialog openfile
=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{ }
}
}

免责声明:文章转载自《数据库存储图片》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【翻译】API-First是什么概念?有什么商业价值?分享一个Winform里面的HTML编辑控件Zeta HTML Edit Control,汉化附源码下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

win10局域网共享报错:不允许一个用户使用一个以上用户名与服务器或共享资源的多重连接

计算机A:共享者(本地文件库);共享访客(工作计算机);计算机A打开另一个帐户-aaa,密码-aaa123。开始文件共享后,计算机B单击网络,发现计算机A已连接。输入帐户密码后,将弹出以下错误。不允许用户使用多个用户名多次连接到服务器或共享资源:断开与此服务器或共享的资源的所有连接。在此链接之前:存在现有连接,或者在建立连接时,现有网络环境已更改,导致帐户被...

Visual Studio Code 之 运行java代码

2、安装成功后,会在右键菜单中多出一个选项:更改vscode“用户设置”文件:添加java.home以及runcode显示在终端coderunner原生的配置有些问题,更改其中终端的命令:加入红色的部分3、点击RunCode,会执行相应的文件...

Json 的日期格式转化(时区标准化)

在JavaScript中,这无疑可以通过初始化Data()对象//converttomsecsinceJan11970localTime=d轻松完成。获取时间();步骤2:接下来,通过Data()对象的getTimezoneOffset()方法//obtainlocalUTCoffsetandconverttomseclocalOffset=d找出本地时间偏...

【转】QImage 图像格式小结

构造图像:,QImagemyImage1=QImage;根据文件名打开图像。如果图像本身是32位或24位,则程序中的图像是32位。如果图像本身是8位或1位,则程序中的对应图像是8位或者1位。宽度表示图像宽度,高度表示图像高度。...

NodeJs使用jwt生成token以及使用express-jwt校验和解密token

=0){//当数据库有当前用户时,它返回tokenlettoken=jwt.sign;res.send}else{res.send}}catch{//p抛出异常并将其发送到错误中间件以处理console.log;next;}})//注册接口路由器。post('/register',异步(req,res,next)=˃{let{用户名,密码,昵称}=req-b...

利用油猴插件实现全网VIP视频免费看

利用油猴插件实现全网VIP视频免费看第一步:首先打开谷歌应用商店搜索tampermonkey安装这个插件第二步:在百度搜索框搜索油猴可以看到以下页面,点击进入。下载谷歌上网助手解压后,将后缀为crx的文件拖入即可。之后注册一个谷歌上网助手账后登录即可进入谷歌应用商店油猴插件...