我有一個錯誤:DrawText的隨機給出錯誤:參數是無效
Parameter is not valid.
發生此錯誤在5次約1。
該錯誤發生在這條線:
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()
在這個方法的頂部解決了這個問題,我不希望使用此「修復」爲更好地防止它。- 一切都一次性在使用語句
- 的方法工作的大部分時間,1/5次失敗
價值觀的DrawText的時出現錯誤:
- Graphics對象(屬性可訪問)
- 「Code12」常規字符串
- 字體
Name = "Source Sans Pro" Size=179.088287
(pr operties訪問) - 矩形
X = 225 Y = 2565 Width = 4950 Height = 330
- 顏色
Name=ff666666, ARGB=(255, 102, 102, 102)
- 標誌
TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPadding
如果有誰知道爲什麼我得到這個錯誤,或者如何解決它,任何幫助表示讚賞。
調用'TextRenderer.DrawText'時檢查參數的值?你有什麼價值? –
@MightyBadaboom在示例中它們總是相同的(並且它失敗了大約1/5次),所以它不可能是它的值。我會去檢查你,雖然 – EpicKip
@MightyBadaboom添加了值 – EpicKip