TextItem.cs 41 KB

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