我在谷歌上找不到答案。
我有很多字符串要在画布上呈现。每个字符串都将使用从 ItemsControl 调用的字符串转换器中的 FormattedText() 方法创建。
代码的问题在于,它需要 RenderTargetBitmap() 中的一个大得离谱的宽度和高度来显示不同位置的所有字符串,即使每个字符串的实际宽度约为 700,实际高度为 40。(看起来像虽然 RenderTargetBitmap() 需要足够大,不仅可以容纳字符串,还可以容纳该字符串在绘图上下文中的位置)。
您如何创建一个仅包含格式化文本的正确实际高度和宽度的单个格式化文本字符串的图像,然后将该图像正确定位在“左上角”点?
从 ItemsControl 调用的转换器定义为:
public class InkConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var stringviewmodel = value as StringViewModel;
if (stringviewmodel != null)
{
stringviewmodel.ft = new FormattedText(
stringviewmodel.text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily("Segoe Script"), FontStyles.Italic, FontWeights.Normal, FontStretches.Normal),
stringviewmodel.emSize,
stringviewmodel.color);
stringviewmodel.ft.TextAlignment = TextAlignment.Left;
stringviewmodel.ft.LineHeight =(double)stringviewmodel.lineheight;
Image myImage = new Image();
DrawingVisual dw = new DrawingVisual();
DrawingContext dc = dw.RenderOpen();
dc.DrawText(stringviewmodel.ft, stringviewmodel.topleft);
dc.Close();
int Width = 2000;
int Height = 2000;
RenderTargetBitmap bmp = new RenderTargetBitmap(Width, Height, 120, 96, PixelFormats.Pbgra32);
bmp.Render(dw);
myImage.Source = bmp;
return myImage;
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
附录:
我写了这个解决了这个问题,但没有回答我的问题。(删除了 gt 和 lt 符号)。
将 itemscontrol 更改为包括:
ItemsControl.ItemContainerStyle Style TargetType="{x:Type FrameworkElement} Setter Property="Canvas.Top" Value="{Binding topleft.Y}" Setter Property="Canvas.Left" Value="{Binding topleft.X}" Setter属性="高度" 值="{绑定英尺高度}"
移除了转换器的所有定位。转换器现在读取为
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var stringviewmodel = value as StringViewModel; if (stringviewmodel != null) { stringviewmodel.ft = new FormattedText( stringviewmodel.text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(new FontFamily("Segoe Script"), FontStyles.Italic, FontWeights.Normal, FontStretches.Normal), stringviewmodel.emSize, stringviewmodel.color); stringviewmodel.ft.TextAlignment = TextAlignment.Left; stringviewmodel.ft.LineHeight =(double)stringviewmodel.lineheight; Image myImage = new Image(); DrawingVisual dw = new DrawingVisual(); DrawingContext dc = dw.RenderOpen(); dc.DrawText(stringviewmodel.ft, new Point(0,0)); dc.Close(); double width = stringviewmodel.ft.text.Width - stringviewmodel.text.OverhangLeading - stringviewmodel.text.OverhangTrailing; RenderTargetBitmap bmp = new RenderTargetBitmap( (int)(width, (int)stringviewmodel.ft.Height, 96d, 96d, PixelFormats.Pbgra32); bmp.Render(dw); myImage.Source = bmp; return myImage; } else { return null; } }