/******************************************************************************* * Copyright(c) 2012 dongke All rights reserved. / Confidential * 类的信息: * 1.程序名称:TextItem.cs * 2.功能描述: 文本Item * 编辑履历: * 作者 日期 版本 修改内容 * 欧阳涛 2012/09/14 1.00 新建 *******************************************************************************/ using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using Curtain.Framework.Barcode; using Curtain.Framework.Barcode.OneD; using Curtain.Framework.Barcode.QRCode; using ZXing; using ZXing.Common; using ZXing.QrCode; using ZXing.QrCode.Internal; namespace Dongke.WinForm.Controls.InvoiceLayout { [Serializable] public class TextItem : LayoutItem, IBoundItem { #region 成员变量 private string _dataMember; // 对应数据源中的字段名 private string _displayValue; // 编辑模式显示的值 private object _formatType; // 格式类别 private string _format; // 文本格式 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; // 绘制文字间隔 //[NonSerialized] //protected RectangleF _marginRectangleFDraw; // 绘制范围 private int _travelDocItemID; // 项目ID private string _itemCode; // 项目ID private int _itemRefClass; // 分类 private ItemStyle _itemStyle; // 项目类别 private string _itemSample; // 项目示例 #endregion 成员变量 #region 属性 /// /// 对应数据源中的字段名 /// 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); //} } } } /// /// 编辑模式显示的值 /// 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); //} } } } /// /// 格式类别 /// public object FormatType { get { return _formatType; } set { _formatType = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } /// /// 文本格式 /// 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); //} } } } /// /// 文本字体 /// 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); //} } } } /// /// 是否自动文本换行 /// public bool Wrap { get { return _wrap; } set { if (_wrap != value) { _wrap = value; _clip = !_wrap; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 是否剪裁延伸到边框外侧的文本 /// public bool Clip { get { _clip = !_wrap; return _clip; } } /// /// 文本颜色 /// public Color TextColor { get { return _textColor; } set { if (!_textColor.Equals(value)) { _textColor = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 文本对齐方式 /// public TextAlignment TextAlign { get { return _textAlign; } set { if (_textAlign != value) { _textAlign = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 文本垂直对齐方式 /// public TextAlignment TextAlignVertical { get { return _textAlignVertical; } set { if (_textAlignVertical != value) { _textAlignVertical = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 文字间隔 /// public float CharacterSpace { get { return _characterSpace; } set { if (_characterSpace != value) { _characterSpace = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 文字行间隔 /// public float LineSpace { get { return _lineSpace; } set { if (_lineSpace != value) { _lineSpace = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 行文字数 /// public int CharacterCount { get { return _characterCount; } set { if (_characterCount != value) { _characterCount = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 文本默认值 /// 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); //} } } } /// /// culture /// 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); //} } } } /// /// 文本项目名 /// 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); //} } } } /// /// 默认项目名 /// 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); //} } } } ///// ///// 数据源中的值 ///// //internal object DataValue //{ // get // { // return _dataValue; // } // set // { // if (value != _dataValue // || (value != null && !value.Equals(_dataValue))) // { // _dataValue = value; // } // } //} ///// ///// 打印数据源中的值 ///// //internal object PrintDataValue //{ // get // { // return _printDataValue; // } // set // { // if (value != _printDataValue // || (value != null && !value.Equals(_printDataValue))) // { // _printDataValue = value; // } // } //} /// /// 是否数据绑定 /// internal bool IsDataBinding { get { return _isDataBinding; } set { _isDataBinding = value; } } ///// ///// 数据源字段 ///// //internal DataBoundField BoundField //{ // get // { // return _boundField; // } // set // { // _boundField = value; // } //} ///// ///// 打印数据源字段 ///// //internal DataBoundField PrintBoundField //{ // get // { // return _printBoundField; // } // set // { // _printBoundField = value; // } //} ///// ///// 绘制字体 ///// //internal Font FontDraw //{ // get // { // return _fontDraw; // } //} ///// ///// 绘制文字间隔 ///// //internal float CharacterSpaceDraw //{ // get // { // return _characterSpaceDraw; // } //} ///// ///// 绘制行间隔 ///// //internal float LineSpaceDraw //{ // get // { // return _lineSpaceDraw; // } //} /// /// 项目ID /// public int TravelDocItemID { get { return _travelDocItemID; } set { if (_travelDocItemID != value) { _travelDocItemID = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } if (0 < _travelDocItemID) { _isDataBinding = true; } else { _isDataBinding = false; } } } /// /// 项目编码 /// public string ItemCode { get { return this._itemCode; } set { if (this._itemCode != value) { this._itemCode = value; } if (string.IsNullOrEmpty(this._itemCode)) { this._isDataBinding = false; } else { this._isDataBinding = true; } } } /// /// 分类 /// public int ItemRefClass { get { return _itemRefClass; } set { if (value != _itemRefClass) { _itemRefClass = value; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 项目类别 /// public ItemStyle ItemStyle { get { return _itemStyle; } set { if (value != _itemStyle) { _itemStyle = value; //this._picCode = null; //if (Owner != null) //{ // Owner.SetItemChangedArgs(this, ItemChangeType.Modified); //} } } } /// /// 项目示例 /// 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); //} } } } public bool PicColumn { get; set; } public bool PicMargin { get; set; } public bool FixedRatio { get; set; } #endregion 属性 #region 构造函数 /// /// 构造函数 /// internal TextItem() : this(ItemType.Text) { } ///// ///// 构造函数 ///// //internal TextItem(LayoutBox box) // : this(box, ItemType.Text) //{ //} /// /// 构造函数 /// 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 函数 /// /// 格式化数据源中的值 /// 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; } /// /// 文本内容设置 /// public object ResetTextValue(object value, bool isDataBound) { //if (Owner == null) //{ // return _displayValue; //} //if (Owner.LayoutMode == LayoutMode.Preview) { if (!string.IsNullOrEmpty(_dataMember)) { if (isDataBound) { //object result = value;//Owner.SetItemValueFormattingArgs(this, value, _format); if (PicColumn) { //if (result != null && result is Image) //{ // return result; //} if (value != null && value is byte[]) { using (MemoryStream ms = new 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; //} } /// /// Item初始化 /// /// LayoutBox /// 是否生成新ID /// 是否成功 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; } ///// ///// Item图形上的OnPaint事件 ///// ///// 指定的对象 ///// 提供的事件数据 //protected override void OnPaint(object sender, PaintEventArgs e) //{ // DrawTextItem(_dataValue, _boundField, e.Graphics, RectangleFDraw, _marginRectangleFDraw); //} //private void DrawTextItem(object date, DataBoundField dataBoundField, Graphics graphics, RectangleF rectangleM, RectangleF rectangle) //{ // object value = ResetTextValue(date, dataBoundField.IsDataBound); // if (value == null) // { // return; // } // if (PicColumn && value is Image) // { // Image image = value as 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; // } // LayoutUtility.DrawText(graphics, // GraphicsUnit.Millimeter, // rectangleM, // rectangle, // value.ToString(), // _fontDraw, // _textColor, // _lineSpaceDraw, // _characterSpaceDraw, // _characterCount, // _textAlign, // _textAlignVertical, // _wrap, // Clip, // Selected); //} public void PrintTextItem(object date, Graphics graphics, RectangleF rectangleM, RectangleF rectangle) { object value = ResetTextValue(date, true); if (value == null) { return; } //bool isPrint = true; try { if (this._itemStyle == ItemStyle.Barcode) { OneDDrawingOptions ops = new OneDDrawingOptions(); //ops.DrawGraphics = graphics; ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom: BarcodeShowType.Show); //ops.ImageRect = new Rectangle( // LayoutCommon.MillimeterToPixel(rectangleM.Left, graphics.DpiX), // LayoutCommon.MillimeterToPixel(rectangleM.Top, graphics.DpiX), // LayoutCommon.MillimeterToPixel(rectangleM.Width, graphics.DpiX), // LayoutCommon.MillimeterToPixel(rectangleM.Height, graphics.DpiX)); //BarcodeDraw.DrawQRCodeImageOnGraphics(value.ToString(), imageRect, ops); ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX); ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY); ops.DpiX = graphics.DpiX; ops.DpiY = graphics.DpiY; using (Image pic1 = OneDHelper.GetOneDImage(value.ToString(), ops)) { graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel); } return; } if (this._itemStyle == ItemStyle.QRCode) { QRCodeDrawingOptions ops = new QRCodeDrawingOptions(); //ops.DrawGraphics = graphics; ops.ShowType = (this.Wrap ? BarcodeShowType.Zoom : BarcodeShowType.Show); //ops.ImageRect = new Rectangle( // LayoutCommon.MillimeterToPixel(rectangleM.Left, graphics.DpiX), // LayoutCommon.MillimeterToPixel(rectangleM.Top, graphics.DpiX), // LayoutCommon.MillimeterToPixel(rectangleM.Width, graphics.DpiX), // LayoutCommon.MillimeterToPixel(rectangleM.Height, graphics.DpiX)); //BarcodeDraw.DrawQRCodeImageOnGraphics(value.ToString(), imageRect, ops); ops.ImageWidth = LayoutCommon.MillimeterToPixelDpi(rectangleM.Width, graphics.DpiX); ops.ImageHeight = LayoutCommon.MillimeterToPixelDpi(rectangleM.Height, graphics.DpiY); ops.DpiX = graphics.DpiX; ops.DpiY = graphics.DpiY; using (Image pic1 = QRCodeHelper.GetQRCodeImage(value.ToString(), ops)) { graphics.DrawImage(pic1, rectangleM, ops.ImageRect, GraphicsUnit.Pixel); } return; } } catch (Exception ex) { throw ex; } if (PicColumn && value is Image) { Image image = value as 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; } LayoutUtility.DrawText(graphics, GraphicsUnit.Millimeter, rectangleM, rectangle, value.ToString(), _font, _textColor, _lineSpace, _characterSpace, _characterCount, _textAlign, _textAlignVertical, _wrap, Clip, false); } #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 class QRCodeWriterMy : Writer { private const int QUIET_ZONE_SIZE = 4; /// /// Encode a barcode using the default settings. /// /// The contents to encode in the barcode /// The barcode format to generate /// The preferred width in pixels /// The preferred height in pixels /// /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white) /// public BitMatrix encode(string contents, BarcodeFormat format, int width, int height) { return encode(contents, format, width, height, null); } /// /// /// The contents to encode in the barcode /// The barcode format to generate /// The preferred width in pixels /// The preferred height in pixels /// Additional parameters to supply to the encoder /// /// The generated barcode as a Matrix of unsigned bytes (0 == black, 255 == white) /// public BitMatrix encode(string contents, BarcodeFormat format, int width, int height, IDictionary 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; } } }