2 回答
TA贡献1843条经验 获得超7个赞
只需绑定 的Text属性TextBlock即可使用TextBox的Text。
像这样:
<TextBlock Grid.Column="1" FontSize="16" HorizontalAlignment="Right" Margin="0,5,22,472" Text="{Binding ElementName=SO, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>
更新
在评论和编辑的问题之后。
您可以将TextBlock之后Image放在网格中,然后生成包含所有视觉效果的新图像。
它会是这样的:
<Grid x:Name="imageToExport">
<Image Source ="/Resources/a4.jpg" Grid.Column="0" Margin="10,35,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="Some text here that will appear on top of the image"/><!-- The text property can use binding instead -->
</Grid>
然后你将它保存为 jpeg,如下所示:
Image myImage = new Image();
FormattedText text = new FormattedText("ABC",
new CultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Normal, new
FontStretch()),
this.FontSize,
this.Foreground);
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawText(text, new Point(2, 2));
drawingContext.Close();
RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96,
PixelFormats.Pbgra32);
bmp.Render(drawingVisual);//In here you could just pass the name of the grid "imageToExport"
myImage.Source = bmp;
注意
请注意保存视觉的代码来自MSDN
TA贡献1794条经验 获得超8个赞
问题是我让程序只在单击按钮时编写文本(如您所见Button_Click(),但我想在编写时显示我在 SO Number 文本框上写的文本。有没有办法在我编写文本而不是Button单击事件时刷新窗口上的图像视图?
尝试为 处理TextChanged事件,TextBox而不是为 处理Click事件Button:
<TextBox x:Name="SO" TextChanged="" ... />
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
String orderNum = SO.Text;
using (var stream = File.OpenRead(path))
{
order = (Bitmap)Bitmap.FromStream(stream);
}
using (order)
using (var graphics = Graphics.FromImage(order))
using (f)
{
graphics.DrawString(orderNum, f, System.Drawing.Brushes.White, order.Height / 2, order.Width / 2);
order.Save(path);
}
}
请注意,每次按下按键时都会调用事件处理程序。如果出于性能原因不希望这样做,您可以考虑绑定到string属性并按照此处的建议实现一些延迟。
- 2 回答
- 0 关注
- 554 浏览
添加回答
举报
