private static Bitmap CreateBarcode(String data) { data = data.ToUpper(); if (data.Length > 0) { if (data[0] != '*') { data = "*" + data; } if (data[data.Length - 1] != '*') { data = data + "*"; } } Bitmap barCode = new Bitmap(1, 1); System.Drawing.Font threeOfNine = new System.Drawing.Font("Free 3 of 9", 240, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); System.Drawing.Font timesNR = new System.Drawing.Font("Times New Roman", 80, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); Graphics graphics = Graphics.FromImage(barCode); SizeF dataSize = new SizeF(); SizeF dataSize_bar = graphics.MeasureString(data, threeOfNine); SizeF dataSize_text = graphics.MeasureString(data, timesNR); if (dataSize_bar.Width > dataSize_text.Width) { dataSize.Width = dataSize_bar.Width; } else { dataSize.Width = dataSize_text.Width; } dataSize.Height = (dataSize_bar.Height + dataSize_text.Height); barCode = new Bitmap(barCode, dataSize.ToSize()); graphics = Graphics.FromImage(barCode); graphics.Clear(Color.White); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; graphics.DrawString(data, threeOfNine, new SolidBrush(Color.Black), ((dataSize.Width/2f)-(dataSize_bar.Width/2f)), 0); graphics.DrawString(data, timesNR, new SolidBrush(Color.Black), ((dataSize.Width / 2f) - (dataSize_text.Width / 2f)), dataSize_bar.Height); graphics.Flush(); threeOfNine.Dispose(); graphics.Dispose(); return barCode; }
// 根据Font,得到字符串的宽度 // parameter: // g Graphics对象 // font Font对象 // strText 字符串 // return: // 字符串的宽度 public static int GetWidth(Graphics g, Font font, string strText) { SizeF sizef = new SizeF(); sizef = g.MeasureString( strText, font); return sizef.ToSize().Width + 6; // 微调 }
private void timer1_Tick(object sender, EventArgs e) { //Thread.Sleep(100); label3.Text = load + l1; if (i < 10) { label2.Text = "0" + i.ToString(); } else { label2.Text = i.ToString(); } int r = random.Next(100, 150); int g = random.Next(50, 100); int b = random.Next(150, 250); SizeF s = new SizeF(textBox2.Size.Width + 2, textBox2.Size.Height); textBox2.Size = s.ToSize(); if (i % 5 == 0) { textBox2.BackColor = Color.FromArgb(r, g, b); label1.ForeColor = Color.FromArgb(b, r, g); textBox1.BackColor = Color.FromArgb(g, b, r); label3.Text = load + l2; } textBox2.Update(); label1.Update(); textBox1.Update(); label3.Update(); Thread.Sleep(100); if (textBox2.Size.Width >= textBox1.Size.Width) { timer1.Stop(); Thread.Sleep(1000); this.Hide(); main.ShowDialog(); } i++; }
private Size CalculateMinimumSize() { using ( Graphics g = Graphics.FromHwnd( Handle ) ) { _sizeText = g.MeasureString( Text, Font, -1, _stringFormat ); } // MeasureString() cuts off the bottom pixel for descenders no matter // which StringFormatFlags are chosen // _sizeText.Height += 1F; return _sizeText.ToSize(); }
protected override void OnPaint(PaintEventArgs e) { if (target == null) return; if (scale <= float.Epsilon) return; //calc startPlace var r = target.VisibleRange; if (startPlace.iLine > r.Start.iLine) startPlace.iLine = r.Start.iLine; else { var endP = target.PlaceToPoint(r.End); endP.Offset(0, -(int)(ClientSize.Height / scale) + target.CharHeight); var pp = target.PointToPlace(endP); if (pp.iLine > startPlace.iLine) startPlace.iLine = pp.iLine; } startPlace.iChar = 0; //calc scroll pos var linesCount = target.Lines.Count; var sp1 = (float)r.Start.iLine / linesCount; var sp2 = (float)r.End.iLine / linesCount; //scale graphics e.Graphics.ScaleTransform(scale, scale); //draw text var size = new SizeF(ClientSize.Width / scale, ClientSize.Height / scale); target.DrawText(e.Graphics, startPlace, size.ToSize()); //draw visible rect var p0 = target.PlaceToPoint(startPlace); var p1 = target.PlaceToPoint(r.Start); var p2 = target.PlaceToPoint(r.End); var y1 = p1.Y - p0.Y; var y2 = p2.Y + target.CharHeight - p0.Y; e.Graphics.SmoothingMode = SmoothingMode.HighQuality; using (var brush = new SolidBrush(Color.FromArgb(50, ForeColor))) using (var pen = new Pen(brush, 1 / scale)) { var rect = new Rectangle(0, y1, (int)((ClientSize.Width - 1) / scale), y2 - y1); e.Graphics.FillRectangle(brush, rect); e.Graphics.DrawRectangle(pen, rect); } //draw scrollbar if (scrollbarVisible) { e.Graphics.ResetTransform(); e.Graphics.SmoothingMode = SmoothingMode.None; using (var brush = new SolidBrush(Color.FromArgb(200, ForeColor))) { var rect = new RectangleF(ClientSize.Width - 3, ClientSize.Height*sp1, 2, ClientSize.Height*(sp2 - sp1)); e.Graphics.FillRectangle(brush, rect); } } needRepaint = false; }
protected override void OnHandleCreated(EventArgs e) { if ( builder.Length > 0 ) { this.exactSize = MeasureString( builder.ToString() ); this.AutoScrollMinSize = exactSize.ToSize(); } this.OnFontChanged(e); base.OnHandleCreated (e); }
public void Write( string text ) { int lengthSoFar = builder.Length; builder.Append( text ); if ( this.IsHandleCreated ) { bool isUpdateVisible = this.AutoScrollPosition.Y + this.AutoScrollMinSize.Height <= this.ClientRectangle.Height; if ( text != null && lengthSoFar > 0 && builder[lengthSoFar-1] == '\n' ) // use fast path { SizeF textSize = MeasureString( text ); this.exactSize = new SizeF( Math.Max( this.exactSize.Width, textSize.Width ), this.exactSize.Height + textSize.Height - this.fastPathHeightAdjust ); this.AutoScrollMinSize = exactSize.ToSize(); } else { this.exactSize = MeasureString( builder.ToString() ); this.AutoScrollMinSize = exactSize.ToSize(); } if ( isUpdateVisible ) this.Invalidate(); } }
private static Bitmap CreateLabel(String data) { data = data.ToUpper(); Bitmap docTypeLabel = new Bitmap(1, 1); System.Drawing.Font timesNR = new System.Drawing.Font("Times New Roman", 80, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); Graphics graphics = Graphics.FromImage(docTypeLabel); SizeF dataSize = new SizeF(); SizeF dataSize_text = graphics.MeasureString(data, timesNR); dataSize.Width = dataSize_text.Width; dataSize.Height = dataSize_text.Height; docTypeLabel = new Bitmap(docTypeLabel, dataSize.ToSize()); graphics = Graphics.FromImage(docTypeLabel); graphics.Clear(Color.White); graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; graphics.DrawString(data, timesNR, new SolidBrush(Color.Black), ((dataSize.Width / 2f) - (dataSize_text.Width / 2f)), 0); graphics.Flush(); timesNR.Dispose(); graphics.Dispose(); return docTypeLabel; }