码迷,mamicode.com
首页 > Windows程序 > 详细

使用WPF将图片转变为灰度并加上水印并保存为文件

时间:2018-10-25 15:38:54      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:tar   black   之一   des   cti   设置   absolute   drawing   lob   

原文:使用WPF将图片转变为灰度并加上水印并保存为文件

运行效果:
技术分享图片

(上图中左下角为原图的缩小显示,By:Johnson为TextBlock)

保存的结果图片:
技术分享图片
上图的“Test Words.”为水印文字。

XAML代码:
<Window
?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
?xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
?x:Class="AboutColor.FormatConvertedBitmapIndex1"
?x:Name="Window"
?Title="FormatConvertedBitmap"
?Width="640" Height="480">

?<Grid x:Name="LayoutRoot">
??? <Button Height="23" HorizontalAlignment="Right" Margin="0,5,12,0" Name="button1" VerticalAlignment="Top" Width="119" Click="FormatConvertedIndex1Bitmap">ConvertToIndex1</Button>
??? <Image Margin="7,35,8,7" Name="destImage" />
??<Image Margin="10,0,0,9.33299999999997" Source="ZhangLala.jpg" Stretch="Fill" Height="151.667" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="205" />
??<TextBlock Margin="229,0,277,42" x:Name="txtBlock_Watermark" VerticalAlignment="Bottom" Height="Auto" FontFamily="Bauhaus 93" FontSize="22" Text="By: Johnson" TextWrapping="Wrap"/>
? </Grid>
</Window>

C#代码:?(为了方便解释,直接将注释放在代码中)
??????? void FormatConvertedIndex1Bitmap(object sender, RoutedEventArgs e)
??????? {
??????????? BitmapImage myBitmapImage = new BitmapImage();

??????????? // 象BitmapImage的BitmapSource对象,仅在BeginInit/EndInit区块中可以改变它们的属性(区块之外的,不起作用)。
??????????? myBitmapImage.BeginInit();
??????????? myBitmapImage.UriSource = new Uri(@"ZhangLala.jpg", UriKind.RelativeOrAbsolute);
??????????? //为了取得更好的性能(比如节省内存及节约处理时间),设置DecodePixelWidth或/和DecodePixelHeight来指定图片的渲染宽度或/和高度。如果不设定此置,则按正常原尺寸做处理。当你的原始图片特别大时,特别建议你设定图片的这两个属性之一或都设置。当只设定其中之一时,将会根据图片的原始宽高,自动变换另一边的大小,这样图片就不会变形失真。
??????????? //当你两个属性值都设定时,将分别渲染图片至指定的宽度/高度。
??????????? myBitmapImage.DecodePixelWidth = 500;
??????????? myBitmapImage.EndInit();

??????????? // 转换BitmapSource为新格式
??????????? // 注:新的BitmapSource不被缓存。它将在需要时才加载。

??????????? FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();

??????????? // 同样,象FormatConvertedBitmap的BitmapSource对象,仅在BeginInit/EndInit区块中可以改变它们的属性(区块之外的,不起作用)。
??????????? newFormatedBitmapSource.BeginInit();

??????????? // 使用已定义的myBitmapImage作为newFormatedBitmapSource的“源”
??????????? newFormatedBitmapSource.Source = myBitmapImage;

??????????? // 将新图的DestinationFormat属性设置为Gray2图像格式。
??????????? newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray16;

??????????? // 将新图的DestinationFormat属性设置为Indexed1图像格式。
??????????? //newFormatedBitmapSource.DestinationFormat = PixelFormats.Indexed1;
???????????
??????????? // 由于目标格式要被处理成索引像素格式(Indexed1),因此需要设置目标调色板。
??????????? // 下面使用红色和蓝色做为目标调色板
??????????? List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
??????????? colors.Add(System.Windows.Media.Colors.White);
??????????? colors.Add(System.Windows.Media.Colors.DarkGoldenrod);
??????????? BitmapPalette myPalette = new BitmapPalette(colors);

??????????? // 将上面自定义的调色板设置为新图的DestinationPalette属性
??????????? newFormatedBitmapSource.DestinationPalette = myPalette;
???????????
??????????? newFormatedBitmapSource.EndInit();

??????????? // 建立Image元素,并将新图作为“源”
??????????? destImage.BeginInit();
??????????? destImage.Source = newFormatedBitmapSource;
??????????? destImage.EndInit();

??????????? string fileName = @"c:/ConvertImageToGray16.jpg";
??????????? DrawingVisual drawingVisual = new DrawingVisual();
??????????? DrawingContext drawingContext = drawingVisual.RenderOpen();
??????????? int width = 500;
??????????? int height = 400;
??????????? Rect rectangle = new Rect(0, 0, width, height);
??????????? drawingContext.DrawImage(newFormatedBitmapSource, rectangle);
??????????? // 画文字
??????????? FormattedText ft =? new FormattedText("Test Words.",
????????????????? System.Globalization.CultureInfo.GetCultureInfo("en-us"),
????????????????? FlowDirection.LeftToRight,
????????????????? new Typeface("Verdana"),
????????????????? 36, Brushes.Black);

??????????? drawingContext.DrawText(ft, new Point(20, 200));

??????????? //在这里你还可以加图片水印等......
??????????? //drawingContext.DrawImage(yourImageSource, imageRectangle);

??????????? drawingContext.Close();

??????????? // 利用RenderTargetBitmap对象,以保存图片
??????????? RenderTargetBitmap renderBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
??????????? renderBitmap.Render(drawingVisual);

??????????? // 利用JpegBitmapEncoder,对图像进行编码,以便进行保存
??????????? JpegBitmapEncoder encoder = new JpegBitmapEncoder();
??????????? encoder.QualityLevel = 80; // 设置JPEG的质量值
??????????? encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
??????????? // 保存文件
??????????? FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
??????????? encoder.Save(fileStream);
??????????? // 关闭文件流
??????????? fileStream.Close();
??????? }

使用WPF将图片转变为灰度并加上水印并保存为文件

标签:tar   black   之一   des   cti   设置   absolute   drawing   lob   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9849691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!