【发布时间】:2017-05-10 15:12:05
【问题描述】:
我有一个错误:
参数无效。
此错误大约发生五分之一。
错误发生在这一行:
TextRenderer.DrawText(drawing, "Code12", font, fullWidthRectangle,
textColor,
flags);
小代码示例(非实际代码):
public Image CreateTripDetailPreview(Image image)
{
using (var fontCollection = new PrivateFontCollection())
using (var fontCollectionBold = new PrivateFontCollection())
{
fontCollection.AddFontFile("Assets\\SourceSansPro-Regular.ttf");
fontCollectionBold.AddFontFile("Assets\\SourceSansPro-Bold.ttf");
//This will be used to define heigt of text and allign text
Rectangle fullWidthRectangle;
var widthInDip = 360;
var imgHeigtInDip = 168;
var canvasWidth = 1080;
var canvasHeight = 1200;
var dip = canvasWidth / widthInDip;
var leftRightMargin = 15 * dip;
var resolutionScale = 5;
using (Image img = new Bitmap(canvasWidth * resolutionScale, canvasHeight * resolutionScale))
using (Graphics drawing = Graphics.FromImage(img))
{
//Clear 'background' and make it white
drawing.Clear(Color.White);
var imageHeight = (int)167.5 * dip;
var height = imageHeight * resolutionScale;
using (var cityImageBitmap = new Bitmap(image))
using (var resizedCityImage = new Bitmap(cityImageBitmap, new Size(canvasWidth, imageHeight)))
{
canvasWidth *= resolutionScale;
canvasHeight *= resolutionScale;
dip *= resolutionScale;
leftRightMargin *= resolutionScale;
TextFormatFlags flags;
using (var regularFont = new Font(fontCollection.Families[0], 1, FontStyle.Regular))
using (var boldFont = new Font(fontCollectionBold.Families[0], 1, FontStyle.Regular)
) //1 as default fontsize, fontsize will be calculated for each property
{
Color textColor = Color.FromArgb(102, 102, 102);
//FlightCode
height += 4 * dip;
fullWidthRectangle = new Rectangle(leftRightMargin, height,
canvasWidth - leftRightMargin * 2,
(int)22.5 * dip);
using (Font font = GetFontSizeByBox(drawing, "Code12",
fullWidthRectangle.Size,
regularFont))
{
flags = TextFormatFlags.NoPadding | TextFormatFlags.HorizontalCenter;
TextRenderer.DrawText(drawing, "Code12", font, fullWidthRectangle,
textColor,
flags);
}
using (var result = new Bitmap(img, canvasWidth / resolutionScale, canvasHeight / resolutionScale))
using (Graphics drawing2 = Graphics.FromImage(result))
{
drawing2.DrawImage(resizedCityImage, new Point(0, 0));
return new Bitmap(result);
}
}
}
}
}
}
GetFontSizeByBox 方法:
private static Font GetFontSizeByBox(Graphics g, string longString, Size room, Font preferedFont, int extraSize = 0)
{
SizeF realSize = g.MeasureString(longString, preferedFont);
var heightScaleRatio = room.Height / realSize.Height;
var widthScaleRatio = room.Width / realSize.Width;
var scaleRatio = heightScaleRatio < widthScaleRatio ? heightScaleRatio : widthScaleRatio;
var scaleFontSize = preferedFont.Size * scaleRatio;
return new Font(preferedFont.FontFamily, scaleFontSize + extraSize, preferedFont.Style);
}
注意事项
-
GC.Collect()在这个方法的顶部解决了这个问题,我不希望使用这个“修复”来更好地防止它。 - 所有一次性的东西都在 using 语句中
- 该方法大部分时间都有效,1/5 次失败
发生错误时 DrawText 的值:
- 图形对象(属性可访问)
- “Code12”常规字符串
- 字体
Name = "Source Sans Pro" Size=179.088287(属性可访问) - 矩形
X = 225 Y = 2565 Width = 4950 Height = 330 - 颜色
Name=ff666666, ARGB=(255, 102, 102, 102) - 标志
TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPadding
如果有人知道我为什么会收到此错误或如何修复它,我们将不胜感激。
【问题讨论】:
-
调用
TextRenderer.DrawText时检查参数的值?你得到了什么价值观? -
@MightyBadaboom 在示例中它们总是相同的(并且失败了大约 1/5 次),因此不太可能是它的值。不过我会帮你查的
-
@MightyBadaboom 添加了值
-
行中 var imageHeight = (int)167.5 * dip;和 canvasWidth - leftRightMargin * 2, (int)22.5 * dip);强制转换仅将值 167.5 和 22.5 转换为 int。也许您需要在整个表达式周围放置括号。例如 var imageHieght = (int)(167.5 * dip)。只是一个想法。
-
@dannyhut 倾角已经是一个整数了,你很好看!你的方式会更干净。遗憾的是错误仍然存在:(
标签: c# system.drawing