1. public void iTextSharpCreatPDF()
  2. {
  3. string pdfpath = System.Web.HttpContext.Current.Server.MapPath("~/log/DPD/");
  4. string imagepath = System.Web.HttpContext.Current.Server.MapPath("~/log/DPD/");
  5. Document doc = new Document(new Rectangle(, ), , , , ); //new Rectangle(1000,1000)
  6. //指定文件预设开档时的缩放为100%
  7. //PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);
  8. try
  9. {
  10. PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/DPD_15505984238198.pdf", FileMode.Create));
  11. doc.Open();
  12.  
  13. //下面对图片进行操作
  14. Image image = Image.GetInstance(imagepath + "/DPD_15505984238198.jpg");
  15. float percentage = ;
  16. //这里都是图片最原始的宽度与高度
  17. float resizedWidht = image.Width;
  18. float resizedHeight = image.Height;
  19.  
  20. ////这时判断图片宽度是否大于页面宽度减去也边距,如果是,那么缩小,如果还大,继续缩小,
  21. ////这样这个缩小的百分比percentage会越来越小
  22. //while (resizedWidht > (doc.PageSize.Width - doc.LeftMargin - doc.RightMargin) * 0.8)
  23. //{
  24. // percentage = percentage * 0.9f;
  25. // resizedHeight = image.Height * percentage;
  26. // resizedWidht = image.Width * percentage;
  27. //}
  28. ////There is a 0.8 here. If the height of the image is too close to the page size height,
  29. ////the image will seem so big
  30. //while (resizedHeight > (doc.PageSize.Height - doc.TopMargin - doc.BottomMargin) * 0.8)
  31. //{
  32. // percentage = percentage * 0.9f;
  33. // resizedHeight = image.Height * percentage;
  34. // resizedWidht = image.Width * percentage;
  35. //}
  36.  
  37. ////这里用计算出来的百分比来缩小图片
  38. image.ScalePercent(percentage * );
  39. //让图片的中心点与页面的中心店进行重合
  40. image.SetAbsolutePosition(doc.PageSize.Width / - resizedWidht / , doc.PageSize.Height / - resizedHeight / );
  41. doc.Add(image);
  42.  
  43. }
  44. catch (DocumentException dex)
  45. {
  46. System.Web.HttpContext.Current.Response.Write(dex.Message);
  47. }
  48. catch (IOException ioex)
  49. {
  50. System.Web.HttpContext.Current.Response.Write(ioex.Message);
  51. }
  52.  
  53. catch (Exception ex)
  54. {
  55. System.Web.HttpContext.Current.Response.Write(ex.Message);
  56. }
  57. finally
  58. {
  59. doc.Close();
  60. }
  61. }
  1. .NET二进制图片存储:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].
  2.   .参数是图片路径:返回Byte[]类型:
  3.  
  4.   .public byte[] GetPictureData(string imagepath)
  5.   . {
  6.   . //根据图片文件的路径使用文件流打开,并保存为byte[]
  7.   . FileStream fs = new FileStream(imagepath, FileMode.Open);
  8.   . byte[] byData = new byte[fs.Length];
  9.   . fs.Read(byData, , byData.Length);
  10.   . fs.Close();
  11.   . return byData;
  12.   . }
  13.   .
  14.  
  15.   .参数类型是Image对象,返回Byte[]类型:
  16.  
  17.  .public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
  18.   . {
  19.   . //将Image转换成流数据,并保存为byte[]
  20.   . MemoryStream mstream = new MemoryStream();
  21.   . imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
  22.   . byte[] byData = new Byte[mstream.Length];
  23.   . mstream.Position = ;
  24.   . mstream.Read(byData, , byData.Length);
  25.   . mstream.Close();
  26.   . return byData;
  27.   . }
  28.  
  29.   好了,这样通过上面的方法就可以把图片转换成Byte[]对象,然后就把这个对象保存到数据库中去就实现了把图片的二进制格式保存到数据库中去了。下面我就谈谈如何把数据库中的图片读取出来,实际上这是一个相反的过程。
  30.   .NET二进制图片读取:把相应的字段转换成Byte[]即:Byte[] bt=(Byte[])XXXX
  31.   .参数是Byte[]类型,返回值是Image对象:
  32.  
  33. .public System.Drawing.Image ReturnPhoto(byte[] streamByte)
  34.   . {
  35.   . System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
  36.   . System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
  37.   . return img;
  38.   . }
  39.  
  40.   .参数是Byte[] 类型,没有返回值,这是针对asp.net中把图片从输出到网页上(Response.BinaryWrite)
  41.  
  42.   .public void WritePhoto(byte[] streamByte)
  43.   . {
  44.   . // Response.ContentType 的默认值为默认值为“text/html”
  45.   . Response.ContentType = "image/GIF";
  46.   . //图片输出的类型有: image/GIF image/JPEG
  47.   . Response.BinaryWrite(streamByte);
  48.   . }
  49.  
  50.   补充:
  51.   针对Response.ContentType的值,除了针对图片的类型外,还有其他的类型:
  52.  
  53.   .Response.ContentType = "application/msword";
  54.   . Response.ContentType = "application/x-shockwave-flash";
  55.   . Response.ContentType = "application/vnd.ms-excel";
  56.  
  57.   另外可以针对不同的格式,用不同的输出类型以适合不同的类型:
  58.  
  59.   .switch (dataread("document_type"))
  60.   . {
  61.   . case "doc":
  62.   . Response.ContentType = "application/msword";
  63.   . case "swf":
  64.   . Response.ContentType = "application/x-shockwave-flash";
  65.   . case "xls":
  66.   . Response.ContentType = "application/vnd.ms-excel";
  67.   . case "gif":
  68.   . Response.ContentType = "image/gif";
  69.   . case "Jpg":
  70.   . Response.ContentType = "image/jpeg";
  71.   . }
  72.  
  73. protected void Button1_Click(object sender, EventArgs e)
  74. {
  75. if (FileUpload1.HasFile)
  76. {
  77. Stream mystream = FileUpload1.PostedFile.InputStream;//FileUpload1是上传控件..
  78. int length = FileUpload1.PostedFile.ContentLength;
  79. byte[] bytes = new byte[length];
  80. mystream.Read(bytes, , length);
  81. mystream.Close();
  82. string conn = "server=(local);database=Image;Uid=sa;Pwd=123456 ";
  83. SqlConnection myconn = new SqlConnection(conn);
  84. myconn.Open();
  85. string str= "insert into image (img) values( ' " + bytes + " ') ";
  86. SqlCommand mycomm = new SqlCommand(str, myconn);
  87. mycomm.ExecuteNonQuery();
  88. myconn.Close();
  89. }
  90. }
  91. 上面把图片存入数据库...
  92. 下面是把图片从数据库中读出来...
  93. protected void Button2_Click(object sender, EventArgs e)
  94. {
  95. string imgtype = FileUpload1.PostedFile.ContentType;
  96. string conn = "server=(local);database=Image;Uid=sa;Pwd=123456 ";
  97. string str= "select img from image where ID=5 ";
  98. SqlConnection myconn = new SqlConnection(conn);
  99. myconn.Open();
  100. //SqlDataAdapter myda = new SqlDataAdapter(str, conn);
  101. //DataSet myds =new DataSet();
  102. //myda.Fill(myds);
  103. SqlCommand mycom = new SqlCommand(str, myconn);
  104. SqlDataReader mydr = mycom.ExecuteReader();
  105. if (mydr.Read())
  106. {
  107. Response.ContentType = imgtype;
  108. Response.BinaryWrite((byte[])mydr[ "img "]);
  109. }
  110. else
  111. {
  112. Response.Write( "没有从数据库中读取图片 ");
  113. }
  114. myconn.Close();
  115. }
  116.  
  117. 一、发生的背景
  118. 在开发新项目中使用了新的语言开发C#和新的技术方案WEB Service,但是在新项目中,一些旧的模块需要继续使用,一般是采用C或C++或Delphi编写的,如何利用旧模块对于开发人员来说,有三种可用方法供选择:第一、将C或C++函数用C#彻底改写一遍,这样整个项目代码比较统一,维护也方便一些。但是尽管微软以及某些书籍说,C#和C++如何接近,但是改写起来还是很痛苦的事情,特别是C++里的指针和内存操作;第二、将C或C++函数封装成COM,在C#中调用COM比较方便,只是在封装时需要处理C或C++类型和COM类型之间的转换,也有一些麻烦,另外COM还需要注册,注册次数多了又可能导致混乱;第三、将C或C++函数封装成动态链接库,封装的过程简单,工作量不大。因此我决定采用加载动态链接库的方法实现,于是产生了在C#中如何调用自定义的动态链接库问题,我在网上搜索相关主题,发现一篇调用系统API的文章,但是没有说明如何解决此问题,在MSDN上也没有相关详细说明。基于此,我决定自己从简单出发,逐步试验,看看能否达到自己的目标。
  119. (说明一点:我这里改写为什么很怕麻烦,我改写的代码是变长加密算法函数,代码有600多行,对算法本身不熟悉,算法中指针和内存操作太多,要想保证算法正确,最可行的方法就是少动代码,否则只要有一点点差错,就不能肯定算法与以前兼容)
  120.  
  121. 二、技术实现
  122. 下面看看如何逐步实现动态库的加载,类型的匹配:
  123.  
  124. 动态链接库函数导出的定义,这个不需要多说,大家参考下面宏定义即可:
  125. #define LIBEXPORT_API extern "C" __declspec(dllexport)
  126.  
  127. 第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和:
  128. LIBEXPORT_API int mySum(int a,int b){ return a+b;}
  129.  
  130. C#定义导入定义:
  131. public class RefComm
  132. {
  133. [DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] public static extern int mySum (int a,int b);
  134. }
  135.  
  136. C#中调用测试:
  137. int iSum= RefComm. mySum(,);
  138. 运行查看结果iSum5,调用正确。第一步试验完成,说明在C#中能够调用自定义的动态链接库函数。
  139.  
  140. 第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函数名),返回结果为字符串:
  141. LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,”%s”,a) return a;}
  142.  
  143. C#定义导入定义:
  144. public class RefComm
  145. {
  146. [DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, string b);
  147. }
  148.  
  149. C#中调用测试:
  150. string strDest=””;
  151. string strTmp= RefComm. mySum(“”, strDest);
  152. 运行查看结果strTmp为“”,但是strDest为空。
  153.  
  154. 我修改动态链接库实现,返回结果为串b
  155. LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,”%s”,a) return b;}
  156.  
  157. 修改C#导入定义,将串b修改为ref方式:
  158. public class RefComm
  159. {
  160. [DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, ref string b);
  161. }
  162. C#中再调用测试:
  163. string strDest=””;
  164. string strTmp= RefComm. mySum(“”, ref strDest);
  165. 运行查看结果strTmpstrDest均不对,含不可见字符。
  166.  
  167. 再修改C#导入定义,将CharSet从Auto修改为Ansi:
  168. public class RefComm
  169. {
  170. [DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, string b);
  171. }
  172. C#中再调用测试:
  173. string strDest=””;
  174. string strTmp= RefComm. mySum(“”, ref strDest);
  175. 运行查看结果strTmp为“”,但是串strDest没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。
  176.  
  177. 再次修改C#导入定义,将串b修改为引用(ref):
  178. public class RefComm
  179. {
  180. [DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, ref string b);
  181. }
  182. 运行时调用失败,不能继续执行。
  183.  
  184. 第三步,修改动态链接库实现,将b修改为双重指针:
  185. LIBEXPORT_API char *mySum(char *a,char **b){sprintf((*b),”%s”,a) return *b;}
  186.  
  187. C#导入定义:
  188. public class RefComm
  189. {
  190. [DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, ref string b);
  191. }
  192.  
  193. C#中调用测试:
  194. string strDest=””;
  195. string strTmp= RefComm. mySum(“”, ref strDest);
  196. 运行查看结果strTmpstrDest均为“”,调用正确。第三步实现了函数出口参数正确输出结果。
  197.  
  198. 第四步,修改动态链接库实现,实现整数参数的输出:
  199. LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;}
  200.  
  201. C#导入的定义:
  202. public class RefComm
  203. {
  204. [DllImport("LibEncrypt.dll", EntryPoint=" mySum ",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern int mySum (int a, int b,ref int c);
  205. }
  206.  
  207. C#中调用测试:
  208. int c=;
  209. int iSum= RefComm. mySum(,, ref c);
  210. 运行查看结果iSum c均为5,调用正确。
  211.  
  212. 经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在C#定义导入,有此基础,很快我实现了变长加密函数在C#中的调用,至此目标实现。
  213.  
  214. 三、结论
  215. C#中,调用C++编写动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于C#的导入定义,则需要使用引用(ref)定义。
  216. 对于函数返回值,C#导入定义和C++动态库函数申明定义需要保持一致,否则会出现函数调用失败。
  217. 定义导入时,一定注意CharSetCallingConvention参数,否则导致调用失败或结果异常。
  218. 运行时,动态链接库放在C#程序的目录下即可,我这里是一个C#的动态链接库,两个动态链接库就在同一个目录下运行。

NET二进制图片存储与读取的常见方法,iTextSharp添加图片生成PDF文件的更多相关文章

  1. [轉載]史上最强php生成pdf文件,html转pdf文件方法

    之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...

  2. 史上最强php生成pdf文件,html转pdf文件方法

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  3. PDF怎样添加注释,PDF文件添加注释的方法

    Word文件跟纸质文件想要添加注释相信大家都知道该怎么添加,那么现在也使用频率挺高的PDF格式的文件要怎么添加注释呢?添加注释的方法有什么呢?有许多的小伙伴们都想知道吧,今天小编就来跟大家分享一下,想 ...

  4. 编辑方法分享之如何编辑PDF文件内容

    我们现在在工作中会经常使用到PDF文件,还会有遇到需要编辑PDF文件的时候,PDF文件的编辑问题一直是个大难题.很多朋友在面对PDF文件的时候束手无策,不知道该怎么对它进行编辑.下面小编就教给大家一个 ...

  5. 前端axios请求二进制数据流转换生成PDF文件空白问题(终极解决方案)

    本文章共1570字,预计阅读时间1 - 3分钟. 问题场景: axios请求二进制数据转换生成PDF空白问题,使用axios请求后端接口,后端返回的二进制流文件,需要转换成PDF,但是在postman ...

  6. 关于java poi itext生成pdf文件的例子以及方法

    最近正在做导出pdf文件的功能,所以查了了一些相关资料,发现不是很完善,这里做一些小小的感想,欢迎各位“猿”童鞋批评指正. poi+itext,所需要的jar包有itext-2.1.7.jar,poi ...

  7. 一个Itextsharp 批量添加图片到pdf 方法

    这里我就直接把我的页面贴进来了 using System; using System.Collections.Generic; using System.Web; using System.Web.U ...

  8. asp.net 生成PDF方法

    今天转博客园看到有人发表了一篇生成PFd的文章,准备自己也留一份准备以后用到的时候方便调用: 首先去itextsharp网站下载控件(https://sourceforge.net/projects/ ...

  9. TensorFlow高效读取数据的方法——TFRecord的学习

    关于TensorFlow读取数据,官网给出了三种方法: 供给数据(Feeding):在TensorFlow程序运行的每一步,让python代码来供给数据. 从文件读取数据:在TensorFlow图的起 ...

随机推荐

  1. 【shell】shell基础脚本合集

    1.向脚本传递参数 #!/bin/bash #功能:打印文件名与输入参数 #作者:OLIVER echo $0 #打印文件名 echo $1 #打印输入参数 执行结果: 2.在脚本中使用参数 #!/b ...

  2. python之函数用法setdefault()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法setdefault() #D.get(k,d) #说明:k在D中,则返回 D[K], ...

  3. python之函数用法endswith()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法endswith() #http://www.runoob.com/python/at ...

  4. Win7 64bit下值得推荐的免费看图软件

    自从更换到Win7 64bit后, 用了十多年的AcdSee3.x不能再正常工作了. 找到了两个替代品: Faststone Image Viewer 和 XnView Faststone Image ...

  5. Swift3 Scanner用法之判断是否数字、提取字符串里的数字

    1.判断是否数字 /// 判断是否是数字 /// /// - Parameter string: <#string description#> /// - Returns: <#re ...

  6. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  7. http状态--status[查询的资料备注]

    HTTP 状态消息 当浏览器从 web 服务器请求服务时,可能会发生错误. 从而有可能会返回下面的一系列状态消息: 1xx: 信息 消息: 描述: 100 Continue 服务器仅接收到部分请求,但 ...

  8. linux下网络配置小节[from 老男孩的linux运维笔记]

    对于linux高手看似简单的网络配置问题,也许要说出所以然来也并不轻松,因此仍然有太多的初学者徘徊在门外就不奇怪了, 这里,老男孩老师花了一些时间总结了这个文档小结,也还不够完善,欢迎大家补充,交流. ...

  9. 阿里面试的一点感受 阿里ali片式经历和面试题

    阿里面试的一点感受 <!-- [废话开始] 百度实习三个月,明天就要离职了,感觉还挺开心的,同事们都很照顾我,Boss也比较欣赏我,我很满足了.掐指一算,这大四其实也没几个月了,同事们都在感叹大 ...

  10. k8s实战之Service

    一.概述 为了适应快速的业务需求,微服务架构已经逐渐成为主流,微服务架构的应用需要有非常好的服务编排支持,k8s中的核心要素Service便提供了一套简化的服务代理和发现机制,天然适应微服务架构,任何 ...