public override void DirectDraw(Canvas ca, float x, float y, float dpiZoom) { SetBorder(); Graphics g = ca.Graphics; if (null == g) { return; } if (null != Border) { Border.FillAreaBackground(ca.Graphics, x, y); } try { if (!string.IsNullOrEmpty(RealText)) { byte[] imageArr = Convert.FromBase64String(RealText); if (null != imageArr) { this.Image = System.Drawing.Image.FromStream(new MemoryStream(imageArr)); } } switch (Mode) { case PictureBoxSizeMode.Normal: if (null != Image) { int w = this.Width > Image.Width ? Image.Width : this.Width; int h = this.Height > Image.Height ? Image.Height : this.Height; g.DrawImage(this.Image, x, y, new Rectangle(0, 0, w, h), GraphicsUnit.Pixel); } else if (null != ErrorImage) { int w = this.Width > Image.Width ? Image.Width : this.Width; int h = this.Height > Image.Height ? Image.Height : this.Height; g.DrawImage(this.ErrorImage, x, y, new Rectangle(0, 0, w, h), GraphicsUnit.Pixel); } break; case PictureBoxSizeMode.CenterImage: float imageLeft = 0, imageTop = 0; float bckLeft = 0, bckTop = 0; float imgDrawWidth = 0, imgDrawHeight = 0; if (this.Width >= Image.Width) { //imageLeft = x; imageLeft = 0; bckLeft = x + ((this.Width - Image.Width) / 2); imgDrawWidth = Image.Width; } else { imageLeft = (Image.Width - this.Width) / 2; bckLeft = x; imgDrawWidth = this.Width; } if (this.Height >= Image.Height) { //imageTop = y; imageTop = 0; bckTop = y + ((this.Height - Image.Height) / 2); imgDrawHeight = Image.Height; } else { imageTop = (Image.Height - this.Height) / 2; bckTop = y; imgDrawHeight = this.Height; } if (null != Image) { g.DrawImage(Image, bckLeft, bckTop, new Rectangle((int)imageLeft, (int)imageTop, (int)imgDrawWidth, (int)imgDrawHeight), GraphicsUnit.Pixel); } else if (null != ErrorImage) { g.DrawImage(ErrorImage, bckLeft, bckTop, new Rectangle((int)imageLeft, (int)imageTop, (int)imgDrawWidth, (int)imgDrawHeight), GraphicsUnit.Pixel); } break; case PictureBoxSizeMode.AutoSize: this.Width = Image.Width; this.Height = Image.Height; if (null != Image) { g.DrawImage(Image, x, y, Image.Width, Image.Height); } else if (null != ErrorImage) { g.DrawImage(ErrorImage, x, y, Image.Width, Image.Height); } break; case PictureBoxSizeMode.StretchImage: if (null != Image) { g.DrawImage(Image, x, y, this.Width, this.Height); } else if (null != ErrorImage) { g.DrawImage(ErrorImage, x, y, this.Width, this.Height); } break; case PictureBoxSizeMode.Zoom: float imgWidthHeightScale = (float)Image.Width / Image.Height; float imgWidth = 0, imgHeight = 0; float drawLeft = 0, drawTop = 0; float ctrlWidthHeightScale = (float)this.Width / this.Height; if (ctrlWidthHeightScale > imgWidthHeightScale) { imgHeight = this.Height; imgWidth = imgHeight * imgWidthHeightScale; drawLeft = ((float)this.Width - imgWidth) / 2 + x; drawTop = y; } else { imgWidth = this.Width; imgHeight = imgWidth / imgWidthHeightScale; drawLeft = x; drawTop = ((float)this.Height - imgHeight) / 2 + y; } if (null != Image) { g.DrawImage(Image, drawLeft, drawTop, imgWidth, imgHeight); } else if (null != ErrorImage) { g.DrawImage(ErrorImage, drawLeft, drawTop, imgWidth, imgHeight); } break; } } catch { if (null != ErrorImage) { g.DrawImage(ErrorImage, x, y, this.Width, this.Height); } } if (null != Border) { Border.DirectDraw(ca, x, y, dpiZoom); } }
/// <summary> /// 直接绘制 /// </summary> /// <param name="ca"></param> /// <param name="x"></param> /// <param name="y"></param> public override void DirectDraw(Canvas ca, float x, float y, float dpiZoom) { SetBorder(); MoveX = x; MoveY = y; Graphics g = ca.Graphics; SizeF textSize = SizeF.Empty; if (null != RealText) { textSize = g.MeasureString(RealText, this.Font); } float left = x; float top = y; if (AutoSize) { this.Width = (int)textSize.Width; this.Height = (int)textSize.Height; } // 上面的计算可能会改变控件的大小,所以将 // 边框的绘制放在放在这里 if (null != Border) { Border.FillAreaBackground(g, x, y); Border.DirectDraw(ca, x, y, dpiZoom); left += Border.BorderWidth; top += Border.BorderWidth; } Brush foreBrush = new SolidBrush(this.ForeColor); float tempWidth = this.Width - Border.BorderWidth; float tempHeight = this.Height - Border.BorderWidth; if (null != RealText && tempWidth != 0 && tempHeight != 0) { StringFormat sf = GetStringFormat(); try { //GraphicsContainer container = g.BeginContainer(); System.Drawing.Drawing2D.GraphicsState state = g.Save(); RectangleF rect = new RectangleF(left, top, tempWidth, tempHeight); Matrix mx = new Matrix(); if (RotateDegree != 0) { mx.RotateAt(RotateDegree, new PointF(rect.X + rect.Width / 2, rect.Y + rect.Height / 2)); //mx.Translate(textSize.Height, textSize.Width / 2); mx.Multiply(g.Transform, MatrixOrder.Append); g.Transform = mx; } g.DrawString(RealText, this.Font, foreBrush, rect /*new RectangleF(0, 0, tempWidth, tempHeight)*/, sf); mx.Reset(); mx.Dispose(); g.Restore(state); //g.EndContainer(container); } finally { sf.Dispose(); } } foreBrush.Dispose(); }
/// <summary> /// 直接绘制 /// </summary> /// <param name="ca"></param> /// <param name="x"></param> /// <param name="y"></param> public override void DirectDraw(Canvas ca, float x, float y, float dpiZoom) { SetBorder(); MoveX = x; MoveY = y; if (null == ca || null == ca.Graphics) { return; } if (string.IsNullOrEmpty(RealText) && !string.IsNullOrEmpty(Text)) { RealText = Text; } else if (string.IsNullOrEmpty(RealText)) { return; } try { if (null == _QRCodeControl) { _QRCodeControl = new QRCodeControl(); _QRCodeControl.IncludeLabel = IncludeLabel; _QRCodeControl.backColor = BackColor; _QRCodeControl.foreColor = ForeColor; _QRCodeControl.CorrectionLevel = CorrectionLevel; _QRCodeControl.EncodedMode = EncodedMode; _QRCodeControl.QRCodeScale = _QRCodeScale; _QRCodeControl.Version = Version; _QRCodeControl.RawData = RealText; } Image img = _QRCodeControl.PictureBoxImage; if (null != img) { ca.Graphics.DrawImage(img, x, y, this.Width, this.Height); img.Dispose(); } } catch (Exception ex) { string message = ex.Message; if (!string.IsNullOrEmpty(message)) { SizeF size = ca.Graphics.MeasureString(ErrorText, this.Font); float left = ((float)this.Width - size.Width) / 2; if (left < 0) { left = x; } else { left += x; } float top = ((float)this.Height - size.Height) / 2; if (top < 0) { top = y; } else { top += y; } float width = this.Width > size.Width ? size.Width : this.Width; float height = this.Height > size.Height ? size.Height : this.Height; ca.Graphics.DrawString(ErrorText, this.Font, Brushes.Red, new RectangleF(left, top, width, height)); } } finally { if (null != _QRCodeControl) { _QRCodeControl.Dispose(); _QRCodeControl = null; } } if (null != Border) { Border.DirectDraw(ca, x, y, dpiZoom); } }
/// <summary> /// 直接绘制 /// </summary> /// <param name="ca"></param> /// <param name="x"></param> /// <param name="y"></param> public override void DirectDraw(Canvas ca, float x, float y, float dpiZoom) { SetBorder(); MoveX = x; MoveY = y; if (null == ca || null == ca.Graphics) { return; } if (string.IsNullOrEmpty(RealText) && !string.IsNullOrEmpty(Text)) { RealText = Text; } else if (string.IsNullOrEmpty(RealText)) { return; } Barcode bc = null; try { bc = new Barcode(); bc.Alignment = this.BarCodeAlign; bc.IncludeLabel = IncludeLabel; bc.LabelFont = LabelFont; Image img = bc.Encode(BarCodeType, RealText, this.ForeColor, this.BackColor, this.Width, this.Height); if (null != img) { ca.Graphics.DrawImage(img, x, y, this.Width, this.Height); img.Dispose(); } } catch (Exception ex) { string message = ex.Message; if (!string.IsNullOrEmpty(message)) { if (!message.StartsWith("EENCODE-1:") && !message.StartsWith("EENCODE-2:")) { SizeF size = ca.Graphics.MeasureString(ErrorText, this.Font); float left = ((float)this.Width - size.Width) / 2; if (left < 0) { left = x; } else { left += x; } float top = ((float)this.Height - size.Height) / 2; if (top < 0) { top = y; } else { top += y; } float width = this.Width > size.Width ? size.Width : this.Width; float height = this.Height > size.Height ? size.Height : this.Height; ca.Graphics.DrawString(ErrorText, this.Font, Brushes.Red, new RectangleF(left, top, width, height)); } } } finally { if (null != bc) { bc.Dispose(); } } if (null != Border) { Border.DirectDraw(ca, x, y, dpiZoom); } }
public override void DirectDraw(Canvas ca, float x, float y, float dpiZoom) { if (Transparent && this.Site == null) { return; } SetBorder(); MoveX = x; MoveY = y; Graphics g = ca.Graphics; float left = x; float top = y; // 上面的计算可能会改变控件的大小,所以将 // 边框的绘制放在放在这里 Border.FillAreaBackground(g, x, y); Border.DirectDraw(ca, x, y, dpiZoom); if (null != RealText && IsRedrawText) { SizeF textSize = g.MeasureString(RealText, this.Font); //非自动大小的情况下的文本位置 #region 计算文本的 XY坐标 switch (TextAlign) { case ContentAlignment.BottomCenter: left += (this.Width - 2 * Border.BorderWidth - textSize.Width) / 2 < 0 ? Border.BorderWidth : (this.Width - 2 * Border.BorderWidth - textSize.Width) / 2 + Border.BorderWidth; top += this.Height - textSize.Height - Border.BorderWidth < 0 ? Border.BorderWidth : this.Height - textSize.Height - Border.BorderWidth; break; case ContentAlignment.BottomLeft: top += this.Height - textSize.Height - 2 * Border.BorderWidth < 0 ? Border.BorderWidth : this.Height - textSize.Height - Border.BorderWidth; left += Border.BorderWidth; break; case ContentAlignment.BottomRight: left += this.Width - textSize.Width - 2 * Border.BorderWidth < Border.BorderWidth ? Border.BorderWidth : this.Width - textSize.Width - Border.BorderWidth; top += this.Height - textSize.Height - Border.BorderWidth < 0 ? Border.BorderWidth : this.Height - textSize.Height - Border.BorderWidth;; break; case ContentAlignment.MiddleCenter: left += (this.Width - 2 * Border.BorderWidth - textSize.Width) / 2 < 0 ? Border.BorderWidth : (this.Width - 2 * Border.BorderWidth - textSize.Width) / 2 + Border.BorderWidth; top += (this.Height - 2 * Border.BorderWidth - textSize.Height) / 2 < 0 ? Border.BorderWidth : (this.Height - 2 * Border.BorderWidth - textSize.Height) / 2 + Border.BorderWidth; break; case ContentAlignment.MiddleLeft: top += (this.Height - 2 * Border.BorderWidth - textSize.Height) / 2 < 0 ? Border.BorderWidth : (this.Height - 2 * Border.BorderWidth - textSize.Height) / 2 + Border.BorderWidth; left += Border.BorderWidth;; break; case ContentAlignment.MiddleRight: left += this.Width - textSize.Width - Border.BorderWidth < Border.BorderWidth ? Border.BorderWidth : this.Width - textSize.Width - Border.BorderWidth; top += (this.Height - 2 * Border.BorderWidth - textSize.Height) / 2 < 0 ? Border.BorderWidth : (this.Height - 2 * Border.BorderWidth - textSize.Height) / 2 + Border.BorderWidth; break; case ContentAlignment.TopCenter: left += (this.Width - textSize.Width) / 2 < 0 ? Border.BorderWidth : (this.Width - textSize.Width) / 2 + Border.BorderWidth; top += Border.BorderWidth; break; case ContentAlignment.TopLeft: top += Border.BorderWidth; left += Border.BorderWidth; break; case ContentAlignment.TopRight: left += this.Width - textSize.Width - Border.BorderWidth < Border.BorderWidth ? Border.BorderWidth : this.Width - textSize.Width - Border.BorderWidth; top += Border.BorderWidth; break; } #endregion Brush foreBrush = new SolidBrush(this.ForeColor); try { float tempWidth = this.Width - left + MoveX - Border.BorderWidth - textSize.Width <= 0 ? this.Width - left + MoveX - Border.BorderWidth : textSize.Width; float tempHeight = this.Height - top + MoveY - Border.BorderWidth - textSize.Height <= 0 ? this.Height - top + MoveY - Border.BorderWidth : textSize.Height; if (tempWidth != 0 && tempHeight != 0) { g.DrawString(RealText, this.Font, foreBrush, new RectangleF(left, top, tempWidth, tempHeight)); } } finally { foreBrush.Dispose(); } } base.DirectDraw(ca, x, y, dpiZoom); }