首页 > Windows开发 > 详细

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

时间:2018-10-25 15:03:15      阅读:236      评论:0      收藏:0      [点我收藏+]
原文:简述WPF中的图像像素格式(PixelFormats)

--------------------------------------------------------------------------------
引用或转载时请保留以下信息:
大可山?[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();
??????? }

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

显示效果图片如下:
技术分享图片

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)相关内容的更新。

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

原文:https://www.cnblogs.com/lonelyxmas/p/9849689.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!