猿问

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

最近在用WPF和C#,想做一个A4页面的编辑器(一个A4大小的JPG模板)


问题是,我想在 JPG 上的某个位置放置一些文本,并在我编写文本时能够看到它(如实时预览)。


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


XAML


<Window x:Class="Tut.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:local="clr-namespace:Tut"

    mc:Ignorable="d"

    Title="Neshalet Logo ltd." Height="900" Width="1400">

<Border Padding="10">

    <StackPanel Margin="0,10,0,0">

        <Grid Height="839">

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="*"/>

                <ColumnDefinition Width="*"/>

                <ColumnDefinition Width=".3*"/>


            </Grid.ColumnDefinitions>


            <TextBlock Margin="0,2,22,817" HorizontalAlignment="Right" Grid.Column="2" FontSize="15">

                בחר מוצר

            </TextBlock>


            <!-- Combo box for product type -->

            <ComboBox x:Name="productType" Grid.Column="1"  VerticalAlignment="Top" Height="25" Margin="10,0,17,0"  >

                <ComboBoxItem>באנרים</ComboBoxItem>

                <ComboBoxItem>שקפים וניירות</ComboBoxItem>

                <ComboBoxItem>וינילים</ComboBoxItem>

                <ComboBoxItem>קשיחים הדפסה ישירה</ComboBoxItem>

                <ComboBoxItem>הדבקה</ComboBoxItem>

            </ComboBox>


            <Image Source ="/Resources/a4.jpg" Grid.Column="0" Margin="10,35,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">

            </Image>



    </StackPanel>

</Border>

问题是我让程序只在单击按钮时编写文本(如您所见Button_Click(),但我想在编写时显示我在SO Number文本框上写的文本。有没有办法在我编写文本而不是按钮单击事件时刷新窗口上的图像视图?


FFIVE
浏览 556回答 2
2回答

蓝山帝景

只需绑定 的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}"/>&nbsp;&nbsp;更新在评论和编辑的问题之后。您可以将TextBlock之后Image放在网格中,然后生成包含所有视觉效果的新图像。它会是这样的:<Grid x:Name="imageToExport">&nbsp; &nbsp; <Image Source ="/Resources/a4.jpg" Grid.Column="0" Margin="10,35,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>&nbsp; &nbsp; <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>&nbsp;&nbsp;然后你将它保存为 jpeg,如下所示:Image myImage = new Image();FormattedText text = new FormattedText("ABC",&nbsp; &nbsp; new CultureInfo("en-us"),&nbsp; &nbsp; FlowDirection.LeftToRight,&nbsp; &nbsp; new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Normal, new&nbsp;FontStretch()),&nbsp; &nbsp; this.FontSize,&nbsp; &nbsp; 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,&nbsp;PixelFormats.Pbgra32);bmp.Render(drawingVisual);//In here you could just pass the name of the grid "imageToExport"myImage.Source = bmp;&nbsp;&nbsp;注意请注意保存视觉的代码来自MSDN

慕田峪9158850

问题是我让程序只在单击按钮时编写文本(如您所见Button_Click(),但我想在编写时显示我在 SO Number 文本框上写的文本。有没有办法在我编写文本而不是Button单击事件时刷新窗口上的图像视图?尝试为 处理TextChanged事件,TextBox而不是为 处理Click事件Button:<TextBox x:Name="SO" TextChanged="" ... />private void TextBox_TextChanged(object sender, TextChangedEventArgs e){&nbsp; &nbsp; String orderNum = SO.Text;&nbsp; &nbsp; using (var stream = File.OpenRead(path))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; order = (Bitmap)Bitmap.FromStream(stream);&nbsp; &nbsp; }&nbsp; &nbsp; using (order)&nbsp; &nbsp; using (var graphics = Graphics.FromImage(order))&nbsp; &nbsp; using (f)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; graphics.DrawString(orderNum, f, System.Drawing.Brushes.White, order.Height / 2, order.Width / 2);&nbsp; &nbsp; &nbsp; &nbsp; order.Save(path);&nbsp; &nbsp; }}请注意,每次按下按键时都会调用事件处理程序。如果出于性能原因不希望这样做,您可以考虑绑定到string属性并按照此处的建议实现一些延迟。
随时随地看视频慕课网APP
我要回答