原文:使用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将图片转变为灰度并加上水印并保存为文件的更多相关文章

  1. WPF 将图片进行灰度处理

    原文:WPF 将图片进行灰度处理 处理前:      处理后:   这个功能使用使用了 FormatConvertedBitmap(为BitmapSource提供像素格式转换功能)   代码如下:   ...

  2. Halcon WPF C#采集图像区域灰度值

    源码下载地址:https://github.com/lizhiqiang0204/ImageGray.git Halcon代码如下: *读取图片,转换成灰度图片 read_image (Image1, ...

  3. C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试(续)

    介绍 本文是接着上文<C# WPF 显示图片和视频显示 EmuguCv.AForge.Net测试>写的,建议先看下上文,因为有些代码还需要了解. 增添 接着上文的代码,我们可以在事件处理方 ...

  4. WPF 中图片的加载 ,使用统一资源标识符 (URI)

    在wpf中,设置图片路径有2种方法: 1.xaml文件中,指定路径 <Button Name=" HorizontalAlignment="Right" Verti ...

  5. WPF显示图片

    1.WPF显示图片内部一部分 <Rectangle Height="> <Rectangle.Fill > <ImageBrush ImageSource=&q ...

  6. Silverlight或WPF动态绑定图片路径问题,不用Converter完美解决

    关于Silverlight或WPF动态绑定图片路径问题,不用Converter完美解决, 可想,一个固定的字符串MS都能找到,按常理动态绑定也应该没问题的,只需在前面标记它是一个Path类型的值它就能 ...

  7. OprenCV学习之路一:将彩色图片转换成灰度图

    //将一张彩色图片转成灰度图: //////////////////////////// #include<cv.h> #include<cvaux.h> #include&l ...

  8. WPF 修改图片颜色

    原文:WPF 修改图片颜色 本文告诉大家如何修改图片的颜色,如去掉图片的蓝色 在 WPF 可以使用很多图片处理的方法,本文告诉大家的是一个图片处理,可以把处理的图片保存在文件. 在阅读本文,我假设大家 ...

  9. WPF 把图片分割成两份自动翻页 WpfFlipPageControl:CtrlBook 书控件

    原文:WPF 把图片分割成两份自动翻页 WpfFlipPageControl:CtrlBook 书控件 版权声明:本文为博主原创文章,需要转载尽管转载. https://blog.csdn.net/z ...

随机推荐

  1. js 99乘法表

    哈哈哈,笑死我了,突然怀念学习时代,撸了一个乘法表 for(let a=1;a<10;a++){let str = ''; for(let b=1;b<10;b++){ str = str ...

  2. 【苦读官方文档】2.Android应用程序基本原理概述

    官方文档原文地址 应用程序原理 Android应用程序是通过Java编程语言来写.Android软件开发工具把你的代码和其它数据.资源文件一起编译.打包成一个APK文件,这个文档以.apk为后缀,保存 ...

  3. js课程 1-5 js如何测试变量的数据类型

    js课程 1-5 js如何测试变量的数据类型 一.总结 一句话总结:用typeof()方法. 1.js如何判断变量的数据类型? 用typeof()方法. 13 v=10; 14 15 if(typeo ...

  4. 在线算法与离线算法(online or offline)

    1. 在线算法(online) PFC(prefix-free code)编码树的解码过程:可以在二进制编码串的接收过程中实时进行,而不必等到所有比特位都到达后才开始: 2. 离线算法(offline ...

  5. JasperReport html 导出

    In my last blog post I discussed about Generating jasper reports in different formats using json fil ...

  6. [RxJS] Use RxJS concatMap to map and concat high order observables

    Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this le ...

  7. [Grid Layout] Use auto-fill and auto-fit if the number of repeated grid tracks is not to be def

    What about the situation in which we aren’t specifying the number of columns or rows to be repeated? ...

  8. BZOJ 2096 Pilots - 单调队列STL(deque)

    传送门 分析: 单调队列:维护两个递增.递减的队列,每次都加入新元素并更新,如果最大值(递减队首)-最小值(递增队首) > k,那么将最左段更新为前面两者中较前的那一个,并弹掉.用deque可以 ...

  9. js进阶 9-5 js如何确认form的提交和重置按钮

    js进阶 9-5 js如何确认form的提交和重置按钮 一.总结 一句话总结: 1.这个并不好做:onsubmit 里面的代码必须返回false才能取消onsubmit方法的执行,所以,有return ...

  10. Erlang Module and Function

    Module   -module(Name). 模块是方法的集合.注意这行最后的“.”符号是必不可少的. 这个模块名必须和保存这段代码的文件(后缀为“erl”的文件)有相同的名称. 当我们在使用另一个 ...