我正在尝试创建一个水印应用程序,我需要添加文本到图像,我发现这个教程,但我是WP7的新手,谁能说我怎么用?
这是我从here得到的代码:
public static class WriteableBitmapEx
{
/// <summary>
/// Creates a watermark on the specified image
/// </summary>
/// <param name="input">The image to create the watermark from</param>
/// <param name="watermark">The text to watermark</param>
/// <param name="color">The color - default is White</param>
/// <param name="fontSize">The font size - default is 50</param>
/// <param name="opacity">The opacity - default is 0.25</param>
/// <param name="hasDropShadow">Specifies if a drop shadow effect must be added - default is true</param>
/// <returns>The watermarked image</returns>
public static WriteableBitmap Watermark(this WriteableBitmap input, string watermark, Color color = default(Color), double fontSize = 50, double opacity = 0.25, bool hasDropShadow = true)
{
var watermarked = GetTextBitmap(watermark, fontSize, color == default(Color) ? Colors.White : color, opacity, hasDropShadow);
var width = watermarked.PixelWidth;
var height = watermarked.PixelHeight;
var result = input.Clone();
var position = new Rect(input.PixelWidth - width - 20 /* right margin */, input.PixelHeight - height, width, height);
result.Blit(position, watermarked, new Rect(0, 0, width, height));
return result;
}
/// <summary>
/// Creates a WriteableBitmap from a text
/// </summary>
/// <param name="text"></param>
/// <param name="fontSize"></param>
/// <param name="color"></param>
/// <param name="opacity"></param>
/// <param name="hasDropShadow"></param>
/// <returns></returns>
private static WriteableBitmap GetTextBitmap(string text, double fontSize, Color color, double opacity, bool hasDropShadow)
{
TextBlock txt = new TextBlock();
txt.Text = text;
txt.FontSize = fontSize;
txt.Foreground = new SolidColorBrush(color);
txt.Opacity = opacity;
if (hasDropShadow) txt.Effect = new DropShadowEffect();
WriteableBitmap bitmap = new WriteableBitmap((int)txt.ActualWidth, (int)txt.ActualHeight);
bitmap.Render(txt, null);
bitmap.Invalidate();
return bitmap;
}
}
如果我添加到模块然后我收到错误,如下所示:
答案 0 :(得分:3)
这看起来很简单:编辑:不,不是。这是一个兔子洞
从某处获取BitmapImage。 (我们假设它被称为originalBitmap)
将其转换为WriteableBitmap。
var myWritableBitmap = new WriteableBitmap(originalBitmap);
调用方法:
var watermarkBitmap = WriteableBitmapEx.Watermark(myWriteableBitmap, "The Watermark");
使用水印图像执行您需要的操作(保存,显示,无论如何)。
编辑:一些修改。
删除WP7不支持的行if (hasDropShadow) txt.Effect = new DropShadowEffect();
。
在水印方法中,更改此行:
var position = new Rect(input.PixelWidth - width - 20 /* right margin */, input.PixelHeight - height, width, height);
为:
var position = new Rect(20, input.PixelHeight - height, width, height);
然后你应该设置为执行以下操作(告诫emptor - 这不是生产代码。这是slapdash代码,旨在帮助你一路上。如果你使用,你可以免除一生的痛苦这在生产代码中):
XAML:
<phone:PhoneApplicationPage
x:Class="StackOWP7.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="72"/>
<RowDefinition Height="72"/>
</Grid.RowDefinitions>
<Image x:Name="picture" Stretch="UniformToFill"/>
<Button Grid.Row="1" Content="Choose Picture" Click="Button_Click"/>
<Button Grid.Row="2" Content="Watermark Picture" Click="Button_Click_1"/>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
并在CodeBehind:
private void Button_Click(object sender, RoutedEventArgs e)
{
var task = new PhotoChooserTask();
task.Completed += (s, arg) =>
{
if(arg.TaskResult == TaskResult.OK)
{
var bm = new BitmapImage(new Uri(arg.OriginalFileName));
picture.Source = bm;
}
};
task.Show();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var wb = new WriteableBitmap((BitmapSource) picture.Source);
var watermarkedImage = wb.Watermark("watermark", Colors.White, fontSize:50, opacity:.8, hasDropShadow:false);
picture.Source = watermarkedImage;
}