2014年4月11日星期五

把图片保存到数据库中和从数据库中读取图片--项目琐碎总结 - lhb62232397

本邮件内容由第三方提供,如果您不想继续收到该邮件,可 点此退订
把图片保存到数据库中和从数据库中读取图片--项目琐碎总结 - lhb62232397  阅读原文»

  最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。

  1、将图片作为其中的一个参数保存到数据库中

  在项目中,一般是将图片转换成二进制流格式,然后保存到数据库中。同时数据库表中存储图片的格式一般为image。此次项目,是将图片作为一个参数,和其他几个参数一起保存到数据库中,和在网上搜索到的图片保存不太一样,此处稍作修改,但都是检测过的。

  存储步骤:

  1、搜索到图片的路径

  2、读取图片并将图片转换成二进制流格式

  3、sql语句保存到数据库中。

  贴代码: 

private void btnWrite_Click(object sender, EventArgs e)
{
OpenFileDialog ofd
= new OpenFileDialog();
ofd.Filter
= "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";

if (ofd.ShowDialog() == DialogResult.OK)
{
string filePath = ofd.FileName;//图片路径
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] imageBytes = new byte[fs.Length];
BinaryReader br
= new BinaryReader(fs);
imageBytes
= br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流

string strSql = string.Format("insert into [SBS].[dbo].[Model] ([M_QRCode],[M_Skills] ) values (@image,'2')");
int count = Write(strSql,imageBytes );

if (count > 0)
{
MessageBox.Show(
"success");
}
else
{
MessageBox.Show(
"failed");
}
}
}

  数据库连接和保存图片语句:

private int Write(string strSql,byte[] imageBytes)
{
string connStr = "Data Source=192.168.4.132;initial Catalog=SBS;User ID=sa;Password=sa;";

using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(strSql, conn))
{
try
{
conn.Open();
SqlParameter sqlParameter
= new SqlParameter("@image", SqlDbType.Image);
sqlParameter.Value
= imageBytes;
cmd.Parameters.Add(sqlParameter);
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (Exception e)
{
throw;
}
}
}
}

  2、从数据库总读取图片

  从数据库中读取图片字段,并转换成内存流生成bitmap。

  贴代码: 

private void btnRead_Click(object sender, EventArgs e)
{
string strSql = string.Format("select M_QRCode from [SBS].[dbo].[Model] where M_id = 7");//图片保存的字段是M_QRCode
Read(strSql);
}

private void Read(string strSql)
{
string connStr = "Data Source=192.168.4.132;initial Catalog=SBS;User ID=sa;Password=sa;";

using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(strSql, conn))
{
conn.Open();
SqlDataReader sqlDr
= cmd.ExecuteReader();
sqlDr.Read();
byte[] images = (byte[])sqlDr["M_QRCode"];
MemoryStream ms
= new MemoryStream(images);
Bitmap bmp
= new Bitmap(ms);
pictureBox1.Image
= bmp;
}
}
}

  3、根据图片路径显示图片

  这个比较简单,直接贴出代码 

private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd
= new OpenFileDialog();
ofd.Filter
= "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if (ofd.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image
= Image.FromFile(ofd.FileName);
}
}

  4、打印图片

  打印图片是在将图片显示在pictureBox的基础上进行的。

  步骤:

  1、将printDocument控件拖到界面,添加打印代码

  2、设置PrintD

程序员知识体系 - 曹超  阅读原文»

  今天总结了下自己的知识结构体系。我是做C/C++开发的,所以可能比较侧重底层一些。

  园子里比较多的都是.net和java开发的,无论是什么语言或者什么阶段,也可以参考下,只是可以侧重点,深挖点不一样。

  比如专业知识,当你其他方面都有所了解之后,你能够深入,比如相关的数据库设计,优化,或者流媒体技术。

  每层知识也是相互深化与了解的,比如有时你下层的知识不懂或者没有涉及到,可以由上层的知识来加深,或者由上层的知识逐步挖到底层知识。

  我画的虽然是个纵向的结构体系,其实知识也是网状的,都相互有关联。所以当你接触一个新知识时,尽量对它进行拓展与延伸,从而构建出更大的知识网络。


本文链接:程序员知识体系,转载请注明。

阅读更多内容

没有评论:

发表评论