为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
FFIVE 2021-12-25 16:24:48
最近在用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文本框上写的文本。有没有办法在我编写文本而不是按钮单击事件时刷新窗口上的图像视图?
查看完整描述

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


查看完整回答
反对 回复 2021-12-25
?
慕田峪9158850

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属性并按照此处的建议实现一些延迟。


查看完整回答
反对 回复 2021-12-25
  • 2 回答
  • 0 关注
  • 554 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号