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

简介: 原文:使用WPF将图片转变为灰度并加上水印并保存为文件 运行效果: (上图中左下角为原图的缩小显示,By:Johnson为TextBlock)保存的结果图片:上图的“Test Words.”为水印文字。
原文: 使用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();
        }

目录
相关文章
|
C# 前端开发
WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
原文:WPF中Style文件的引用——使用xaml代码或者C#代码动态加载   WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观和行为,如同CSS代码一般。
4518 0
|
6月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
260 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
|
C#
WPF Image Source 设置相对路径图片
原文:WPF Image Source 设置相对路径图片   BitmapImage bt = new BitmapImage(new Uri("Images\\3_u10484.png", UriKind.Relative));this.Img1.Source = bt;
3812 0
|
C#
WPF下载远程文件,并显示进度条和百分比
原文:WPF下载远程文件,并显示进度条和百分比 WPF下载远程文件,并显示进度条和百分比 1、xaml  2、CS程序 using System; using System.
1823 0
|
C#
WPF 调用资源图片
原文:WPF 调用资源图片      最近做的wpf项目中,在开发的时候,把图片放到了bin下面,采用了imagePath =System.IO.Directory.GetCurrentDirectory()+"/images/starShow.
1261 0
|
C#
【C#/WPF】Image图片的Transform变换:平移、缩放、旋转
原文:【C#/WPF】Image图片的Transform变换:平移、缩放、旋转 WPF中图像控件Image的变换属性Transform: 平移 缩放 旋转 即要想实现图片的平移、缩放、旋转,是修改它所在的Image控件的Transform变换属性。
4737 0
|
C#
【C#/WPF】保存BitmapImage数据到文件中
原文:【C#/WPF】保存BitmapImage数据到文件中 参考: http://stackoverflow.
1470 0
|
C#
【C#/WPF】图片的切割/切图/裁剪图片
原文:【C#/WPF】图片的切割/切图/裁剪图片 前台准备两个Image控件。上面是显示原图,下面显示切割后的效果。 对应的后台代码: public par...
2119 0
|
C# 图形学
在WPF里面实现以鼠标位置为中心缩放移动图片
原文:在WPF里面实现以鼠标位置为中心缩放移动图片 在以前的文章使用WPF Resource以及Transform等技术实现鼠标控制图片缩放和移动的效果里面,介绍了如何在WPF里面移动和放大缩小图片,程序也支持使用滚轮的方式缩放图片。
1597 0