ich einen Fehler haben:DrawText gibt zufällig Fehler: Parameter ist nicht gültig
Parameter is not valid.
Dieser Fehler etwa 1 in 5-mal auftritt.
Der Fehler tritt auf dieser Linie:
TextRenderer.DrawText(drawing, "Code12", font, fullWidthRectangle,
textColor,
flags);
kleine Code-Beispiel (nicht tatsächliche Code):
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 Methode:
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);
}
Hinweise
GC.Collect()
an der Spitze dieser Methode behebt das Problem, ich möchte nicht als besser, dieses ‚fix‘ verwenden, um es zu verhindern.- Alles Einweg ist in using-Anweisungen
- Die Methode funktioniert die meiste Zeit, 1/5 es
Werte von DrawText schlägt fehl, wenn Fehler auftritt:
- Graphics-Objekt (Eigenschaften zugänglich)
- "Code12" reguläre Zeichenfolge
- Schriftart
Name = "Source Sans Pro" Size=179.088287
(pr operties zugänglich) - Rechteck
X = 225 Y = 2565 Width = 4950 Height = 330
- Farbe
Name=ff666666, ARGB=(255, 102, 102, 102)
- Flags
TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPadding
Wenn jemand weiß, warum ich diesen Fehler bin immer oder wie man es beheben, ist jede Hilfe dankbar.
Überprüfen Sie die Werte der Argumente beim Aufrufen von 'TextRenderer.DrawText'? Welche Werte bekommst du? –
@MightyBadaboom Sie sind immer die gleichen in dem Beispiel (und es scheitert etwa 1/5 mal), so dass es nicht die Werte ist. Ich werde für Sie überprüfen, obwohl – EpicKip
@MightyBadaboom Hinzugefügt die Werte – EpicKip