| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750 |
-
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.Windows.Forms;
- using Curtain.Framework.Barcode;
- using Curtain.Framework.Barcode.OneD;
- using Curtain.Framework.Barcode.QRCode;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode.Internal;
- namespace Dongke.WinForm.Controls.InvoiceLayout
- {
- /// <summary>
- ///
- /// </summary>
- [Serializable]
- public class TextItem : LayoutItem, IBoundItem
- {
- #region 成员变量
- private string _dataMember; // 对应数据源中的字段名
- private string _displayValue; // 编辑模式显示的值
- private object _formatType; // 格式类别
- private string _format; // 文本格式
- private string _barcodeFormat; // 文本格式
- private Font _font; // 文本字体
- private bool _isUnderline; // 下划线
- private bool _isStrikeout; // 删除线
- private bool _wrap; // 是否自动换行
- private bool _clip; // 是否剪裁延伸到边框外侧的文本
- private Color _textColor; // 文本颜色
- private TextAlignment _textAlign; // 文本对齐方式
- private TextAlignment _textAlignVertical; // 文本垂直对齐
- private float _characterSpace; // 文字间隔
- private float _lineSpace; // 文字行间隔
- private int _characterCount; // 行文字数
- private string _defaultValue; // 文本默认值
- private string _culture; // culture
- private string _defaultItemName; // 默认项目名
- private string _textItemName; // 文本项目名
- private bool _isDataBinding; // 是否数据绑定
- [NonSerialized]
- private string _text; // 文本
- [NonSerialized]
- private object _dataValue; // 数据源中的值
- [NonSerialized]
- private object _printDataValue; // 打印数据源中的值
- [NonSerialized]
- private DataBoundField _boundField; // 数据源字段
- [NonSerialized]
- private DataBoundField _printBoundField; // 打印数据源字段
- [NonSerialized]
- private Font _fontDraw; // 绘制字体
- [NonSerialized]
- private float _lineSpaceDraw; // 绘制行间隔
- [NonSerialized]
- private float _characterSpaceDraw; // 绘制文字间隔
- /// <summary>
- ///
- /// </summary>
- [NonSerialized]
- protected RectangleF _marginRectangleFDraw; // 绘制范围
- private string _itemCode; // 项目ID
- private int _itemRefClass; // 分类
- private ItemStyle _itemStyle; // 项目类别
- private string _itemSample; // 项目示例
- /// <summary>
- /// 条码打印用
- /// </summary>
- [NonSerialized]
- private string _picCodeValue = null;
- [NonSerialized]
- private int _picCodeWidth = -1;
- [NonSerialized]
- private int _picCodeHeight = -1;
- [NonSerialized]
- private Image _picCode = null;
-
- //[NonSerialized]
- //BarcodeWriter _writer = null;
- //[NonSerialized]
- //EncodingOptions _options = null;
- #endregion 成员变量
- #region 属性
- /// <summary>
- /// 对应数据源中的字段名
- /// </summary>
- public string DataMember
- {
- get
- {
- return _dataMember;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_dataMember))
- {
- _dataMember = value;
- if (Owner != null)
- {
- if (Owner.LayoutMode == LayoutMode.Preview)
- {
- Owner.RefreshTextItemBound(this);
- }
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 编辑模式显示的值
- /// </summary>
- public string DisplayValue
- {
- get
- {
- return _displayValue;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_displayValue))
- {
- _displayValue = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 格式类别
- /// </summary>
- public object FormatType
- {
- get
- {
- return _formatType;
- }
- set
- {
- _formatType = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- /// <summary>
- /// 文本格式
- /// </summary>
- public string Format
- {
- get
- {
- return _format;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_format))
- {
- _format = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 一维码格式
- /// </summary>
- public string BarcodeFormat
- {
- get
- {
- return _barcodeFormat;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_barcodeFormat))
- {
- _barcodeFormat = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本字体
- /// </summary>
- public Font Font
- {
- get
- {
- return _font;
- }
- set
- {
- if (!_font.Equals(value))
- {
- _font = value;
- _isUnderline = _font.Underline;
- _isStrikeout = _font.Strikeout;
- float zoom = base.GetZoom();
- _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 是否自动文本换行
- /// </summary>
- public bool Wrap
- {
- get
- {
- return _wrap;
- }
- set
- {
- if (_wrap != value)
- {
- _wrap = value;
- _clip = !_wrap;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 是否剪裁延伸到边框外侧的文本
- /// </summary>
- internal bool Clip
- {
- get
- {
- _clip = !_wrap;
- return _clip;
- }
- }
- /// <summary>
- /// 文本颜色
- /// </summary>
- public Color TextColor
- {
- get
- {
- return _textColor;
- }
- set
- {
- if (!_textColor.Equals(value))
- {
- _textColor = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本对齐方式
- /// </summary>
- public TextAlignment TextAlign
- {
- get
- {
- return _textAlign;
- }
- set
- {
- if (_textAlign != value)
- {
- _textAlign = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本垂直对齐方式
- /// </summary>
- public TextAlignment TextAlignVertical
- {
- get
- {
- return _textAlignVertical;
- }
- set
- {
- if (_textAlignVertical != value)
- {
- _textAlignVertical = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文字间隔
- /// </summary>
- public float CharacterSpace
- {
- get
- {
- return _characterSpace;
- }
- set
- {
- if (_characterSpace != value)
- {
- _characterSpace = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文字行间隔
- /// </summary>
- public float LineSpace
- {
- get
- {
- return _lineSpace;
- }
- set
- {
- if (_lineSpace != value)
- {
- _lineSpace = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 行文字数
- /// </summary>
- public int CharacterCount
- {
- get
- {
- return _characterCount;
- }
- set
- {
- if (_characterCount != value)
- {
- _characterCount = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本默认值
- /// </summary>
- public string DefaultValue
- {
- get
- {
- return _defaultValue;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_defaultValue))
- {
- _defaultValue = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// culture
- /// </summary>
- public string Culture
- {
- get
- {
- return _culture;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_culture))
- {
- _culture = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 文本项目名
- /// </summary>
- public string TextItemName
- {
- get
- {
- return _textItemName;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_textItemName))
- {
- _textItemName = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 默认项目名
- /// </summary>
- public string DefaultItemName
- {
- get
- {
- return _defaultItemName;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_defaultItemName))
- {
- _defaultItemName = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 数据源中的值
- /// </summary>
- internal object DataValue
- {
- get
- {
- return _dataValue;
- }
- set
- {
- if (value != _dataValue
- || (value != null && !value.Equals(_dataValue)))
- {
- _dataValue = value;
- }
- }
- }
- /// <summary>
- /// 打印数据源中的值
- /// </summary>
- internal object PrintDataValue
- {
- get
- {
- return _printDataValue;
- }
- set
- {
- if (value != _printDataValue
- || (value != null && !value.Equals(_printDataValue)))
- {
- _printDataValue = value;
- }
- }
- }
- /// <summary>
- /// 是否数据绑定
- /// </summary>
- internal bool IsDataBinding
- {
- get
- {
- return _isDataBinding;
- }
- set
- {
- _isDataBinding = value;
- }
- }
- /// <summary>
- /// 数据源字段
- /// </summary>
- internal DataBoundField BoundField
- {
- get
- {
- return _boundField;
- }
- set
- {
- _boundField = value;
- }
- }
- /// <summary>
- /// 打印数据源字段
- /// </summary>
- internal DataBoundField PrintBoundField
- {
- get
- {
- return _printBoundField;
- }
- set
- {
- _printBoundField = value;
- }
- }
- /// <summary>
- /// 绘制字体
- /// </summary>
- internal Font FontDraw
- {
- get
- {
- return _fontDraw;
- }
- }
- /// <summary>
- /// 绘制文字间隔
- /// </summary>
- internal float CharacterSpaceDraw
- {
- get
- {
- return _characterSpaceDraw;
- }
- }
- /// <summary>
- /// 绘制行间隔
- /// </summary>
- internal float LineSpaceDraw
- {
- get
- {
- return _lineSpaceDraw;
- }
- }
- /// <summary>
- /// 项目编码
- /// </summary>
- public string ItemCode
- {
- get
- {
- return this._itemCode;
- }
- set
- {
- if (this._itemCode != value)
- {
- this._itemCode = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- if (string.IsNullOrEmpty(this._itemCode))
- {
- this._isDataBinding = false;
- }
- else
- {
- this._isDataBinding = true;
- }
- }
- }
- /// <summary>
- /// 分类
- /// </summary>
- public int ItemRefClass
- {
- get
- {
- return _itemRefClass;
- }
- set
- {
- if (value != _itemRefClass)
- {
- _itemRefClass = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 项目类别
- /// </summary>
- public ItemStyle ItemStyle
- {
- get
- {
- return _itemStyle;
- }
- set
- {
- if (value != _itemStyle)
- {
- _itemStyle = value;
- this._picCode = null;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- /// 项目示例
- /// </summary>
- public string ItemSample
- {
- get
- {
- return _itemSample;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_itemSample))
- {
- _itemSample = value;
- if (Owner != null)
- {
- Owner.SetItemChangedArgs(this, ItemChangeType.Modified);
- }
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- public bool PicColumn
- {
- get;
- set;
- }
- /// <summary>
- ///
- /// </summary>
- public bool PicMargin
- {
- get;
- set;
- }
- /// <summary>
- ///
- /// </summary>
- public bool FixedRatio
- {
- get;
- set;
- }
- #endregion 属性
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem()
- : this(null)
- {
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem(LayoutBox box)
- : this(box, ItemType.Text)
- {
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- internal TextItem(LayoutBox box, ItemType itemType)
- : base(box, itemType)
- {
- _width = LayoutCommon.DefaultTextItemSizeWidth;
- _height = LayoutCommon.DefaultTextItemSizeHeight;
- _displayValue = string.Empty;
- _format = string.Empty;
- _defaultValue = string.Empty;
- _font = LayoutCommon.DefaultItemFont;
- _fontDraw = LayoutCommon.DefaultItemFont;
- _textColor = Color.Black;
- _textAlign = TextAlignment.LeftOrTop;
- _characterSpace = 0f;
- _lineSpace = 0f;
- _characterCount = 0;
- _dataValue = null;
- _text = string.Empty;
- _itemRefClass = 0;
- _itemStyle = ItemStyle.Other;
- _isDataBinding = false;
- _boundField = new DataBoundField();
- _boundField.IsDataBound = false;
- _boundField.BoundFieldIndex = -1;
- _printBoundField = new DataBoundField();
- _printBoundField.IsDataBound = false;
- _printBoundField.BoundFieldIndex = -1;
- _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
- _textItemName = _defaultItemName;
- }
- #endregion 构造函数
- #region 函数
- /// <summary>
- /// 格式化数据源中的值
- /// </summary>
- private string FormatDataValue(object value)
- {
- string result = null;
- if (value != null && value != DBNull.Value)
- {
- if (_itemStyle == ItemStyle.Date)
- {
- result = value.ToString();
- DateTime dateTime;
- if (DateTime.TryParse(result, out dateTime))
- {
- if (string.IsNullOrEmpty(_format))
- {
- result = dateTime.ToString(LayoutConsts.DATETIME_FRORMAT);
- }
- else
- {
- if (LayoutConsts.CULTUREINFO_JP.Equals(_culture))
- {
- result = dateTime.ToString(_format, LayoutCommon.CultureInfoJP);
- }
- else
- {
- // MMM
- if (_format.Contains(LayoutConsts.DATETIME_FRORMAT_MMM))
- {
- if (string.IsNullOrEmpty(_culture))
- {
- result = dateTime.ToString(_format).ToUpper();
- }
- else
- {
- result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture)).ToUpper();
- }
- }
- else if (string.IsNullOrEmpty(_culture))
- {
- result = dateTime.ToString(_format);
- }
- else
- {
- result = dateTime.ToString(_format, CultureInfo.CreateSpecificCulture(_culture));
- }
- }
- }
- }
- }
- else if (_itemStyle == ItemStyle.Sign)
- {
- if (string.IsNullOrEmpty(_format))
- {
- result = value.ToString();
- }
- else
- {
- result = _format;
- }
- }
- else
- {
- result = value.ToString();
- }
- }
- return result;
- }
- /// <summary>
- /// 文本内容设置
- /// </summary>
- internal object ResetTextValue(object value, bool isDataBound)
- {
- if (Owner == null)
- {
- return _displayValue;
- }
- if (Owner.LayoutMode == LayoutMode.Preview)
- {
- if (!string.IsNullOrEmpty(_dataMember))
- {
- if (isDataBound)
- {
- object result = Owner.SetItemValueFormattingArgs(this, value, _format);
- if (this.ItemStyle == ItemStyle.LinearGraph || PicColumn)
- {
- if (result != null && result is Image)
- {
- return result;
- }
- if (value != null && value is byte[])
- {
- System.IO.MemoryStream ms = new System.IO.MemoryStream(value as byte[]);
- return Image.FromStream(ms);
- }
- return value;
- }
- if (result != null)
- {
- return result.ToString();
- }
- if (value != null && value != DBNull.Value)
- {
- return FormatDataValue(value);
- }
- return _defaultValue;
- }
- //return _defaultValue;
- return "";
- }
- return _displayValue;
- }
- else
- {
- //object result = Owner.SetItemValueFormattingArgs(this, value, _format);
- object result = Owner.SetItemValueFormattingArgs(this, _displayValue, _format);
- if (result != null)
- {
- return result.ToString();
- }
- return _displayValue;
- }
- }
- /// <summary>
- /// 显示Item属性设置画面
- /// </summary>
- /// <returns>
- /// DialogResult.OK:选中的Item有效,设置成功<br/>
- /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
- /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
- /// </returns>
- protected override DialogResult ShowItemPropertyDialogInner()
- {
- //if (this.PicColumn)
- if (this.ItemStyle == ItemStyle.LinearGraph)
- {
- using (ImageItemSetting itemPropertySetting = new ImageItemSetting())
- {
- itemPropertySetting.ImageLocationX = Left;
- itemPropertySetting.ImageLocationY = Top;
- itemPropertySetting.ImageWidth = Width;
- itemPropertySetting.ImageHeight = Height;
- itemPropertySetting.SideRatioFixed = SideRatioFixed;
- //itemPropertySetting.FileName = FileName;
- //itemPropertySetting.Image = Image;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- Left = itemPropertySetting.ImageLocationX;
- Top = itemPropertySetting.ImageLocationY;
- Width = itemPropertySetting.ImageWidth;
- Height = itemPropertySetting.ImageHeight;
- SideRatioFixed = itemPropertySetting.SideRatioFixed;
- ResetItemFrame();
- SetDrawProperty();
- Owner.Dirty = true;
- }
- return result;
- }
- }
- using (TextItemSetting itemPropertySetting = new TextItemSetting())
- {
- itemPropertySetting.IsNew = false;
- itemPropertySetting.IsDataBinding = IsDataBinding;
- itemPropertySetting.TextLocationX = Left;
- itemPropertySetting.TextLocationY = Top;
- itemPropertySetting.TextWidth = Width;
- itemPropertySetting.TextHeight = Height;
- itemPropertySetting.Culture = Culture;
- itemPropertySetting.LineSpace = LineSpace;
- itemPropertySetting.CharacterSpace = CharacterSpace;
- itemPropertySetting.CharacterCount = CharacterCount;
- itemPropertySetting.TextAlign = TextAlign;
- itemPropertySetting.TextAlignVertical = TextAlignVertical;
- itemPropertySetting.TextPreview = DisplayValue;
- itemPropertySetting.TextColor = TextColor;
- itemPropertySetting.ItemStyle = ItemStyle;
- itemPropertySetting.ItemName = TextItemName;
- itemPropertySetting.TextFormat = Format;
- itemPropertySetting.BarcodeFormat = BarcodeFormat;
- itemPropertySetting.TextFont = Font;
- itemPropertySetting.Wrap = Wrap;
- itemPropertySetting.DefaultValue = DefaultValue;
- itemPropertySetting.chkFixedRatio.Checked = FixedRatio;
- itemPropertySetting.chkM.Checked = PicMargin;
- itemPropertySetting.chkPic.Checked = PicColumn;
- itemPropertySetting.chkFixedRatio.Enabled = PicColumn;
- itemPropertySetting.chkM.Enabled = PicColumn;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- Left = itemPropertySetting.TextLocationX;
- Top = itemPropertySetting.TextLocationY;
- Width = itemPropertySetting.TextWidth;
- Height = itemPropertySetting.TextHeight;
- LineSpace = itemPropertySetting.LineSpace;
- CharacterSpace = itemPropertySetting.CharacterSpace;
- CharacterCount = itemPropertySetting.CharacterCount;
- Font = itemPropertySetting.TextFont;
- Wrap = itemPropertySetting.Wrap;
- TextAlign = itemPropertySetting.TextAlign;
- TextAlignVertical = itemPropertySetting.TextAlignVertical;
- TextColor = itemPropertySetting.TextColor;
- DisplayValue = itemPropertySetting.TextPreview;
- ItemStyle = itemPropertySetting.ItemStyle;
- TextItemName = itemPropertySetting.ItemName;
- Format = itemPropertySetting.TextFormat;
- BarcodeFormat = itemPropertySetting.BarcodeFormat;
- Culture = itemPropertySetting.Culture;
- DefaultValue = itemPropertySetting.DefaultValue;
- FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
- PicMargin = itemPropertySetting.chkM.Checked;
- PicColumn = itemPropertySetting.chkPic.Checked;
- ResetItemFrame();
- SetDrawProperty();
- _picCode = null;
- Owner.Dirty = true;
- }
- return result;
- }
- }
- /// <summary>
- /// Item初始化
- /// </summary>
- /// <param name="box">LayoutBox</param>
- /// <param name="isNewID">是否生成新ID</param>
- /// <returns>是否成功</returns>
- internal override bool Init(LayoutBox box, bool isNewID)
- {
- if (_fontDraw == null)
- {
- if (_font == null)
- {
- _font = LayoutCommon.DefaultItemFont;
- _fontDraw = LayoutCommon.DefaultItemFont;
- }
- else
- {
- _fontDraw = new Font(_font.FontFamily, _font.Size, _font.Style & (FontStyle.Regular | FontStyle.Bold | FontStyle.Italic));
- }
- }
- _boundField.IsDataBound = false;
- _boundField.BoundFieldIndex = -1;
- _printBoundField.IsDataBound = false;
- _printBoundField.BoundFieldIndex = -1;
- bool isInit = base.Init(box, isNewID);
- if (isNewID)
- {
- _defaultItemName = LayoutBox.ITEMNAME_FIX + ID;
- _textItemName = _defaultItemName;
- }
- return isInit;
- }
- /// <summary>
- /// 初始化Item绘制用属性
- /// </summary>
- internal override float SetDrawProperty()
- {
- float zoom = base.SetDrawProperty();
- _fontDraw = new Font(_font.FontFamily, _font.Size * zoom, _fontDraw.Style);
- _lineSpaceDraw = _lineSpace * zoom;
- if (_characterCount < 1)
- {
- _characterSpaceDraw = _characterSpace * zoom;
- }
- _marginRectangleFDraw = new RectangleF();
- _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- return zoom;
- }
- /// <summary>
- /// 初始化Item绘制用属性
- /// </summary>
- internal override float SetRectangleF()
- {
- float zoom = base.SetRectangleF();
- if (_characterCount < 1)
- {
- _characterSpaceDraw = _characterSpace * zoom;
- }
- _marginRectangleFDraw = new RectangleF();
- _marginRectangleFDraw.X = (Left + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Y = (Top + LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Width = (Width - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- _marginRectangleFDraw.Height = (Height - LayoutConsts.TEXT_MARGIN - LayoutConsts.TEXT_MARGIN) * zoom;
- return zoom;
- }
- /// <summary>
- /// Item图形上的OnPaint事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- protected override void OnPaint(object sender, PaintEventArgs e)
- {
- DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw, false);
- }
- internal void DrawTextItem(object data, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle, bool isPrint)
- {
- object value = ResetTextValue(data, dataBoundField.IsDataBound);
- if (value == null)
- {
- return;
- }
- try
- {
- if (this._itemStyle == ItemStyle.Barcode)
- {
- if (isPrint)
- {
- OneDDrawingOptions ops = new OneDDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- //ops.ImageWidth = this.SimpleShape.Width;
- //ops.ImageHeight = this.SimpleShape.Height;
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- //string vv = value + "";
- //if (vv.StartsWith("69"))
- try
- {
- //if (this.ItemCode == "ZWLJC")
- //{
- // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, OneDFormat.EAN_13))
- // {
- // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- // }
- //}
- //else
- //{
- // using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops))
- // {
- // graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- // }
- //}
- using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops, GetOneDFormat(this._barcodeFormat)))
- {
- graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- }
- }
- catch
- {
- }
- return;
- }
- Image pic = null;
- if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
- this.SimpleShape.Height == this._picCodeHeight &&
- value.ToString() == _picCodeValue)
- {
- pic = this._picCode;
- }
- if (pic == null)
- {
- OneDDrawingOptions ops = new OneDDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- //ops.ImageWidth = this.SimpleShape.Width;
- //ops.ImageHeight = this.SimpleShape.Height;
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- //string vv = value + "";
- //if (vv.StartsWith("69"))
- try
- {
- //if (this.ItemCode == "ZWLJC")
- //{
- // pic = OneDHelper.GetOneDImage(value.ToString(), ops, OneDFormat.EAN_13);
- //}
- //else
- //{
- // pic = OneDHelper.GetOneDImage(value.ToString(), ops);
- //}
- pic = OneDHelper.GetOneDImage(value.ToString(), ops, GetOneDFormat(this._barcodeFormat));
- }
- catch
- {
- pic = null;
- }
- if (!isPrint)
- {
- try
- {
- this._picCode?.Dispose();
- }
- catch
- { }
- this._picCode = pic;
- _picCodeValue = value.ToString();
- _picCodeWidth = this.SimpleShape.Width;
- _picCodeHeight = this.SimpleShape.Height;
- }
- }
- try
- {
- if (pic != null)
- {
- //graphics.DrawImage(pic, this.SimpleShape.Bounds, new Rectangle(0, 0, pic.Width, pic.Height), GraphicsUnit.Pixel);
- graphics.DrawImage(pic, this.SimpleShape.Bounds);
- }
- }
- catch
- {
- }
- finally
- {
- if (pic != null && isPrint)
- {
- pic.Dispose();
- }
- }
- return;
- }
- if (this._itemStyle == ItemStyle.QRCode)
- {
- if (isPrint)
- {
- QRCodeDrawingOptions ops = new QRCodeDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- //ops.ImageWidth = this.SimpleShape.Width;
- //ops.ImageHeight = this.SimpleShape.Height;
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- QRCodeEncodingOptions qre = new QRCodeEncodingOptions();
- qre.ECLevel = QRECLevel.Q;
- using (Image pic1 = QRCodeHelper.GetQRCodeImage(value.ToString(), ops, qre))
- {
- graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- }
- return;
- }
- Image pic = null;
- if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
- this.SimpleShape.Height == this._picCodeHeight &&
- value.ToString() == _picCodeValue)
- {
- pic = this._picCode;
- }
- if (pic == null)
- {
- QRCodeDrawingOptions ops = new QRCodeDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- //ops.ImageWidth = this.SimpleShape.Width;
- //ops.ImageHeight = this.SimpleShape.Height;
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- QRCodeEncodingOptions qre = new QRCodeEncodingOptions();
- qre.ECLevel = QRECLevel.Q;
- pic = QRCodeHelper.GetQRCodeImage(value.ToString(), ops, qre);
- //pic.Save("C://1.png");
- if (!isPrint)
- {
- try
- {
- this._picCode?.Dispose();
- }
- catch
- {
- }
- this._picCode = pic;
- _picCodeValue = value.ToString();
- _picCodeWidth = this.SimpleShape.Width;
- _picCodeHeight = this.SimpleShape.Height;
- }
- }
- try
- {
- if (pic != null)
- {
- //graphics.DrawImage(pic, this.SimpleShape.Bounds, new Rectangle(0, 0, pic.Width, pic.Height), GraphicsUnit.Pixel);
- graphics.DrawImage(pic, this.SimpleShape.Bounds);
- }
- }
- catch
- {
- }
- finally
- {
- if (pic != null && isPrint)
- {
- pic.Dispose();
- }
- }
- return;
- }
- //xuwei add 2024-04-12
- /*
- if (this._itemStyle == ItemStyle.LinearGraph)
- {
- if (isPrint)
- {
- OneDDrawingOptions ops = new OneDDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- //ops.ImageWidth = this.SimpleShape.Width;
- //ops.ImageHeight = this.SimpleShape.Height;
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- //xuwei to do ===========================
- using (Image pic1 = Image.FromFile(value.ToString()))
- {
- graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel);
- }
- //xuwei end
- return;
- }
- Image pic = null;
- if (!isPrint && this.SimpleShape.Width == this._picCodeWidth &&
- this.SimpleShape.Height == this._picCodeHeight &&
- value.ToString() == _picCodeValue)
- {
- pic = this._picCode;
- }
- if (pic == null)
- {
- OneDDrawingOptions ops = new OneDDrawingOptions();
- //ops.DrawGraphics = graphics;
- ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show);
- //ops.ImageWidth = this.SimpleShape.Width;
- //ops.ImageHeight = this.SimpleShape.Height;
- ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX);
- ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiX);
- ops.DpiX = graphics.DpiX;
- ops.DpiY = graphics.DpiY;
- //pic = OneDHelper.GetOneDImage(value.ToString(), ops);
- pic = Image.FromFile(value.ToString());
- if (!isPrint)
- {
- try
- {
- this._picCode?.Dispose();
- }
- catch
- { }
- this._picCode = pic;
- _picCodeValue = value.ToString();
- _picCodeWidth = this.SimpleShape.Width;
- _picCodeHeight = this.SimpleShape.Height;
- }
- }
- try
- {
- if (pic != null)
- {
- //graphics.DrawImage(pic, this.SimpleShape.Bounds, new Rectangle(0, 0, pic.Width, pic.Height), GraphicsUnit.Pixel);
- graphics.DrawImage(pic, this.SimpleShape.Bounds);
- }
- }
- catch
- {
- }
- finally
- {
- if (pic != null && isPrint)
- {
- pic.Dispose();
- }
- }
- return;
- }
- */
- //xuwei end
- if (this._itemStyle == ItemStyle.LinearGraph)
- {
- if (value is null)
- {
- return;
- }
- System.Drawing.Image image = null;
- if (value is string)
- {
- string ff = value + "";
- if (ff == "" || !File.Exists(ff))
- {
- return;
- }
- FileInfo fileInfo = new FileInfo(ff);
- Stream imageStream = null;
- try
- {
- imageStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
- image = System.Drawing.Image.FromStream(imageStream);
- MemoryStream mstream = new MemoryStream();
- image.Save(mstream, image.RawFormat);
- image = System.Drawing.Image.FromStream(mstream);
- }
- finally
- {
- if (imageStream != null)
- {
- imageStream.Dispose();
- }
- }
- }
- if (value is System.Drawing.Image)
- {
- image = value as System.Drawing.Image;
- }
- if (image != null)
- {
- RectangleF rfp = PicMargin ? rectangle : rectangleM;
- if (FixedRatio)
- {
- // 背景图片的高宽比例
- float imgWHRate = (float)image.Width / (float)image.Height;
- // 打印纸张的高宽比例
- float paperWHRate = rfp.Width / rfp.Height;
- // 背景图片打印范围
- RectangleF newImgRectangleF = RectangleF.Empty;
- // 缩放背景图片,但保持图片高宽比例
- if (imgWHRate > paperWHRate)
- {
- newImgRectangleF.Width = rfp.Width;
- newImgRectangleF.Height = rfp.Width / imgWHRate;
- newImgRectangleF.X = rfp.X;
- newImgRectangleF.Y = (rfp.Height - newImgRectangleF.Height) / 2 + rfp.Y;
- }
- else
- {
- newImgRectangleF.Width = rfp.Height * imgWHRate;
- newImgRectangleF.Height = rfp.Height;
- newImgRectangleF.X = (rfp.Width - newImgRectangleF.Width) / 2 + rfp.X;
- newImgRectangleF.Y = rfp.Y;
- }
- rfp = newImgRectangleF;
- }
- // 在指定的范围绘制背景图片
- LayoutUtility.DrawImage(graphics, GraphicsUnit.Millimeter, rfp, image);
- }
- return;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- LayoutUtility.DrawText(graphics,
- GraphicsUnit.Millimeter,
- rectangleM,
- rectangle,
- value.ToString(),
- _fontDraw,
- _textColor,
- _lineSpaceDraw,
- _characterSpaceDraw,
- _characterCount,
- _textAlign,
- _textAlignVertical,
- _wrap,
- Clip,
- Selected);
- }
- #endregion 函数
- public Image pictureProcess(Image sourceImage, int targetWidth, int targetHeight)
- {
- int width;//图片最终的宽
- int height;//图片最终的高
- try
- {
- System.Drawing.Imaging.ImageFormat format = sourceImage.RawFormat;
- Bitmap targetPicture = new Bitmap(targetWidth, targetHeight);
- Graphics g = Graphics.FromImage(targetPicture);
- g.Clear(Color.White);
- //计算缩放图片的大小
- if (sourceImage.Width > targetWidth && sourceImage.Height <= targetHeight)
- {
- width = targetWidth;
- height = (width * sourceImage.Height) / sourceImage.Width;
- }
- else if (sourceImage.Width <= targetWidth && sourceImage.Height > targetHeight)
- {
- height = targetHeight;
- width = (height * sourceImage.Width) / sourceImage.Height;
- }
- else if (sourceImage.Width <= targetWidth && sourceImage.Height <= targetHeight)
- {
- width = sourceImage.Width;
- height = sourceImage.Height;
- }
- else
- {
- width = targetWidth;
- height = (width * sourceImage.Height) / sourceImage.Width;
- if (height > targetHeight)
- {
- height = targetHeight;
- width = (height * sourceImage.Width) / sourceImage.Height;
- }
- }
- g.DrawImage(sourceImage, (targetWidth - width) / 2, (targetHeight - height) / 2, width, height);
- sourceImage.Dispose();
- return targetPicture;
- }
- catch// (Exception ex)
- {
- }
- return null;
- }
- public OneDFormat GetOneDFormat(string value)
- {
- if (string.IsNullOrWhiteSpace(value))
- {
- return OneDFormat.CODE_128;
- }
- switch (value)
- {
- case "CODE_128":
- return OneDFormat.CODE_128;
- case "EAN_13":
- return OneDFormat.EAN_13;
- case "EAN_8":
- return OneDFormat.EAN_8;
- case "CODABAR":
- return OneDFormat.CODABAR;
- case "CODE_39":
- return OneDFormat.CODE_39;
- case "CODE_93":
- return OneDFormat.CODE_93;
- case "ITF":
- return OneDFormat.ITF;
- case "MSI":
- return OneDFormat.MSI;
- case "PLESSEY":
- return OneDFormat.PLESSEY;
- case "UPC_E":
- return OneDFormat.UPC_E;
- default:
- return OneDFormat.CODE_128;
- }
- }
- }
- public class QRCodeWriterMy : Writer
- {
- private const int QUIET_ZONE_SIZE = 4;
- /// <summary>
- /// Encode a barcode using the default settings.
- /// </summary>
- /// <param name="contents">The contents to encode in the barcode</param>
- /// <param name="format">The barcode format to generate</param>
- /// <param name="width">The preferred width in pixels</param>
- /// <param name="height">The preferred height in pixels</param>
- /// <returns>
- /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
- /// </returns>
- public BitMatrix encode(string contents, BarcodeFormat format, int width, int height)
- {
- return encode(contents, format, width, height, null);
- }
- /// <summary>
- /// </summary>
- /// <param name="contents">The contents to encode in the barcode</param>
- /// <param name="format">The barcode format to generate</param>
- /// <param name="width">The preferred width in pixels</param>
- /// <param name="height">The preferred height in pixels</param>
- /// <param name="hints">Additional parameters to supply to the encoder</param>
- /// <returns>
- /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white)
- /// </returns>
- public BitMatrix encode(string contents,
- BarcodeFormat format,
- int width,
- int height,
- IDictionary<EncodeHintType, object> hints)
- {
- if (String.IsNullOrEmpty(contents))
- {
- throw new ArgumentException("Found empty contents");
- }
- if (format != BarcodeFormat.QR_CODE)
- {
- throw new ArgumentException("Can only encode QR_CODE, but got " + format);
- }
- if (width < 0 || height < 0)
- {
- throw new ArgumentException("Requested dimensions are too small: " + width + 'x' + height);
- }
- var errorCorrectionLevel = ErrorCorrectionLevel.L;
- int quietZone = QUIET_ZONE_SIZE;
- if (hints != null)
- {
- if (hints.ContainsKey(EncodeHintType.ERROR_CORRECTION))
- {
- var requestedECLevel = hints[EncodeHintType.ERROR_CORRECTION];
- if (requestedECLevel != null)
- {
- errorCorrectionLevel = requestedECLevel as ErrorCorrectionLevel;
- if (errorCorrectionLevel == null)
- {
- switch (requestedECLevel.ToString().ToUpper())
- {
- case "L":
- errorCorrectionLevel = ErrorCorrectionLevel.L;
- break;
- case "M":
- errorCorrectionLevel = ErrorCorrectionLevel.M;
- break;
- case "Q":
- errorCorrectionLevel = ErrorCorrectionLevel.Q;
- break;
- case "H":
- errorCorrectionLevel = ErrorCorrectionLevel.H;
- break;
- default:
- errorCorrectionLevel = ErrorCorrectionLevel.L;
- break;
- }
- }
- }
- }
- if (hints.ContainsKey(EncodeHintType.MARGIN))
- {
- var quietZoneInt = hints[EncodeHintType.MARGIN];
- if (quietZoneInt != null)
- {
- quietZone = Convert.ToInt32(quietZoneInt.ToString());
- }
- }
- }
- var code = Encoder.encode(contents, errorCorrectionLevel, hints);
- BitMatrix bm = renderResult(code, width, height, quietZone);
- hints[EncodeHintType.WIDTH] = bm.Width;
- hints[EncodeHintType.HEIGHT] = bm.Height;
- return bm;
- }
- // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
- // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
- private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone)
- {
- var input = code.Matrix;
- if (input == null)
- {
- throw new InvalidOperationException();
- }
- int inputWidth = input.Width;
- int inputHeight = input.Height;
- int qrWidth = inputWidth + (quietZone << 1);
- int qrHeight = inputHeight + (quietZone << 1);
- int outputWidth = Math.Max(width, qrWidth);
- int outputHeight = Math.Max(height, qrHeight);
- int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
- // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
- // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
- // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
- // handle all the padding from 100x100 (the actual QR) up to 200x160.
- int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
- int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
- //int leftPadding = 0;//(outputWidth - (inputWidth * multiple)) / 2;
- //int topPadding = 0;//(outputHeight - (inputHeight * multiple)) / 2;
- //outputWidth = (inputWidth * multiple);
- //outputHeight = (inputHeight * multiple);
- var output = new BitMatrix(outputWidth, outputHeight);
- for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple)
- {
- // Write the contents of this row of the barcode
- for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple)
- {
- if (input[inputX, inputY] == 1)
- {
- output.setRegion(outputX, outputY, multiple, multiple);
- //int mX = outputWidth - outputX;
- //int mY = outputHeight - outputY;
- //mX = Math.Min(mX, multiple);
- //mY = Math.Min(mY, multiple);
- //output.setRegion(outputX, outputY, mX, mY);
- }
- }
- }
- return output;
- }
- }
- }
|