| 订阅 | 在线投稿
分享
 
 
 

C# 2.0中的上传多个图片合并及存入数据库的示例

2008-12-13 08:24:10 编辑來源:互联网 国际版 评论
 
 
本文为【C# 2.0中的上传多个图片合并及存入数据库的示例】的汉字拼音对照版显示拼音
  xiangmushangyaoqiubatupiancunrushujukuyoushihoutupianshifenkaidedanshiyaozaishangchuanshihebingshangxiahebingxinchuandetupianfanghoumianteshuxuyaoheheGooglelebantianwangshangyoutupiancunrushujukudeshili便bianshihebingdejiujibenmeiyongyouyeshishaoliangqitacaozuofangshidedaimapianduanjiehewangshangsiluyanjiuchutupianhebingdebijiaowanzhengdedaimaruxia

  daimaleixingC# 2.0

  shuomingciwenbenzhanyuanchuangzhuanzaiqingzhumingchuchu

  shangchuanwenjian使shiyongdeshiVS2005zidaideshangchuankongjian

  shengluexiangguanshangchuandejiqitabuzenmetaiyouguanxidedaimashangchuandaimakanMSDNbangzhubijiaowanzheng

  hebingliuchengshangchuanyizhangcunrushujukuranhouzaishangchuanyizhanghezhidingcunrushujukudetupianhebinghougengxinshujukuziduanview plaincopy to clipboardprint?

  //fu_SelectImageVS2005zidaishangchuankongjian

  byte[] FileByteArray = fu_SelectImage.FileBytes;//tuxiangwenjianlinshichucunByteshuzu

  Stream StreamObject = fu_SelectImage.FileContent;//jianlishujuliuduixiang

  

  Response.Write("wenjianchangduwei"+ FileLength + "wenjianmingwei" + fu_SelectImage.FileName + "wenjianleixingwei" + fu_SelectImage.GetType().Name);//yonglaizijikankande

  

  //dingyiyigexindebyte[]yonglaicunfangshujukuzhongdetupianbyte[]

  byte[] FileByteOldArray = new byte[1];

  

  DataHandle dh = new DataHandle(); //zhegeshipengyouxiehaodeshujukucaozuoleizhijienalaiyongshanglezhuyaoshiduqushujukuzhidingziduanneirong

  dh.TargetTableName = "ImageStore";//cuntupiandeshujubiao

  dh.DbConditionAdd("ImageID",5); //tupiandeIDIDkeyicongwangzhishenshuchuhuodegetimage.aspx?id=5

  SqlDataReader rd = dh.ExecuteReader();

  if (rd.Read())

  {

  FileByteOldArray = (byte[])rd["ImageData"];//congshujukuquchutupiandebyte[]shujukuciziduanweiImageleixing

  }

  rd.Close();

  dh.Close();

  

  //jiangtuxiangdezijieshuzufangruneicunliu

  MemoryStream oldms = new MemoryStream(FileByteOldArray); //cunfangshujukudetupianzijieshuzuneicunliu

  MemoryStream newms = new MemoryStream(); //yonglaicunfanghebinghoudeneicunliu

  

  //hebingweituzhegebufenshiguanjiancongzheliyekeyiyanhuazuoyouhebingzhileidedangrantupiandesuoxiaoyechabuduosilu

  Bitmap b1 = new Bitmap(oldms); //oldmslaodecunzaishujukudeneicunliu

  Bitmap b2 = new Bitmap(StreamObject);//StreamObjectxinshangchuandetupianliu

  Bitmap b = new Bitmap(b1.Width > b2.Width ? b1.Width : b2.Width, b1.Height + b2.Height+1);

  Graphics g = Graphics.FromImage(b);

  g.DrawImage(b1,0,0);

  g.DrawImage(b2, 0, b1.Height+1);//shangxiahebingbaxintupianfangdaolaotupiandexiamianzhege1zhuyaoshiweileyou1pxdejiangeyekeyiqude

  

  //bahebinghoudeweitubaocundaoneicunliuzhong

  b.Save(newms, ImageFormat.Jpeg);

  

  //conghebinghoutupiandeneicunliuzhongqudecunfangshujukusuoxudexiangguanshenshu

  byte[] FileByteNewArray = newms.GetBuffer();

  FileLength = FileByteNewArray.Length;

  

  //shifangziyuan

  oldms.Dispose();

  newms.Dispose();

  b1.Dispose();

  b2.Dispose();

  b.Dispose();

  g.Dispose();

  

  //jianliSQL Serverlianjie

  SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=MapPointManageDemo;User ID=sa;Pwd=sa;");

  //String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,ImageDescription, ImageSize) valueS (@Image, @ContentType,@ImageDescription, @ImageSize)";

  String SqlCmd = "UPDATE ImageStore SET ImageData = @Image, ImageSize = ImageSize + @ImageSize WHERE ImageID = 5";

  SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);

  CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteNewArray;//baocuntupianbyte[]

  //CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = "image/jpeg"; //jiluwenjianleixing

  //CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;//baqitadanbiaoshujujilushangchuan

  CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;//jiluwenjianchangduduqushi使shiyong

  Con.Open();

  CmdObj.ExecuteNonQuery();

  Con.Close();

  //fu_SelectImageVS2005zidaishangchuankongjian

  byte[] FileByteArray = fu_SelectImage.FileBytes;//tuxiangwenjianlinshichucunByteshuzu

  Stream StreamObject = fu_SelectImage.FileContent;//jianlishujuliuduixiang

  Response.Write("wenjianchangduwei"+ FileLength + "wenjianmingwei" + fu_SelectImage.FileName + "wenjianleixingwei" + fu_SelectImage.GetType().Name);//yonglaizijikankande

  //dingyiyigexindebyte[]yonglaicunfangshujukuzhongdetupianbyte[]

  byte[] FileByteOldArray = new byte[1];

  DataHandle dh = new DataHandle(); //zhegeshipengyouxiehaodeshujukucaozuoleizhijienalaiyongshanglezhuyaoshiduqushujukuzhidingziduanneirong

  dh.TargetTableName = "ImageStore";//cuntupiandeshujubiao

  dh.DbConditionAdd("ImageID",5); //tupiandeIDIDkeyicongwangzhishenshuchuhuodegetimage.aspx?id=5

  SqlDataReader rd = dh.ExecuteReader();

  if (rd.Read())

  {

  FileByteOldArray = (byte[])rd["ImageData"];//congshujukuquchutupiandebyte[]shujukuciziduanweiImageleixing

  }

  rd.Close();

  dh.Close();

  //jiangtuxiangdezijieshuzufangruneicunliu

  MemoryStream oldms = new MemoryStream(FileByteOldArray); //cunfangshujukudetupianzijieshuzuneicunliu

  MemoryStream newms = new MemoryStream(); //yonglaicunfanghebinghoudeneicunliu

  //hebingweituzhegebufenshiguanjiancongzheliyekeyiyanhuazuoyouhebingzhileidedangrantupiandesuoxiaoyechabuduosilu

  Bitmap b1 = new Bitmap(oldms); //oldmslaodecunzaishujukudeneicunliu

  Bitmap b2 = new Bitmap(StreamObject);//StreamObjectxinshangchuandetupianliu

  Bitmap b = new Bitmap(b1.Width > b2.Width ? b1.Width : b2.Width, b1.Height + b2.Height+1);

  Graphics g = Graphics.FromImage(b);

  g.DrawImage(b1,0,0);

  g.DrawImage(b2, 0, b1.Height+1);//shangxiahebingbaxintupianfangdaolaotupiandexiamianzhege1zhuyaoshiweileyou1pxdejiangeyekeyiqude

  //bahebinghoudeweitubaocundaoneicunliuzhong

  b.Save(newms, ImageFormat.Jpeg);

  //conghebinghoutupiandeneicunliuzhongqudecunfangshujukusuoxudexiangguanshenshu

  byte[] FileByteNewArray = newms.GetBuffer();

  FileLength = FileByteNewArray.Length;

  //shifangziyuan

  oldms.Dispose();

  newms.Dispose();

  b1.Dispose();

  b2.Dispose();

  b.Dispose();

  g.Dispose();

  //jianliSQL Serverlianjie

  SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=MapPointManageDemo;User ID=sa;Pwd=sa;");

  //String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,ImageDescription, ImageSize) valueS (@Image, @ContentType,@ImageDescription, @ImageSize)";

  String SqlCmd = "UPDATE ImageStore SET ImageData = @Image, ImageSize = ImageSize + @ImageSize WHERE ImageID = 5";

  SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);

  CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteNewArray;//baocuntupianbyte[]

  //CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = "image/jpeg"; //jiluwenjianleixing

  //CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;//baqitadanbiaoshujujilushangchuan

  CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;//jiluwenjianchangduduqushi使shiyong

  Con.Open();

  CmdObj.ExecuteNonQuery();

  Con.Close();fudaidianqitadetupiancaozuomoshi

  laiyuanyuhttp://topic.csdn.net/t/20050225/15/3806327.htmlview plaincopy to clipboardprint?

  //chulitupiandaxiaodaozhidingchicunfanhuizhiweiyigeImageduixiang使shiyongImageduixiangdeSavefangfajiukeyibaocungaitupian

  //xiangxiyongfachaMSDN

  private System.Drawing.Image PhotoSizeChange(string strPhoto)

  {

  //strPhotoshiyuanlaidetupianwenjiansuozaidewulilujing

  //chulitupiangongneng

  System.Drawing.Image image = new Bitmap(strPhoto);//dedaoyuantu

  //chuangjianzhidingdaxiaodetu

  System.Drawing.Image newImage = image.GetThumbnailImage(zhidingkuan(xiangsuzhi int), zhidinggao(xiangsuzhi int), null, new IntPtr());

  Graphics g=Graphics.FromImage(newImage);

  //jiangyuantuhuadaozhidingdetushang

  g.DrawImage(newImage,X,Y, newImage.Width, newImage.Height);

  g.Dispose();

  return newImage;

  }

http://blog.breakn.net/article.asp?id=325原文
 
 
  项目上要求把图片存入数据库,有时候图片是分开的但是要在上传时合并(上下合并,新传的图片放后面,特殊需要呵呵)。Google了半天网上有图片存入数据库的示例,便是合并的就基本没用,有也是少量其它操作方式的代码片段。结合网上思路,研究出图片合并的比较完整的代码如下:   代码类型:C# 2.0   说明:此文本站原创,转载请注明出处      上传文件使用的是VS2005自带的上传控件      省略相关上传的及其它不怎么太有关系的代码,上传代码看MSDN帮助,比较完整      合并流程:上传一张,存入数据库,然后再上传一张和指定存入数据库的图片合并后更新数据库字段view plaincopy to clipboardprint? //fu_SelectImage(VS2005自带上传控件) byte[] FileByteArray = fu_SelectImage.FileBytes;//图象文件临时储存Byte数组 Stream StreamObject = fu_SelectImage.FileContent;//建立数据流对像 Response.Write("文件长度为:"+ FileLength + "文件名为:" + fu_SelectImage.FileName + "文件类型为:" + fu_SelectImage.GetType().Name); //用来自己看看的。 //定义一个新的byte[]用来存放数据库中的图片byte[] byte[] FileByteOldArray = new byte[1]; DataHandle dh = new DataHandle(); //这个是朋友写好的数据库操作类直接拿来用上了,主要是读取数据库指定字段内容。 dh.TargetTableName = "ImageStore"; //存图片的数据表 dh.DbConditionAdd("ImageID",5); //图片的ID(ID可以从网址参数处获得getimage.aspx?id=5) SqlDataReader rd = dh.ExecuteReader(); if (rd.Read()) { FileByteOldArray = (byte[])rd["ImageData"]; //从数据库取出图片的byte[],数据库此字段为Image类型 } rd.Close(); dh.Close(); //将图像的字节数组放入内存流 MemoryStream oldms = new MemoryStream(FileByteOldArray); //存放数据库的图片字节数组内存流 MemoryStream newms = new MemoryStream(); //用来存放合并后的内存流 //合并位图,这个部分是关键,从这里也可以演化左右合并之类的。当然图片的缩小也差不多思路 Bitmap b1 = new Bitmap(oldms); //oldms老的存在数据库的内存流 Bitmap b2 = new Bitmap(StreamObject); //StreamObject新上传的图片流 Bitmap b = new Bitmap(b1.Width > b2.Width ? b1.Width : b2.Width, b1.Height + b2.Height+1); Graphics g = Graphics.FromImage(b); g.DrawImage(b1,0,0); g.DrawImage(b2, 0, b1.Height+1);//上下合并,把新图片放到老图片的下面,这个1主要是为了有1px的间隔,也可以去的。 //把合并后的位图保存到内存流中 b.Save(newms, ImageFormat.Jpeg); //从合并后图片的内存流中取得存放数据库所需的相关参数 byte[] FileByteNewArray = newms.GetBuffer(); FileLength = FileByteNewArray.Length; //释放资源 oldms.Dispose(); newms.Dispose(); b1.Dispose(); b2.Dispose(); b.Dispose(); g.Dispose(); //建立SQL Server链接 SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=MapPointManageDemo;User ID=sa;Pwd=sa;"); //String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,ImageDescription, ImageSize) valueS (@Image, @ContentType,@ImageDescription, @ImageSize)"; String SqlCmd = "UPDATE ImageStore SET ImageData = @Image, ImageSize = ImageSize + @ImageSize WHERE ImageID = 5"; SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteNewArray;//保存图片byte[] //CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = "image/jpeg"; //记录文件类型 //CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;//把其它单表数据记录上传 CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;//记录文件长度,读取时使用 Con.Open(); CmdObj.ExecuteNonQuery(); Con.Close(); //fu_SelectImage(VS2005自带上传控件) byte[] FileByteArray = fu_SelectImage.FileBytes;//图象文件临时储存Byte数组 Stream StreamObject = fu_SelectImage.FileContent;//建立数据流对像 Response.Write("文件长度为:"+ FileLength + "文件名为:" + fu_SelectImage.FileName + "文件类型为:" + fu_SelectImage.GetType().Name); //用来自己看看的。 //定义一个新的byte[]用来存放数据库中的图片byte[] byte[] FileByteOldArray = new byte[1]; DataHandle dh = new DataHandle(); //这个是朋友写好的数据库操作类直接拿来用上了,主要是读取数据库指定字段内容。 dh.TargetTableName = "ImageStore"; //存图片的数据表 dh.DbConditionAdd("ImageID",5); //图片的ID(ID可以从网址参数处获得getimage.aspx?id=5) SqlDataReader rd = dh.ExecuteReader(); if (rd.Read()) { FileByteOldArray = (byte[])rd["ImageData"]; //从数据库取出图片的byte[],数据库此字段为Image类型 } rd.Close(); dh.Close(); //将图像的字节数组放入内存流 MemoryStream oldms = new MemoryStream(FileByteOldArray); //存放数据库的图片字节数组内存流 MemoryStream newms = new MemoryStream(); //用来存放合并后的内存流 //合并位图,这个部分是关键,从这里也可以演化左右合并之类的。当然图片的缩小也差不多思路 Bitmap b1 = new Bitmap(oldms); //oldms老的存在数据库的内存流 Bitmap b2 = new Bitmap(StreamObject); //StreamObject新上传的图片流 Bitmap b = new Bitmap(b1.Width > b2.Width ? b1.Width : b2.Width, b1.Height + b2.Height+1); Graphics g = Graphics.FromImage(b); g.DrawImage(b1,0,0); g.DrawImage(b2, 0, b1.Height+1);//上下合并,把新图片放到老图片的下面,这个1主要是为了有1px的间隔,也可以去的。 //把合并后的位图保存到内存流中 b.Save(newms, ImageFormat.Jpeg); //从合并后图片的内存流中取得存放数据库所需的相关参数 byte[] FileByteNewArray = newms.GetBuffer(); FileLength = FileByteNewArray.Length; //释放资源 oldms.Dispose(); newms.Dispose(); b1.Dispose(); b2.Dispose(); b.Dispose(); g.Dispose(); //建立SQL Server链接 SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=MapPointManageDemo;User ID=sa;Pwd=sa;"); //String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,ImageDescription, ImageSize) valueS (@Image, @ContentType,@ImageDescription, @ImageSize)"; String SqlCmd = "UPDATE ImageStore SET ImageData = @Image, ImageSize = ImageSize + @ImageSize WHERE ImageID = 5"; SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteNewArray;//保存图片byte[] //CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = "image/jpeg"; //记录文件类型 //CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;//把其它单表数据记录上传 CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;//记录文件长度,读取时使用 Con.Open(); CmdObj.ExecuteNonQuery(); Con.Close();附带点其它的图片操作模式 来源于:[url=http://topic.csdn.net/t/20050225/15/3806327.htmlview]http://topic.csdn.net/t/20050225/15/3806327.htmlview[/url] plaincopy to clipboardprint? //处理图片大小到指定尺寸,返回值为一个Image对象,使用Image对象的Save方法就可以保存该图片 //详细用法查MSDN private System.Drawing.Image PhotoSizeChange(string strPhoto) { //strPhoto是原来的图片文件所在的物理路径 //处理图片功能 System.Drawing.Image image = new Bitmap(strPhoto);//得到原图 //创建指定大小的图 System.Drawing.Image newImage = image.GetThumbnailImage(指定宽(像素值 int), 指定高(像素值 int), null, new IntPtr()); Graphics g=Graphics.FromImage(newImage); //将原图画到指定的图上 g.DrawImage(newImage,X,Y, newImage.Width, newImage.Height); g.Dispose(); return newImage; } [url=http://blog.breakn.net/article.asp?id=325]http://blog.breakn.net/article.asp?id=325[/url]
󰈣󰈤
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

网络安全治理:国家安全保障的主要方向是打击犯罪,而不是处置和惩罚受害者

来源:中国青年报 新的攻击方法不断涌现,黑客几乎永远占据网络攻击的上风,我们不可能通过技术手段杜绝网络攻击。国家安全保障的主要方向是打击犯罪,而不是处置和惩罚...

 
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
单纯美女 迷人女孩
校园甜美少女
忍辱负重
大学校园
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有