Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改图像后在wpf窗口中重新加载图像_C#_Wpf_Image_Xaml_Data Binding - Fatal编程技术网

C# 更改图像后在wpf窗口中重新加载图像

C# 更改图像后在wpf窗口中重新加载图像,c#,wpf,image,xaml,data-binding,C#,Wpf,Image,Xaml,Data Binding,最近我与WPF和C#合作,我想为A4页面(A4大小的JPG模板)制作一个编辑器 问题是,我想把一些文本放在JPG的某个地方,并在我写文本时能够看到它(就像一个实时预览) 这就是我现在所取得的成就: XAML 问题是,我让程序只在单击按钮时写入文本(正如您在按钮上所看到的那样()),但我想在写入文本时显示我在上写入的文本,因此编号文本框。 当我在一个按钮点击事件中写入文本和而不是时,有没有办法刷新窗口上的图像视图 以下是一个例子: 我希望文本框中输入的文本将出现在jpg上 只需绑定TextBloc

最近我与WPF和C#合作,我想为A4页面(A4大小的JPG模板)制作一个编辑器

问题是,我想把一些文本放在JPG的某个地方,并在我写文本时能够看到它(就像一个实时预览)

这就是我现在所取得的成就:

XAML

问题是,我让程序只在单击按钮时写入文本(正如您在
按钮上所看到的那样()
),但我想在写入文本时显示我在上写入的文本,因此编号文本框。 当我在一个按钮点击事件中写入文本和而不是时,有没有办法刷新窗口上的图像视图

以下是一个例子: 我希望文本框中输入的文本将出现在jpg上


只需绑定
TextBlock
Text
属性,即可使用
TextBox
Text

像这样:

<TextBlock Grid.Column="1" FontSize="16" HorizontalAlignment="Right" Margin="0,5,22,472" Text="{Binding ElementName=SO, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>  
然后将其保存为jpeg格式,如下所示:

<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>  
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;  
注意

请注意,保存视觉效果的代码来自

只需绑定
TextBlock
Text
属性即可使用
TextBox
Text

像这样:

<TextBlock Grid.Column="1" FontSize="16" HorizontalAlignment="Right" Margin="0,5,22,472" Text="{Binding ElementName=SO, Path=Text, UpdateSourceTrigger=PropertyChanged}"/>  
然后将其保存为jpeg格式,如下所示:

<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>  
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;  
注意
请注意,保存视觉效果的代码来自

问题是,我让程序只在单击按钮时写入文本(正如您在
按钮_click()
上看到的,但我想在写入文本时显示我在文本框中写入的文本。有没有办法在我写入文本时刷新窗口中的图像视图,而不是在
按钮
单击事件中

尝试处理
TextBox
TextChanged
事件,而不是处理
按钮的
Click
事件:

<TextBox x:Name="SO" TextChanged="" ... />

注意,事件处理程序将在每个按键上调用。如果这不是出于性能原因而需要的,则可以考虑绑定到<代码>字符串属性,并按照建议执行一些延迟。 问题是,我让程序只在单击按钮时写入文本(正如您在

按钮_click()
上看到的,但我想在写入文本时显示我在文本框中写入的文本。有没有办法在我写入文本时刷新窗口中的图像视图,而不是在
按钮
单击事件中

尝试处理
TextBox
TextChanged
事件,而不是处理
按钮的
Click
事件:

<TextBox x:Name="SO" TextChanged="" ... />

注意,事件处理程序将在每个按键上调用。如果这不是出于性能原因而需要的,则可以考虑绑定到<代码>字符串属性,并按照建议执行一些延迟。

如果您使用绑定,则可以将<代码>文本< /代码>值绑定到<代码>文本块 > <代码>文本/COD。e>
TextBox
的属性。问题已解决,请确保设置了
UpdateSourceTrigger=PropertyChanged
如果您要使用绑定,则可以将
TextBlock
Text
值绑定到
TextBox
Text
属性。问题已解决,请确保设置了
updatesource>igger=PropertyChanged
我想我被误解了..你给出的解决方案改变了文本块,我想在图像上粘贴一个文本,在文本框内粘贴相同的文本。编辑了我的问题,请看一下:)更新了我的答案,我想这就是你要找的:-)我想我被误解了。你给出的解决方案改变了文本块,我想在图像上粘贴一个文本,在文本框内粘贴相同的文本。编辑了我的问题,请看一下:)更新了我的答案,我想这就是你要找的:-)这是我一直在找的评论!你抓住了我的心上人!但有两个问题:1。当输入超过1个字符2时,它会给我一个异常。您发布的链接已断开:(@t0nty:link已修复。您会遇到什么样的异常?System.ArgumentException:'参数无效。'在线图形.DrawString(orderNum,f,System.Drawing.Brush.White,order.Height/2,order.Width/2);嗯,这是和以前一样的代码。你需要调试并找出正常调用和失败调用之间的区别。明白了,第一个字母是整个字符串,如果我再添加一个字符,程序就会崩溃,那么延迟应该会起作用吗?这是我一直在寻找的注释!你有我的思维伴侣!但是有两个问题:1.当键入超过1个字符时,它会给我一个异常2.您发布的链接已断开:(@t0nty:link已修复。您会遇到什么样的异常以及出现在哪里?System.ArgumentException:'参数无效。'在线图形.DrawString(orderNum,f,System.Drawing.Brush.White,order.Height/2,order.Width/2);嗯,这是和以前一样的代码。您需要调试并找出正常调用和失败调用之间的区别。明白了,它将第一个字母作为整个字符串,如果我再添加一个字符,程序就会崩溃,那么延迟应该会行得通吗?