gpt4 book ai didi

c# - System.Drawing.Graphics.DrawString - "Parameter is not valid"异常

转载 作者:可可西里 更新时间:2023-11-01 08:05:42 24 4
gpt4 key购买 nike

有时,Microsoft 的异常消息毫无帮助,令人恼火。我创建了一个漂亮的小 MVC 方法来呈现文本。方法体如下。当它到达“DrawString”方法时,我得到一个异常抛出说“参数无效”。

请注意,据我所知,字体构造正确(我只是使用 10pt 的 Arial),矩形大小为正且看起来有效,画笔为白色 SolidBrush,格式标志不影响输出;也就是说,如果我从调用中排除格式标志,我仍然会收到错误消息。

DrawString 调用就在底部附近。

public ActionResult RenderText(
string fontFamily,
float pointSize,
string foreColor,
string backColor,
bool isBold,
bool isItalic,
bool isVertical,
string align,
string[] allText,
int textIndex)
{
// method renders a horizontal or vertical text image, taking all the text strings that will be rendered in each image
// and sizing the final bitmap according to which text would take the most space, thereby making it possible to render
// a selection of text images all at the same size.

Response.ContentType = "image/png";

var fmt = StringFormat.GenericTypographic;
if(isVertical)
fmt.FormatFlags = StringFormatFlags.DirectionVertical;

Func<string,StringAlignment> getAlign = (s => {
switch(s.ToLower())
{
case "right": return StringAlignment.Far;
case "center": return StringAlignment.Center;
default: return StringAlignment.Near;
}
});
fmt.LineAlignment = isVertical ? StringAlignment.Center : getAlign(align);
fmt.Alignment = isVertical ? getAlign(align) : StringAlignment.Center;

var strings = (allText ?? new string[0]).Where(t => t.Length > 0).ToList();
if(strings.Count == 0)
strings.Add("[Missing Text]");

FontStyle style = FontStyle.Regular;
if(isBold)
if(isItalic)
style = FontStyle.Bold | FontStyle.Italic;
else
style = FontStyle.Bold;
else if(isItalic)
style = FontStyle.Italic;

Font font = new Font(fontFamily, pointSize, style, GraphicsUnit.Point);
Color fc = foreColor.IsHexColorString() ? foreColor.ToColorFromHex() : foreColor.ToColor();
Color bc = backColor.IsHexColorString() ? backColor.ToColorFromHex() : backColor.ToColor();

var maxSize = new Size(0,0);
using(var tmp = new Bitmap(100, 200))
using(var gfx = Graphics.FromImage(tmp))
foreach(var txt in strings)
{
var size = gfx.MeasureString(txt, font, 1000, fmt);
maxSize = new Size(
Math.Max(Convert.ToInt32(isVertical ? size.Height : size.Width), maxSize.Width),
Math.Max(Convert.ToInt32(isVertical ? size.Width : size.Height), maxSize.Width)
);
}

using(var bmp = new Bitmap(maxSize.Width, maxSize.Height))
{
using(var gfx = Graphics.FromImage(bmp))
{
gfx.CompositingMode = CompositingMode.SourceCopy;
gfx.CompositingQuality = CompositingQuality.HighQuality;
gfx.SmoothingMode = SmoothingMode.HighQuality;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

var rect = new RectangleF(new PointF(0,0), maxSize);
gfx.FillRectangle(new SolidBrush(bc), rect);
gfx.DrawString(strings[textIndex], font, new SolidBrush(fc), rect, fmt);
}
bmp.Save(Response.OutputStream, ImageFormat.Png);
}
return new EmptyResult();
}

最佳答案

好吧,我找到了问题的原因。很晦涩的东西。当我删除此行时代码有效:

gfx.CompositingMode = CompositingMode.SourceCopy;

关于c# - System.Drawing.Graphics.DrawString - "Parameter is not valid"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1232165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com