我正在尝试为彩色图像添加文本比例.agcScale.jpg图像(下方)是顶部和底部的2个winform标签,左侧和右侧是2个winform图片框.完全相同的代码用于在右侧和左侧图片框中生成字符串,唯一的区别是pictureBoxAgcVscale仅包含字符串.为什么pictureBoxAgc中的DrawString看起来不错,但pictureBoxAgcVscale中的DrawString看起来如此糟糕?我可以通过为每个像素执行bmp.SetPixel来修复pictureBoxAgcVscale,但这似乎是解决此问题的错误方法.
private void DisplayAgcVscale(double min, double max)
{
var bmp = new Bitmap(pictureBoxAgcVscale.Width, pictureBoxAgcVscale.Height);
var c = (max - min) / bmp.Height;
using (var g = Graphics.FromImage(bmp))
{
var font = new Font("Microsoft Sans Serif", 8.25F);
var y1 = bmp.Height / 10;
for (var y = y1; y < bmp.Height; y += y1)
{
var agc = y * c + min;
var text = agc.ToString("#0.000V");
var h = bmp.Height - y - font.Height / 2;
g.DrawString(text, font, Brushes.Black, 0, …
Run Code Online (Sandbox Code Playgroud) 我的drawString()
方法中有一个paintComponent
方法.有没有办法让drawString()
粗体绘制文字?另外,有没有办法让文字更大?我想避免使用JLabel
s,除非绝对必要.
我需要能够旋转标签中的文本并将其与左,右或中心对齐.到目前为止,我能够在派生标签的onPaint方法中使用此代码进行旋转:
float width = graphics.MeasureString(Text, this.Font).Width;
float height = graphics.MeasureString(Text, this.Font).Height;
double angle = (_rotationAngle / 180) * Math.PI;
graphics.TranslateTransform(
(ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
(ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
graphics.RotateTransform(270f);
graphics.DrawString(Text, this.Font, textBrush, new PointF(0,0), stringFormat);
graphics.ResetTransform();
Run Code Online (Sandbox Code Playgroud)
它工作正常.我可以看到文字旋转了270度.
但是当我尝试在stringFormat中设置对齐时,它变得疯狂,我无法弄清楚发生了什么.
如何将文本旋转270度并将其对齐?
我正在为图像绘制字符串.图像的大小是动态的,或者换句话说,图像与显示字符串所需的一样大.为了实现这一点,我在使用Graphics.DrawString()渲染文本之前使用Graphics.MeasureString()测量大小.
这一切都很好,直到轮换发挥作用.到目前为止,我正在将字符串绘制到位图并旋转我得到的整个位图.
问题是我的调色板非常有限,没有混合颜色.所以我必须避免任何类型的抗锯齿,这只能通过旋转文本位图使用InterpolationMode.NearestNeighbor来实现.虽然这确保没有渲染出不需要的颜色,但结果确实非常难看(从用户的角度来看).
我的想法:应该可以通过使用Graphics.RotateTransform()旋转它来将文本绘制到位图并避免剪切,不是吗?
因为我必须首先定义要绘制的图像的大小,并且因为这个大小通过旋转而增加,所以我不知道如何完成这项工作.
任何帮助非常感谢!
我对这两种方法感到困惑.
我的理解是Graphics.DrawString()使用GDI +并且是基于图形的实现,而TextRenderer.DrawString()使用GDI并允许大量字体并支持unicode.
我的问题是当我尝试将基于十进制的数字打印为打印机的百分比时.我的研究让我相信TextRenderer是一个更好的方法.
但是,MSDN建议,"TextRenderer的DrawText方法不支持打印.您应该始终使用Graphics类的DrawString方法."
我使用Graphics.DrawString打印的代码是:
if (value != 0)
e.Graphics.DrawString(String.Format("{0:0.0%}", value), GetFont("Arial", 12, "Regular"), GetBrush("Black"), HorizontalOffset + X, VerticleOffset + Y);
Run Code Online (Sandbox Code Playgroud)
对于0到1之间的数字打印"100%",对于零以下的数字打印"-100%".
我放的时候
Console.WriteLine(String.Format("{0:0.0%}", value));
Run Code Online (Sandbox Code Playgroud)
在我的print方法中,值以正确的格式打印(例如:75.0%),所以我很确定问题出在Graphics.DrawString()中.
我正在使用c#2005我想在图像上对角线写字符串.但默认情况下,c#提供水平或垂直写入的选项.
我们如何写对角线?
谢谢
我已经在C#WinForms中创建了一个控件,并且在我的OnPaint()
处理程序中自定义绘图文本.使用我的表单中的以下代码将字体设置为Courier New:
FontFamily family = new FontFamily("Courier New");
this.myControl.Font = new Font(family, 10);
Run Code Online (Sandbox Code Playgroud)
在控件本身,字符串存储在realText
,我使用以下代码将其绘制到屏幕:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString(realText, Font, new SolidBrush(ForeColor), ClientRectangle);
}
Run Code Online (Sandbox Code Playgroud)
一些随机示例文本的结果如下所示:http: //img219.imageshack.us/img219/1778/courier.png
如果你放大,你可以看到,例如,第一个'as'之间的空间不同于第二个'as'之间的空格(1个像素对2个像素).有没有人知道可能导致这种情况的原因,或者我如何防止它发生?我使用不同的字体绘制的间距有很多类似的怪异,但我认为它们都是同一问题的结果.
提前感谢您的任何想法.
我正在尝试将一些文本渲染到Web窗体应用程序中的图像的特定部分.文本将由用户输入,因此我想改变字体大小以确保它适合边界框.
我的代码在我的概念验证实现上做得很好,但我现在正在尝试对付设计师的资产,这些资产更大,而且我得到了一些奇怪的结果.
我正在运行大小计算如下:
StringFormat fmt = new StringFormat();
fmt.Alignment = StringAlignment.Center;
fmt.LineAlignment = StringAlignment.Near;
fmt.FormatFlags = StringFormatFlags.NoClip;
fmt.Trimming = StringTrimming.None;
int size = __startingSize;
Font font = __fonts.GetFontBySize(size);
while (GetStringBounds(text, font, fmt).IsLargerThan(__textBoundingBox))
{
context.Trace.Write("MyHandler.ProcessRequest",
"Decrementing font size to " + size + ", as size is "
+ GetStringBounds(text, font, fmt).Size()
+ " and limit is " + __textBoundingBox.Size());
size--;
if (size < __minimumSize)
{
break;
}
font = __fonts.GetFontBySize(size);
}
context.Trace.Write("MyHandler.ProcessRequest", "Writing " + text + " in " …
Run Code Online (Sandbox Code Playgroud) 我使用Graphics DrawString方法在图像上写文本,用RectangleF绑定我的文本.这是我的代码:
//Write header2
RectangleF header2Rect = new RectangleF();
header2Rect.Width = 600;
header2Rect.Location = new Point(30, 105);
graphicImage.DrawString(header2, new Font("Gotham Medium", 28, FontStyle.Bold),brush, header2Rect);
//Write Description
RectangleF descrRect = new RectangleF();
descrRect.Width = 600;
int measurement = ((int)graphicImage.MeasureString(header2, new Font("Gotham Medium", 28, FontStyle.Bold)).Height);
var yindex = int.Parse(105 + header2Rect.Height.ToString());
descrRect.Location = new Point(30, 105+measurement);
graphicImage.DrawString(description.ToLower(), new Font("Gotham", 24, FontStyle.Italic), SystemBrushes.WindowText, descrRect);
Run Code Online (Sandbox Code Playgroud)
这适用于某些情况(即,当header2
只有1行长时),但我的measurement
变量只测量字体的高度,而不是整个DrawString
矩形.我不想设置静态header2Rect
高度,因为高度会根据该文本而变化.
yindex不起作用,因为header2Rect.Height = 0
.有没有办法看到我header2
有多少行?
我只需要做MeasureString
宽度并将其除以边界矩形宽度,然后乘以 …
我在C#中有这个代码来绘制旋转的文本
Font font = new Font("Arial", 80, FontStyle.Bold);
int nWidth = pictureBox1.Image.Width;
int nHeight = pictureBox1.Image.Height;
Graphics g = Graphics.FromImage(pictureBox1.Image);
float w = nWidth / 2;
float h = nHeight / 2;
g.TranslateTransform(w, h);
g.RotateTransform(90);
PointF drawPoint = new PointF(w, h);
g.DrawString("Hello world", font, Brushes.White, drawPoint);
Image myImage=new Bitmap(pictureBox1.Image);
g.DrawImage(myImage, new Point(0, 0));
pictureBox1.Image = myImage;
pictureBox1.Refresh();
Run Code Online (Sandbox Code Playgroud)
如果没有旋转,文本将被绘制在图像的中心,但是使用RotateTransform,它会从图像中移出一半,旋转中心也会偏离.
如何仅在文本中心旋转文本?不影响图像上的文字位置.