简述WPF中的图像像素格式(PixelFormats)

123 篇文章 2 订阅
116 篇文章 9 订阅

--------------------------------------------------------------------------------
引用或转载时请保留以下信息:
大可山 [MSN:a3news(AT)hotmail.com]
http://www.zpxp.com http://www.brawdraw.com
萝卜鼠在线图形图像处理
--------------------------------------------------------------------------------

WPF中,图像的像素格式较之于GDI+中有不少变化。比如新增了CMYK印刷通道的支持,对灰度图片的灰阶支持也有长足的进步,在颜色空间方面,新增了scRGB颜色空间,使图像的色彩处理能力有很大的提高。

为了方便加深了解,我将它们按格式性质和色彩空间大致进行了分类。比如将Gray类的放在一起,将Index类的又放在一起....

(1) BlackWhite
用于显示黑白两种色值的像素格式(非黑即白)。

(2) Gray2
2BPP(Bits Per Pixel,位/像素)的灰色通道。允许四种灰阶。

(3) Gray4
4BPP的灰度通道,允许16种灰阶值表示灰色。

(4) Gray8
显示8BPP的灰度通道,允许256种灰阶值表示灰色。

(5) Gray16
16BPP的灰色通道,最多允许65536种灰阶值表示灰色。这种格式的Gamma是1.0。

(6) Gray32Float
32BPP的灰度通道,允许超过40亿灰阶。此格式的Gamma值是1.0。

(7) Indexed1
指定2种颜色作为调色板的像素格式。

(8) Indexed2
指定4种颜色作为调色板的像素格式。

(9) Indexed4
指定16种颜色作为调色板的像素格式。

(10) Indexed8
指定256种颜色作为调色板的像素格式。

(11) Bgr24
Bgr24像素格式是一种采用24BPP的sRGB格式。 每个颜色通道(蓝色blue, 绿色green, 红色red)各占8BPP(位/像素)。

(12) Bgra32
Bgra32像素格式是一种32BPP的sRGB格式。每个颜色通道(蓝色blue, 绿色green, 红色red)各占8BPP(位/像素),与Bgr24不同的是,它还有用于表现不透明度的alpha通道(8BPP)。

(13) Bgr101010
Bgr101010像素格式是一种采用32BPP(位/像素)的sRGB格式。每个颜色通道(蓝色blue, 绿色green, 红色red)各占10BPP(位/像素)。

(14) Bgr32
Bgr32像素格式是一种采用32BPP(位/像素)的sRGB格式。与Bgr101010格式不同的是,它的每个颜色通道(蓝色blue, 绿色green, 红色red)各占8BPP(位/像素)。

(15) Bgr555
Bgr555也是一种sRGB格式,它采用16BPP(位/像素). 它的每个颜色通道(蓝色blue, 绿色green, 红色red)各占5BPP(位/像素)。

(16) Bgr565
Bgr565像素格式是一种16BPP(位/像素)的sRGB格式。它的每个颜色通道(蓝色blue, 绿色green, 红色red)分别占5BPP,6BPP,5BPP(位/像素)。

(17) Pbgra32
采用32BPP的一种基于sRGB的像素格式。每个颜色通道(蓝色blue, 绿色green, 红色red,Alpha通道)各占8BPP(位/像素)。每种颜色通道是经过与Alpha值预处理之后的。

(18) Prgba64
是一种基于sRGB格式,采用64BPP。每个颜色通道(蓝色blue, 绿色green, 红色red,Alpha通道)各占32BPP(位/像素)。每种颜色通道是经过与Alpha值预处理之后的。这种格式的Gamma是1.0。

(19) Rgb24
是一种基于sRGB格式,采用24BPP。每个颜色通道(蓝色blue, 绿色green, 红色red)各占8BPP(位/像素)。

(20) Rgb48
是一种基于sRGB格式,采用48BPP。每个颜色通道(蓝色blue, 绿色green, 红色red)各占16BPP(位/像素)。这种格式的Gamma是1.0。

(21) Rgba64
是一种基于sRGB格式,采用64BPP。每个颜色通道(蓝色blue, 绿色green, 红色red,Alpha通道)各占16BPP(位/像素)。这种格式的Gamma是1.0。 

(22) Rgb128Float
是一种基于ScRGB格式,采用128BPP。每个颜色通道各占32BPP(位/像素)。这种格式的Gamma是1.0。

(23) Rgba128Float
是一种基于ScRGB格式,采用128BPP。每个颜色通道(蓝色blue, 绿色green, 红色red,Alpha通道)各占32BPP(位/像素)。这种格式的Gamma是1.0。

(24) Prgba128Float
是一种基于ScRGB格式,采用128BPP。每个颜色通道(蓝色blue, 绿色green, 红色red,Alpha通道)各占32BPP(位/像素)。每种颜色通道是经过与Alpha值预处理之后的。这种格式的Gamma是1.0。

(25) Cmyk32
用于表现印刷色格式,采用32BPP,共四个颜色通道即C、M、Y、K(青色Cyan, 品红Magenta, 黄色Yellow和黑色blacK),各占8PP。

如果你深入看看System.Windows.Media 下PixelFormats的公共属性时,你还会发现有一个PixelFormats.Default:它是用于系统根据不同的条件,选用最合适的像素格式操作(由系统判断)。

使用如下代码可以列出PixelFormats的一些细节:

        StringBuilder sb = new StringBuilder();
        void ListPixelFormats(object sender, RoutedEventArgs e)
        {
            foreach (PropertyInfo pi in typeof(PixelFormats).GetProperties())
            {
                PixelFormat myPixelFormat = (PixelFormat)pi.GetValue(pi, null);
                if (myPixelFormat != PixelFormats.Default)
                {
                    int bpp = myPixelFormat.BitsPerPixel;

                    sb.AppendLine("PixelFormats." + myPixelFormat.ToString());
                    sb.AppendLine("BPP:" + bpp.ToString());
                    /*
                    IList<PixelFormatChannelMask> myChannelMaskCollection = myPixelFormat.Masks;

                    foreach (PixelFormatChannelMask myMask in myChannelMaskCollection)
                    {
                        sb.AppendLine(myMask.ToString());
                        IList<byte> myBytesCollection = myMask.Mask;
                        foreach (byte myByte in myBytesCollection)
                        {
                            sb.AppendLine(myByte.ToString());
                        }
                    }
                    */
                }
                else
                {
                    sb.AppendLine("PixelFormats.Default");
                }

                sb.AppendLine("---------------------------------------");

            }

            this.txtBoxPixelFormatsList.Text = sb.ToString();
        }

下面给出一个将图像转换为灰色图片并在窗体中显示出来的例子:

显示效果图片如下:
WPF彩色图像转灰度图片

XAML代码:
<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="ButtonSimple.PixelFormatsDemo"
 x:Name="Window"
 Title="PixelFormatsDemo"
 Width="400" Height="600">
<Canvas>
   <Canvas.Resources>
    <BitmapImage x:Key="masterImage" DecodePixelHeight="280"
     UriSource="33.jpg"/>
    <FormatConvertedBitmap x:Key="convertFormatImage" Source="{StaticResource masterImage}"
     DestinationFormat="Gray32Float" />    
  </Canvas.Resources>
 <Image Height="280" Canvas.Left="8" Canvas.Top="18" Source="33.jpg" Stretch="Fill"/>
 <Image Height="280" Canvas.Left="8" Canvas.Top="300" Source="{StaticResource convertFormatImage}" Stretch="Fill"/>
  </Canvas>
</Window>

上面是原始图片的显示效果,下面是变成Gray32Float像素格式后的样子。

C#代码:
        //将图片转灰度
        public FormatConvertedBitmapExample()
        {
            BitmapImage myBitmapImage = new BitmapImage();
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(@"33.jpg",UriKind.Relative);
            myBitmapImage.DecodePixelHeight = 280;
            myBitmapImage.EndInit();
            FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
            newFormatedBitmapSource.BeginInit();
            newFormatedBitmapSource.Source = myBitmapImage;
            newFormatedBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
            newFormatedBitmapSource.EndInit();
            Image myImage = new Image();
            myImage.Height = 280;
            myImage.Source = newFormatedBitmapSource;
            // 其他代码
        }

从上面代码看,WPF中对图像格式的转换显然比在GDI+中方便很多,代码量可用“超少”来形容。原因在于WPF中已经帮我们实现了大部分格式转换的功能。

有时间我再将它们之间的图像像素格式转换写一些例子,分别讲解,这样更能加深图像颜色处理方面的印象。敬请关注我BLOG(http://blog.csdn.net/johnsuna)相关内容的更新。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值