TextItem.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /*******************************************************************************
  2. * Copyright(c) 2012 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:TextItem.cs
  5. * 2.功能描述: 文本Item
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2012/09/14 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Drawing;
  13. using System.Globalization;
  14. using System.IO;
  15. using Curtain.Framework.Barcode;
  16. using Curtain.Framework.Barcode.OneD;
  17. using Curtain.Framework.Barcode.QRCode;
  18. using ZXing;
  19. using ZXing.Common;
  20. using ZXing.QrCode;
  21. using ZXing.QrCode.Internal;
  22. namespace Dongke.WinForm.Controls.InvoiceLayout
  23. {
  24. [Serializable]
  25. public class TextItem : LayoutItem, IBoundItem
  26. {
  27. #region 成员变量
  28. private string _dataMember; // 对应数据源中的字段名
  29. private string _displayValue; // 编辑模式显示的值
  30. private object _formatType; // 格式类别
  31. private string _format; // 文本格式
  32. private string _barcodeFormat; // 文本格式
  33. private string _qrCodeLevel; // 文本格式
  34. private Font _font; // 文本字体
  35. private bool _isUnderline; // 下划线
  36. private bool _isStrikeout; // 删除线
  37. private bool _wrap; // 是否自动换行
  38. private bool _clip; // 是否剪裁延伸到边框外侧的文本
  39. private Color _textColor; // 文本颜色
  40. private TextAlignment _textAlign; // 文本对齐方式
  41. private TextAlignment _textAlignVertical; // 文本垂直对齐
  42. private float _characterSpace; // 文字间隔
  43. private float _lineSpace; // 文字行间隔
  44. private int _characterCount; // 行文字数
  45. private string _defaultValue; // 文本默认值
  46. private string _culture; // culture
  47. private string _defaultItemName; // 默认项目名
  48. private string _textItemName; // 文本项目名
  49. private bool _isDataBinding; // 是否数据绑定
  50. //[NonSerialized]
  51. //private string _text; // 文本
  52. //[NonSerialized]
  53. //private object _dataValue; // 数据源中的值
  54. //[NonSerialized]
  55. //private object _printDataValue; // 打印数据源中的值
  56. //[NonSerialized]
  57. //private DataBoundField _boundField; // 数据源字段
  58. //[NonSerialized]
  59. //private DataBoundField _printBoundField; // 打印数据源字段
  60. //[NonSerialized]
  61. //private Font _fontDraw; // 绘制字体
  62. //[NonSerialized]
  63. //private float _lineSpaceDraw; // 绘制行间隔
  64. //[NonSerialized]
  65. //private float _characterSpaceDraw; // 绘制文字间隔
  66. //[NonSerialized]
  67. //protected RectangleF _marginRectangleFDraw; // 绘制范围
  68. private int _travelDocItemID; // 项目ID
  69. private string _itemCode; // 项目ID
  70. private int _itemRefClass; // 分类
  71. private ItemStyle _itemStyle; // 项目类别
  72. private string _itemSample; // 项目示例
  73. #endregion 成员变量
  74. #region 属性
  75. /// <summary>
  76. /// 对应数据源中的字段名
  77. /// </summary>
  78. public string DataMember
  79. {
  80. get
  81. {
  82. return _dataMember;
  83. }
  84. set
  85. {
  86. if (string.IsNullOrEmpty(value))
  87. {
  88. value = string.Empty;
  89. }
  90. if (!value.Equals(_dataMember))
  91. {
  92. _dataMember = value;
  93. //if (Owner != null)
  94. //{
  95. // if (Owner.LayoutMode == LayoutMode.Preview)
  96. // {
  97. // Owner.RefreshTextItemBound(this);
  98. // }
  99. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  100. //}
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 编辑模式显示的值
  106. /// </summary>
  107. public string DisplayValue
  108. {
  109. get
  110. {
  111. return _displayValue;
  112. }
  113. set
  114. {
  115. if (string.IsNullOrEmpty(value))
  116. {
  117. value = string.Empty;
  118. }
  119. if (!value.Equals(_displayValue))
  120. {
  121. _displayValue = value;
  122. //if (Owner != null)
  123. //{
  124. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  125. //}
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// 格式类别
  131. /// </summary>
  132. public object FormatType
  133. {
  134. get
  135. {
  136. return _formatType;
  137. }
  138. set
  139. {
  140. _formatType = value;
  141. //if (Owner != null)
  142. //{
  143. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  144. //}
  145. }
  146. }
  147. /// <summary>
  148. /// 文本格式
  149. /// </summary>
  150. public string Format
  151. {
  152. get
  153. {
  154. return _format;
  155. }
  156. set
  157. {
  158. if (string.IsNullOrEmpty(value))
  159. {
  160. value = string.Empty;
  161. }
  162. if (!value.Equals(_format))
  163. {
  164. _format = value;
  165. //if (Owner != null)
  166. //{
  167. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  168. //}
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// 一维码格式
  174. /// </summary>
  175. public string BarcodeFormat
  176. {
  177. get
  178. {
  179. return _barcodeFormat;
  180. }
  181. set
  182. {
  183. if (string.IsNullOrEmpty(value))
  184. {
  185. value = string.Empty;
  186. }
  187. if (!value.Equals(_barcodeFormat))
  188. {
  189. _barcodeFormat = value;
  190. }
  191. }
  192. }
  193. /// <summary>
  194. /// 二维码级别
  195. /// </summary>
  196. public string QRCodeLevel
  197. {
  198. get
  199. {
  200. return _qrCodeLevel;
  201. }
  202. set
  203. {
  204. if (string.IsNullOrEmpty(value))
  205. {
  206. value = string.Empty;
  207. }
  208. if (!value.Equals(_qrCodeLevel))
  209. {
  210. _qrCodeLevel = value;
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 文本字体
  216. /// </summary>
  217. public Font Font
  218. {
  219. get
  220. {
  221. return _font;
  222. }
  223. set
  224. {
  225. if (!_font.Equals(value))
  226. {
  227. _font = value;
  228. _isUnderline = _font.Underline;
  229. _isStrikeout = _font.Strikeout;
  230. //float zoom = base.GetZoom();
  231. //_fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  232. //if (Owner != null)
  233. //{
  234. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  235. //}
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// 是否自动文本换行
  241. /// </summary>
  242. public bool Wrap
  243. {
  244. get
  245. {
  246. return _wrap;
  247. }
  248. set
  249. {
  250. if (_wrap != value)
  251. {
  252. _wrap = value;
  253. _clip = !_wrap;
  254. //if (Owner != null)
  255. //{
  256. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  257. //}
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// 是否剪裁延伸到边框外侧的文本
  263. /// </summary>
  264. public bool Clip
  265. {
  266. get
  267. {
  268. _clip = !_wrap;
  269. return _clip;
  270. }
  271. }
  272. /// <summary>
  273. /// 文本颜色
  274. /// </summary>
  275. public Color TextColor
  276. {
  277. get
  278. {
  279. return _textColor;
  280. }
  281. set
  282. {
  283. if (!_textColor.Equals(value))
  284. {
  285. _textColor = value;
  286. //if (Owner != null)
  287. //{
  288. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  289. //}
  290. }
  291. }
  292. }
  293. /// <summary>
  294. /// 文本对齐方式
  295. /// </summary>
  296. public TextAlignment TextAlign
  297. {
  298. get
  299. {
  300. return _textAlign;
  301. }
  302. set
  303. {
  304. if (_textAlign != value)
  305. {
  306. _textAlign = value;
  307. //if (Owner != null)
  308. //{
  309. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  310. //}
  311. }
  312. }
  313. }
  314. /// <summary>
  315. /// 文本垂直对齐方式
  316. /// </summary>
  317. public TextAlignment TextAlignVertical
  318. {
  319. get
  320. {
  321. return _textAlignVertical;
  322. }
  323. set
  324. {
  325. if (_textAlignVertical != value)
  326. {
  327. _textAlignVertical = value;
  328. //if (Owner != null)
  329. //{
  330. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  331. //}
  332. }
  333. }
  334. }
  335. /// <summary>
  336. /// 文字间隔
  337. /// </summary>
  338. public float CharacterSpace
  339. {
  340. get
  341. {
  342. return _characterSpace;
  343. }
  344. set
  345. {
  346. if (_characterSpace != value)
  347. {
  348. _characterSpace = value;
  349. //if (Owner != null)
  350. //{
  351. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  352. //}
  353. }
  354. }
  355. }
  356. /// <summary>
  357. /// 文字行间隔
  358. /// </summary>
  359. public float LineSpace
  360. {
  361. get
  362. {
  363. return _lineSpace;
  364. }
  365. set
  366. {
  367. if (_lineSpace != value)
  368. {
  369. _lineSpace = value;
  370. //if (Owner != null)
  371. //{
  372. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  373. //}
  374. }
  375. }
  376. }
  377. /// <summary>
  378. /// 行文字数
  379. /// </summary>
  380. public int CharacterCount
  381. {
  382. get
  383. {
  384. return _characterCount;
  385. }
  386. set
  387. {
  388. if (_characterCount != value)
  389. {
  390. _characterCount = value;
  391. //if (Owner != null)
  392. //{
  393. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  394. //}
  395. }
  396. }
  397. }
  398. /// <summary>
  399. /// 文本默认值
  400. /// </summary>
  401. public string DefaultValue
  402. {
  403. get
  404. {
  405. return _defaultValue;
  406. }
  407. set
  408. {
  409. if (string.IsNullOrEmpty(value))
  410. {
  411. value = string.Empty;
  412. }
  413. if (!value.Equals(_defaultValue))
  414. {
  415. _defaultValue = value;
  416. //if (Owner != null)
  417. //{
  418. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  419. //}
  420. }
  421. }
  422. }
  423. /// <summary>
  424. /// culture
  425. /// </summary>
  426. public string Culture
  427. {
  428. get
  429. {
  430. return _culture;
  431. }
  432. set
  433. {
  434. if (string.IsNullOrEmpty(value))
  435. {
  436. value = string.Empty;
  437. }
  438. if (!value.Equals(_culture))
  439. {
  440. _culture = value;
  441. //if (Owner != null)
  442. //{
  443. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  444. //}
  445. }
  446. }
  447. }
  448. /// <summary>
  449. /// 文本项目名
  450. /// </summary>
  451. public string TextItemName
  452. {
  453. get
  454. {
  455. return _textItemName;
  456. }
  457. set
  458. {
  459. if (string.IsNullOrEmpty(value))
  460. {
  461. value = string.Empty;
  462. }
  463. if (!value.Equals(_textItemName))
  464. {
  465. _textItemName = value;
  466. //if (Owner != null)
  467. //{
  468. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  469. //}
  470. }
  471. }
  472. }
  473. /// <summary>
  474. /// 默认项目名
  475. /// </summary>
  476. public string DefaultItemName
  477. {
  478. get
  479. {
  480. return _defaultItemName;
  481. }
  482. set
  483. {
  484. if (string.IsNullOrEmpty(value))
  485. {
  486. value = string.Empty;
  487. }
  488. if (!value.Equals(_defaultItemName))
  489. {
  490. _defaultItemName = value;
  491. //if (Owner != null)
  492. //{
  493. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  494. //}
  495. }
  496. }
  497. }
  498. ///// <summary>
  499. ///// 数据源中的值
  500. ///// </summary>
  501. //internal object DataValue
  502. //{
  503. // get
  504. // {
  505. // return _dataValue;
  506. // }
  507. // set
  508. // {
  509. // if (value != _dataValue
  510. // || (value != null && !value.Equals(_dataValue)))
  511. // {
  512. // _dataValue = value;
  513. // }
  514. // }
  515. //}
  516. ///// <summary>
  517. ///// 打印数据源中的值
  518. ///// </summary>
  519. //internal object PrintDataValue
  520. //{
  521. // get
  522. // {
  523. // return _printDataValue;
  524. // }
  525. // set
  526. // {
  527. // if (value != _printDataValue
  528. // || (value != null && !value.Equals(_printDataValue)))
  529. // {
  530. // _printDataValue = value;
  531. // }
  532. // }
  533. //}
  534. /// <summary>
  535. /// 是否数据绑定
  536. /// </summary>
  537. internal bool IsDataBinding
  538. {
  539. get
  540. {
  541. return _isDataBinding;
  542. }
  543. set
  544. {
  545. _isDataBinding = value;
  546. }
  547. }
  548. ///// <summary>
  549. ///// 数据源字段
  550. ///// </summary>
  551. //internal DataBoundField BoundField
  552. //{
  553. // get
  554. // {
  555. // return _boundField;
  556. // }
  557. // set
  558. // {
  559. // _boundField = value;
  560. // }
  561. //}
  562. ///// <summary>
  563. ///// 打印数据源字段
  564. ///// </summary>
  565. //internal DataBoundField PrintBoundField
  566. //{
  567. // get
  568. // {
  569. // return _printBoundField;
  570. // }
  571. // set
  572. // {
  573. // _printBoundField = value;
  574. // }
  575. //}
  576. ///// <summary>
  577. ///// 绘制字体
  578. ///// </summary>
  579. //internal Font FontDraw
  580. //{
  581. // get
  582. // {
  583. // return _fontDraw;
  584. // }
  585. //}
  586. ///// <summary>
  587. ///// 绘制文字间隔
  588. ///// </summary>
  589. //internal float CharacterSpaceDraw
  590. //{
  591. // get
  592. // {
  593. // return _characterSpaceDraw;
  594. // }
  595. //}
  596. ///// <summary>
  597. ///// 绘制行间隔
  598. ///// </summary>
  599. //internal float LineSpaceDraw
  600. //{
  601. // get
  602. // {
  603. // return _lineSpaceDraw;
  604. // }
  605. //}
  606. /// <summary>
  607. /// 项目ID
  608. /// </summary>
  609. public int TravelDocItemID
  610. {
  611. get
  612. {
  613. return _travelDocItemID;
  614. }
  615. set
  616. {
  617. if (_travelDocItemID != value)
  618. {
  619. _travelDocItemID = value;
  620. //if (Owner != null)
  621. //{
  622. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  623. //}
  624. }
  625. if (0 < _travelDocItemID)
  626. {
  627. _isDataBinding = true;
  628. }
  629. else
  630. {
  631. _isDataBinding = false;
  632. }
  633. }
  634. }
  635. /// <summary>
  636. /// 项目编码
  637. /// </summary>
  638. public string ItemCode
  639. {
  640. get
  641. {
  642. return this._itemCode;
  643. }
  644. set
  645. {
  646. if (this._itemCode != value)
  647. {
  648. this._itemCode = value;
  649. }
  650. if (string.IsNullOrEmpty(this._itemCode))
  651. {
  652. this._isDataBinding = false;
  653. }
  654. else
  655. {
  656. this._isDataBinding = true;
  657. }
  658. }
  659. }
  660. /// <summary>
  661. /// 分类
  662. /// </summary>
  663. public int ItemRefClass
  664. {
  665. get
  666. {
  667. return _itemRefClass;
  668. }
  669. set
  670. {
  671. if (value != _itemRefClass)
  672. {
  673. _itemRefClass = value;
  674. //if (Owner != null)
  675. //{
  676. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  677. //}
  678. }
  679. }
  680. }
  681. /// <summary>
  682. /// 项目类别
  683. /// </summary>
  684. public ItemStyle ItemStyle
  685. {
  686. get
  687. {
  688. return _itemStyle;
  689. }
  690. set
  691. {
  692. if (value != _itemStyle)
  693. {
  694. _itemStyle = value;
  695. //this._picCode = null;
  696. //if (Owner != null)
  697. //{
  698. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  699. //}
  700. }
  701. }
  702. }
  703. /// <summary>
  704. /// 项目示例
  705. /// </summary>
  706. public string ItemSample
  707. {
  708. get
  709. {
  710. return _itemSample;
  711. }
  712. set
  713. {
  714. if (string.IsNullOrEmpty(value))
  715. {
  716. value = string.Empty;
  717. }
  718. if (!value.Equals(_itemSample))
  719. {
  720. _itemSample = value;
  721. //if (Owner != null)
  722. //{
  723. // Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
  724. //}
  725. }
  726. }
  727. }
  728. public bool PicColumn
  729. {
  730. get;
  731. set;
  732. }
  733. public bool PicMargin
  734. {
  735. get;
  736. set;
  737. }
  738. public bool FixedRatio
  739. {
  740. get;
  741. set;
  742. }
  743. #endregion 属性
  744. #region 构造函数
  745. /// <summary>
  746. /// 构造函数
  747. /// </summary>
  748. internal TextItem()
  749. : this(ItemType.Text)
  750. {
  751. }
  752. ///// <summary>
  753. ///// 构造函数
  754. ///// </summary>
  755. //internal TextItem(LayoutBox box)
  756. // : this(box, ItemType.Text)
  757. //{
  758. //}
  759. /// <summary>
  760. /// 构造函数
  761. /// </summary>
  762. internal TextItem(/*LayoutBox box,*/ ItemType itemType)
  763. : base(/*box, */itemType)
  764. {
  765. //_width = LayoutCommon.DefaultTextItemSizeWidth;
  766. //_height = LayoutCommon.DefaultTextItemSizeHeight;
  767. _displayValue = string.Empty;
  768. _format = string.Empty;
  769. _defaultValue = string.Empty;
  770. //_font = LayoutCommon.DefaultItemFont;
  771. //_fontDraw = LayoutCommon.DefaultItemFont;
  772. _textColor = Color.Black;
  773. _textAlign = TextAlignment.LeftOrTop;
  774. _characterSpace = 0f;
  775. _lineSpace = 0f;
  776. _characterCount = 0;
  777. //_dataValue = null;
  778. //_text = string.Empty;
  779. _itemRefClass = 0;
  780. _itemStyle = ItemStyle.Other;
  781. _isDataBinding = false;
  782. //_boundField = new DataBoundField();
  783. //_boundField.IsDataBound = false;
  784. //_boundField.BoundFieldIndex = -1;
  785. //_printBoundField = new DataBoundField();
  786. //_printBoundField.IsDataBound = false;
  787. //_printBoundField.BoundFieldIndex = -1;
  788. //_defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  789. _textItemName = _defaultItemName;
  790. }
  791. #endregion 构造函数
  792. #region 函数
  793. /// <summary>
  794. /// 格式化数据源中的值
  795. /// </summary>
  796. private string FormatDataValue(object value)
  797. {
  798. string result = null;
  799. if (value != null && value != DBNull.Value)
  800. {
  801. if (_itemStyle == ItemStyle.Date)
  802. {
  803. result = value.ToString();
  804. DateTime dateTime;
  805. if (DateTime.TryParse(result, out dateTime))
  806. {
  807. if (string.IsNullOrEmpty(_format))
  808. {
  809. result = dateTime.ToString(LayoutConsts.DATETIME_FRORMAT);
  810. }
  811. else
  812. {
  813. //if (LayoutConsts.CULTUREINFO_JP.Equals(_culture))
  814. //{
  815. // result = dateTime.ToString(_format, LayoutCommon.CultureInfoJP);
  816. //}
  817. //else
  818. //{
  819. // MMM
  820. if (_format.Contains(LayoutConsts.DATETIME_FRORMAT_MMM))
  821. {
  822. if (string.IsNullOrEmpty(_culture))
  823. {
  824. result = dateTime.ToString(_format).ToUpper();
  825. }
  826. else
  827. {
  828. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture)).ToUpper();
  829. }
  830. }
  831. else if (string.IsNullOrEmpty(_culture))
  832. {
  833. result = dateTime.ToString(_format);
  834. }
  835. else
  836. {
  837. result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture));
  838. }
  839. //}
  840. }
  841. }
  842. }
  843. else if (_itemStyle == ItemStyle.Sign)
  844. {
  845. if (string.IsNullOrEmpty(_format))
  846. {
  847. result = value.ToString();
  848. }
  849. else
  850. {
  851. result = _format;
  852. }
  853. }
  854. else
  855. {
  856. result = value.ToString();
  857. }
  858. }
  859. return result;
  860. }
  861. /// <summary>
  862. /// 文本内容设置
  863. /// </summary>
  864. public object ResetTextValue(object value, bool isDataBound)
  865. {
  866. //if (Owner == null)
  867. //{
  868. // return _displayValue;
  869. //}
  870. //if (Owner.LayoutMode == LayoutMode.Preview)
  871. {
  872. if (!string.IsNullOrEmpty(_dataMember))
  873. {
  874. if (isDataBound)
  875. {
  876. //object result = value;//Owner.SetItemValueFormattingArgs(this, value, _format);
  877. if (PicColumn)
  878. {
  879. //if (result != null && result is Image)
  880. //{
  881. // return result;
  882. //}
  883. if (value != null && value is byte[])
  884. {
  885. using (MemoryStream ms = new MemoryStream(value as byte[]))
  886. {
  887. return Image.FromStream(ms);
  888. }
  889. }
  890. return value;
  891. }
  892. //if (result != null)
  893. //{
  894. // return result.ToString();
  895. //}
  896. if (value != null && value != DBNull.Value)
  897. {
  898. return FormatDataValue(value);
  899. }
  900. return _defaultValue;
  901. }
  902. //return _defaultValue;
  903. return "";
  904. }
  905. return _displayValue;
  906. }
  907. //else
  908. //{
  909. // //object result = Owner.SetItemValueFormattingArgs(this, value, _format);
  910. // object result = Owner.SetItemValueFormattingArgs(this, _displayValue, _format);
  911. // if (result != null)
  912. // {
  913. // return result.ToString();
  914. // }
  915. // return _displayValue;
  916. //}
  917. }
  918. /// <summary>
  919. /// Item初始化
  920. /// </summary>
  921. /// <param name="box">LayoutBox</param>
  922. /// <param name="isNewID">是否生成新ID</param>
  923. /// <returns>是否成功</returns>
  924. internal override bool Init(/*LayoutBox box, bool isNewID*/)
  925. {
  926. //if (_fontDraw == null)
  927. //{
  928. // if (_font == null)
  929. // {
  930. // _font = LayoutCommon.DefaultItemFont;
  931. // _fontDraw = LayoutCommon.DefaultItemFont;
  932. // }
  933. // else
  934. // {
  935. // _fontDraw = new Font(_font.FontFamily, _font.Size, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
  936. // }
  937. //}
  938. //_boundField.IsDataBound = false;
  939. //_boundField.BoundFieldIndex = -1;
  940. //_printBoundField.IsDataBound = false;
  941. //_printBoundField.BoundFieldIndex = -1;
  942. bool isInit = base.Init(/*box, isNewID*/);
  943. //if (isNewID)
  944. //{
  945. // _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
  946. // _textItemName = _defaultItemName;
  947. //}
  948. return isInit;
  949. }
  950. ///// <summary>
  951. ///// Item图形上的OnPaint事件
  952. ///// </summary>
  953. ///// <param name="sender">指定的对象</param>
  954. ///// <param name="e">提供的事件数据</param>
  955. //protected override void OnPaint(object sender, PaintEventArgs e)
  956. //{
  957. // DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw);
  958. //}
  959. //private void DrawTextItem(object date, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle)
  960. //{
  961. // object value = ResetTextValue(date, dataBoundField.IsDataBound);
  962. // if (value == null)
  963. // {
  964. // return;
  965. // }
  966. // if (PicColumn && value is Image)
  967. // {
  968. // Image image = value as Image;
  969. // if (image != null)
  970. // {
  971. // RectangleF rfp = PicMargin ? rectangle : rectangleM;
  972. // if (FixedRatio)
  973. // {
  974. // // 背景图片的高宽比例
  975. // float imgWHRate = (float)image.Width / (float)image.Height;
  976. // // 打印纸张的高宽比例
  977. // float paperWHRate = rfp.Width / rfp.Height;
  978. // // 背景图片打印范围
  979. // RectangleF newImgRectangleF = RectangleF.Empty;
  980. // // 缩放背景图片,但保持图片高宽比例
  981. // if (imgWHRate > paperWHRate)
  982. // {
  983. // newImgRectangleF.Width = rfp.Width;
  984. // newImgRectangleF.Height = rfp.Width / imgWHRate;
  985. // newImgRectangleF.X = rfp.X;
  986. // newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
  987. // }
  988. // else
  989. // {
  990. // newImgRectangleF.Width = rfp.Height * imgWHRate;
  991. // newImgRectangleF.Height = rfp.Height;
  992. // newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
  993. // newImgRectangleF.Y = rfp.Y;
  994. // }
  995. // rfp = newImgRectangleF;
  996. // }
  997. // // 在指定的范围绘制背景图片
  998. // LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
  999. // }
  1000. // return;
  1001. // }
  1002. // LayoutUtility.DrawText(graphics,
  1003. // GraphicsUnit.Millimeter,
  1004. // rectangleM,
  1005. // rectangle,
  1006. // value.ToString(),
  1007. // _fontDraw,
  1008. // _textColor,
  1009. // _lineSpaceDraw,
  1010. // _characterSpaceDraw,
  1011. // _characterCount,
  1012. // _textAlign,
  1013. // _textAlignVertical,
  1014. // _wrap,
  1015. // Clip,
  1016. // Selected);
  1017. //}
  1018. public void PrintTextItem(object date, Graphics graphics, RectangleF rectangleM, RectangleF rectangle)
  1019. {
  1020. object value = ResetTextValue(date, true);
  1021. if (value == null)
  1022. {
  1023. return;
  1024. }
  1025. //bool isPrint = true;
  1026. try
  1027. {
  1028. if (this._itemStyle == ItemStyle.Barcode)
  1029. {
  1030. // TODO 切换zxing生成69码图片时将EAN_13=》EAN13
  1031. if (this._barcodeFormat == "EAN_13")
  1032. {
  1033. string vv = EAN13((value + "").PadRight(12, '0'));
  1034. //Rectangle rr = new Rectangle();
  1035. //rr.X = LayoutCommon.MillimeterToPixelDpi(rectangleM.X, graphics.DpiX);
  1036. //rr.Y = LayoutCommon.MillimeterToPixelDpi(rectangleM.Y, graphics.DpiY);
  1037. //rr.Width = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1038. //rr.Height = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
  1039. //graphics.DrawString(vv, _font, Brushes.Black, rr);
  1040. graphics.DrawString(vv, _font, Brushes.Black, rectangleM);
  1041. return;
  1042. }
  1043. else
  1044. {
  1045. OneDDrawingOptions ops = new OneDDrawingOptions();
  1046. //ops.DrawGraphics = graphics;
  1047. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1048. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1049. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
  1050. ops.DpiX = graphics.DpiX;
  1051. ops.DpiY = graphics.DpiY;
  1052. if (this._barcodeFormat == "EAN_13")
  1053. {
  1054. ops.ShowText = true;
  1055. ops.TextFont = _font;
  1056. // zxing AlternateBitmapRenderer 创建图片没有设置分辨率
  1057. //ops.TextFont = new Font(_font.FontFamily, _font.Size * graphics.DpiX / 96, _font.Style);
  1058. //ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, 96);
  1059. //ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, 96);
  1060. //ops.DpiX = 96;
  1061. //ops.DpiY = 96;
  1062. //string vv = value + "6970322941388";
  1063. string vv = value + "";
  1064. int len = vv.Length;
  1065. if (len < 12)
  1066. {
  1067. vv = vv.PadRight(12, '0');
  1068. }
  1069. else if (len > 13)
  1070. {
  1071. vv = vv.Substring(0, 12);
  1072. }
  1073. value = vv;
  1074. }
  1075. try
  1076. {
  1077. //if (this.ItemCode == "ZWLJC")
  1078. //{
  1079. // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, OneDFormat.EAN_13))
  1080. // {
  1081. // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1082. // }
  1083. //}
  1084. //else
  1085. //{
  1086. // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops))
  1087. // {
  1088. // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1089. // }
  1090. //}
  1091. using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, GetOneDFormat(this._barcodeFormat)))
  1092. {
  1093. //pic1.Save("D://1.png");
  1094. graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1095. }
  1096. }
  1097. catch { }
  1098. return;
  1099. }
  1100. }
  1101. if (this._itemStyle == ItemStyle.QRCode)
  1102. {
  1103. QRCodeDrawingOptions ops = new QRCodeDrawingOptions();
  1104. //ops.DrawGraphics = graphics;
  1105. ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
  1106. //ops.ImageRect = new Rectangle(
  1107. // LayoutCommon.MillimeterToPixel(rectangleM.Left, graphics.DpiX),
  1108. // LayoutCommon.MillimeterToPixel(rectangleM.Top, graphics.DpiX),
  1109. // LayoutCommon.MillimeterToPixel(rectangleM.Width, graphics.DpiX),
  1110. // LayoutCommon.MillimeterToPixel(rectangleM.Height, graphics.DpiX));
  1111. //BarcodeDraw.DrawQRCodeImageOnGraphics(value.ToString(), imageRect, ops);
  1112. ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
  1113. ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY);
  1114. ops.DpiX = graphics.DpiX;
  1115. ops.DpiY = graphics.DpiY;
  1116. QRCodeEncodingOptions qre = new QRCodeEncodingOptions();
  1117. qre.ECLevel = GetQRCodeLevel(this.QRCodeLevel);
  1118. using (Image pic1 = QRCodeHelper.GetQRCodeImage(value.ToString(), ops, qre))
  1119. {
  1120. graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
  1121. }
  1122. return;
  1123. }
  1124. }
  1125. catch (Exception ex)
  1126. {
  1127. throw ex;
  1128. }
  1129. if (PicColumn && value is Image)
  1130. {
  1131. Image image = value as Image;
  1132. if (image != null)
  1133. {
  1134. RectangleF rfp = PicMargin ? rectangle : rectangleM;
  1135. if (FixedRatio)
  1136. {
  1137. // 背景图片的高宽比例
  1138. float imgWHRate = (float)image.Width / (float)image.Height;
  1139. // 打印纸张的高宽比例
  1140. float paperWHRate = rfp.Width / rfp.Height;
  1141. // 背景图片打印范围
  1142. RectangleF newImgRectangleF = RectangleF.Empty;
  1143. // 缩放背景图片,但保持图片高宽比例
  1144. if (imgWHRate > paperWHRate)
  1145. {
  1146. newImgRectangleF.Width = rfp.Width;
  1147. newImgRectangleF.Height = rfp.Width / imgWHRate;
  1148. newImgRectangleF.X = rfp.X;
  1149. newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
  1150. }
  1151. else
  1152. {
  1153. newImgRectangleF.Width = rfp.Height * imgWHRate;
  1154. newImgRectangleF.Height = rfp.Height;
  1155. newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
  1156. newImgRectangleF.Y = rfp.Y;
  1157. }
  1158. rfp = newImgRectangleF;
  1159. }
  1160. // 在指定的范围绘制背景图片
  1161. LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
  1162. }
  1163. return;
  1164. }
  1165. LayoutUtility.DrawText(graphics,
  1166. GraphicsUnit.Millimeter,
  1167. rectangleM,
  1168. rectangle,
  1169. value.ToString(),
  1170. _font,
  1171. _textColor,
  1172. _lineSpace,
  1173. _characterSpace,
  1174. _characterCount,
  1175. _textAlign,
  1176. _textAlignVertical,
  1177. _wrap,
  1178. Clip,
  1179. false);
  1180. }
  1181. #endregion 函数
  1182. public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
  1183. {
  1184. int width;//图片最终的宽
  1185. int height;//图片最终的高
  1186. try
  1187. {
  1188. System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
  1189. Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
  1190. Graphics g = Graphics.FromImage(targetPicture);
  1191. g.Clear(Color.White);
  1192. //计算缩放图片的大小
  1193. if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
  1194. {
  1195. width = targetWidth;
  1196. height = (width * sourceImage.Height) / sourceImage.Width;
  1197. }
  1198. else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
  1199. {
  1200. height = targetHeight;
  1201. width = (height * sourceImage.Width) / sourceImage.Height;
  1202. }
  1203. else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
  1204. {
  1205. width = sourceImage.Width;
  1206. height = sourceImage.Height;
  1207. }
  1208. else
  1209. {
  1210. width = targetWidth;
  1211. height = (width * sourceImage.Height) / sourceImage.Width;
  1212. if (height > targetHeight)
  1213. {
  1214. height = targetHeight;
  1215. width = (height * sourceImage.Width) / sourceImage.Height;
  1216. }
  1217. }
  1218. g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
  1219. sourceImage.Dispose();
  1220. return targetPicture;
  1221. }
  1222. catch// (Exception ex)
  1223. {
  1224. }
  1225. return null;
  1226. }
  1227. public OneDFormat GetOneDFormat(string value)
  1228. {
  1229. if (string.IsNullOrWhiteSpace(value))
  1230. {
  1231. return OneDFormat.CODE_128;
  1232. }
  1233. switch (value)
  1234. {
  1235. case "CODE_128":
  1236. return OneDFormat.CODE_128;
  1237. case "EAN_13":
  1238. return OneDFormat.EAN_13;
  1239. case "EAN_8":
  1240. return OneDFormat.EAN_8;
  1241. case "CODABAR":
  1242. return OneDFormat.CODABAR;
  1243. case "CODE_39":
  1244. return OneDFormat.CODE_39;
  1245. case "CODE_93":
  1246. return OneDFormat.CODE_93;
  1247. case "ITF":
  1248. return OneDFormat.ITF;
  1249. case "MSI":
  1250. return OneDFormat.MSI;
  1251. case "PLESSEY":
  1252. return OneDFormat.PLESSEY;
  1253. case "UPC_E":
  1254. return OneDFormat.UPC_E;
  1255. default:
  1256. return OneDFormat.CODE_128;
  1257. }
  1258. }
  1259. public QRECLevel GetQRCodeLevel(string value)
  1260. {
  1261. if (string.IsNullOrWhiteSpace(value))
  1262. {
  1263. return QRECLevel.H;
  1264. }
  1265. switch (value)
  1266. {
  1267. case "H":
  1268. return QRECLevel.H;
  1269. case "Q":
  1270. return QRECLevel.Q;
  1271. case "M":
  1272. return QRECLevel.M;
  1273. case "L":
  1274. return QRECLevel.L;
  1275. default:
  1276. return QRECLevel.H;
  1277. }
  1278. }
  1279. /// <summary>
  1280. /// 12位条码补齐校验位生成13位的条码
  1281. /// </summary>
  1282. /// <param name="chaine"></param>
  1283. /// <returns></returns>
  1284. public static string EAN13(string chaine)
  1285. {
  1286. int i;
  1287. int first;
  1288. int checkSum = 0;
  1289. string Barcode = "";
  1290. bool tableA;
  1291. //if (Regex.IsMatch(chaine, "^\\d{12}$"))
  1292. {
  1293. for (i = 1; i < 12; i += 2)
  1294. {
  1295. //System.Diagnostics.Debug.WriteLine(chaine.Substring(i, 1));
  1296. checkSum += Convert.ToInt32(chaine.Substring(i, 1));
  1297. }
  1298. checkSum *= 3;
  1299. for (i = 0; i < 12; i += 2)
  1300. {
  1301. checkSum += Convert.ToInt32(chaine.Substring(i, 1));
  1302. }
  1303. chaine += (10 - checkSum % 10) % 10;
  1304. string Barcode13Digits = chaine.ToString();
  1305. Barcode = chaine.Substring(0, 1) + (char)(65 + Convert.ToInt32(chaine.Substring(1, 1)));
  1306. first = Convert.ToInt32(chaine.Substring(0, 1));
  1307. for (i = 2; i <= 6; i++)
  1308. {
  1309. tableA = false;
  1310. switch (i)
  1311. {
  1312. case 2:
  1313. if (first >= 0 && first <= 3)
  1314. {
  1315. tableA = true;
  1316. }
  1317. break;
  1318. case 3:
  1319. if (first == 0 || first == 4 || first == 7 || first == 8)
  1320. {
  1321. tableA = true;
  1322. }
  1323. break;
  1324. case 4:
  1325. if (first == 0 || first == 1 || first == 4 || first == 5 || first == 9)
  1326. {
  1327. tableA = true;
  1328. }
  1329. break;
  1330. case 5:
  1331. if (first == 0 || first == 2 || first == 5 || first == 6 || first == 7)
  1332. {
  1333. tableA = true;
  1334. }
  1335. break;
  1336. case 6:
  1337. if (first == 0 || first == 3 || first == 6 || first == 8 || first == 9)
  1338. {
  1339. tableA = true;
  1340. }
  1341. break;
  1342. }
  1343. if (tableA)
  1344. {
  1345. Barcode += (char)(65 + Convert.ToInt32(chaine.Substring(i, 1)));
  1346. }
  1347. else
  1348. {
  1349. Barcode += (char)(75 + Convert.ToInt32(chaine.Substring(i, 1)));
  1350. }
  1351. }
  1352. Barcode += "*";
  1353. for (i = 7; i <= 12; i++)
  1354. {
  1355. Barcode += (char)(97 + Convert.ToInt32(chaine.Substring(i, 1)));
  1356. }
  1357. Barcode += "+";
  1358. }
  1359. return Barcode;
  1360. }
  1361. }
  1362. public class QRCodeWriterMy : Writer
  1363. {
  1364. private const int QUIET_ZONE_SIZE = 4;
  1365. /// <summary>
  1366. /// Encode a barcode using the default settings.
  1367. /// </summary>
  1368. /// <param name="contents">The contents to encode in the barcode</param>
  1369. /// <param name="format">The barcode format to generate</param>
  1370. /// <param name="width">The preferred width in pixels</param>
  1371. /// <param name="height">The preferred height in pixels</param>
  1372. /// <returns>
  1373. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1374. /// </returns>
  1375. public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
  1376. {
  1377. return encode(contents, format, width, height, null);
  1378. }
  1379. /// <summary>
  1380. /// </summary>
  1381. /// <param name="contents">The contents to encode in the barcode</param>
  1382. /// <param name="format">The barcode format to generate</param>
  1383. /// <param name="width">The preferred width in pixels</param>
  1384. /// <param name="height">The preferred height in pixels</param>
  1385. /// <param name="hints">Additional parameters to supply to the encoder</param>
  1386. /// <returns>
  1387. /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
  1388. /// </returns>
  1389. public BitMatrix encode(string contents,
  1390. BarcodeFormat format,
  1391. int width,
  1392. int height,
  1393. IDictionary<EncodeHintType, object> hints)
  1394. {
  1395. if (String.IsNullOrEmpty(contents))
  1396. {
  1397. throw new ArgumentException("Found empty contents");
  1398. }
  1399. if (format != BarcodeFormat.QR_CODE)
  1400. {
  1401. throw new ArgumentException("Can only encode QR_CODE, but got " + format);
  1402. }
  1403. if (width < 0 || height < 0)
  1404. {
  1405. throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
  1406. }
  1407. var errorCorrectionLevel = ErrorCorrectionLevel.L;
  1408. int quietZone = QUIET_ZONE_SIZE;
  1409. if (hints != null)
  1410. {
  1411. if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
  1412. {
  1413. var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
  1414. if (requestedECLevel != null)
  1415. {
  1416. errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
  1417. if (errorCorrectionLevel == null)
  1418. {
  1419. switch (requestedECLevel.ToString().ToUpper())
  1420. {
  1421. case "L":
  1422. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1423. break;
  1424. case "M":
  1425. errorCorrectionLevel = ErrorCorrectionLevel.M;
  1426. break;
  1427. case "Q":
  1428. errorCorrectionLevel = ErrorCorrectionLevel.Q;
  1429. break;
  1430. case "H":
  1431. errorCorrectionLevel = ErrorCorrectionLevel.H;
  1432. break;
  1433. default:
  1434. errorCorrectionLevel = ErrorCorrectionLevel.L;
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. }
  1440. if (hints.ContainsKey(EncodeHintType.MARGIN))
  1441. {
  1442. var quietZoneInt = hints[EncodeHintType.MARGIN];
  1443. if (quietZoneInt != null)
  1444. {
  1445. quietZone = Convert.ToInt32(quietZoneInt.ToString());
  1446. }
  1447. }
  1448. }
  1449. var code = Encoder.encode(contents, errorCorrectionLevel, hints);
  1450. BitMatrix bm = renderResult(code, width, height, quietZone);
  1451. hints[EncodeHintType.WIDTH] = bm.Width;
  1452. hints[EncodeHintType.HEIGHT] = bm.Height;
  1453. return bm;
  1454. }
  1455. // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
  1456. // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  1457. private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
  1458. {
  1459. var input = code.Matrix;
  1460. if (input == null)
  1461. {
  1462. throw new InvalidOperationException();
  1463. }
  1464. int inputWidth = input.Width;
  1465. int inputHeight = input.Height;
  1466. int qrWidth = inputWidth + (quietZone << 1);
  1467. int qrHeight = inputHeight + (quietZone << 1);
  1468. int outputWidth = Math.Max(width, qrWidth);
  1469. int outputHeight = Math.Max(height, qrHeight);
  1470. int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
  1471. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  1472. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  1473. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  1474. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  1475. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  1476. int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  1477. //int leftPadding = 0;//(outputWidth - (inputWidth * multiple)) / 2;
  1478. //int topPadding = 0;//(outputHeight - (inputHeight * multiple)) / 2;
  1479. //outputWidth = (inputWidth * multiple);
  1480. //outputHeight = (inputHeight * multiple);
  1481. var output = new BitMatrix(outputWidth, outputHeight);
  1482. for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple)
  1483. {
  1484. // Write the contents of this row of the barcode
  1485. for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
  1486. {
  1487. if (input[inputX, inputY] == 1)
  1488. {
  1489. output.setRegion(outputX, outputY, multiple, multiple);
  1490. //int mX = outputWidth - outputX;
  1491. //int mY = outputHeight - outputY;
  1492. //mX = Math.Min(mX, multiple);
  1493. //mY = Math.Min(mY, multiple);
  1494. //output.setRegion(outputX, outputY, mX, mY);
  1495. }
  1496. }
  1497. }
  1498. return output;
  1499. }
  1500. }
  1501. }