TextItem.cs 52 KB

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