TextItem.cs 62 KB

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