惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog
Google Online Security Blog
Google Online Security Blog
NISL@THU
NISL@THU
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Engineering at Meta
Engineering at Meta
AWS News Blog
AWS News Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Privacy International News Feed
B
Blog
PCI Perspectives
PCI Perspectives
Martin Fowler
Martin Fowler
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
T
Tenable Blog
F
Fortinet All Blogs
G
GRAHAM CLULEY
V2EX - 技术
V2EX - 技术
C
Check Point Blog
Project Zero
Project Zero
P
Palo Alto Networks Blog
J
Java Code Geeks
W
WeLiveSecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Exploit Database - CXSecurity.com
博客园 - 司徒正美
P
Privacy & Cybersecurity Law Blog
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
Forbes - Security
Forbes - Security
C
Cybersecurity and Infrastructure Security Agency CISA
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Recent Announcements
Recent Announcements
博客园 - Franky
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
量子位
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - Hotcan

Microsoft Azure Project Oxford 体验 使用CSDN Code将网站部署到Windows Azure Website上 各种云计算虚拟机大比拼 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage 云计算里AWS和Azure的探究(5) ——EC2和Azure VM磁盘性能分析 Windows Azure Overview 云计算里AWS和Azure的探究(4) 云计算里AWS和Azure的探究(3) 云计算里AWS和Azure的探究(2.1) 云计算里AWS和Azure的探究(1) 云计算里AWS和Azure的探究(2) GDI+中常见的几个问题(11) GDI+中常见的几个问题(9) GDI+中常见的几个问题(8) GDI+中常见的几个问题(8.外传1) GDI+中常见的几个问题(7) GDI+中常见的几个问题(6) GDI+中常见的几个问题(5) GDI+中常见的几个问题(4)
GDI+中常见的几个问题(10)
Hotcan · 2008-11-26 · via 博客园 - Hotcan

10. Graphics的几个属性。

今天我来讲讲Graphics在DrawImage里的几个的属性。

Graphics是GDI+里面的大拿,可以用来画线,画矩形,甚至可以用来画各种各样的材质。通过不同的Pen,Brush来实现。具体的使用方法是所有想用GDI+的同学的基础,我就不详细讲了,具体可以参考MSDN:http://msdn.microsoft.com/en-us/library/haxsc50a(VS.80).aspx。我主要来讲2个大家不太注意的属性。

a.Graphics.CompositingMode

这是一个枚举属性,可以取的值有2种,一种是SourceOver, 另外一种是SourceCopy。这定义了Graphics如何将当前颜色和背景合成。如果是SourceCopy,那么颜色不和当前背景合成。如果是SourceOver,那么背景颜色会和当前的颜色混合,算法如下:

显示颜色 = 源颜色 × alpha / 255 + 背景颜色 × (255 - alpha) / 255

新颜色的透明分量是255,也就是不透明。我们来看看下面的代码:

        private int CompositeColor(int color, int alpha, int backgroudColor)
        {
            
//显示颜色 = 源颜色 × alpha / 255 + 背景颜色 × (255 - alpha) / 255
            return color * alpha / 255 + backgroudColor * (255 - alpha) / 255;
        }
        
private void Draw(object sender, EventArgs e)
        {
            
this.BackColor = Color.FromArgb(255255255);
            Graphics g 
= this.CreateGraphics();
            g.CompositingMode 
= System.Drawing.Drawing2D.CompositingMode.SourceOver;
            g.FillRectangle(
new SolidBrush(Color.FromArgb(12725500)), new Rectangle(00200200));

            g.CompositingMode 

= System.Drawing.Drawing2D.CompositingMode.SourceCopy;
            g.FillRectangle(
new SolidBrush(Color.FromArgb(12725500)), new Rectangle(2000200200));

            
            g.CompositingMode 

= System.Drawing.Drawing2D.CompositingMode.SourceCopy;
            g.FillRectangle(
new SolidBrush(Color.FromArgb(
                
255,
                CompositeColor(
255127255),
                CompositeColor(
0127255),
                CompositeColor(
0127255))
                ), 
new Rectangle(0200200200));
          
            g.Dispose();           
        }

第1个色块和第2个色块分别是混合和不混合的,如果我们用不混合的方式希望得到混合的效果,那么应该用第三个色块的写法。从下面的图像中我们可以很清楚地看到结果。

b.Graphics.CompositingQuality

合成质量,一共有5种

  成员名称 说明
AssumeLinear 假定线性值。 
Default 默认质量。 
GammaCorrected 使用灰度校正。 
HighQuality 高质量、低速度复合。 
HighSpeed 高速度、低质量。 
Invalid 无效质量。 

这部分东西稍有点学问,MSDN里面没怎么讲清楚,有些实践派的同学用了其他的几个相关的属性来解释GDI+中的图像质量,例如http://www.cnblogs.com/adow/archive/2007/10/05/914573.html,不过不得精髓。我来解释一下图像合成的一些理论基础。这里还需要和另外一个属性InterpolationMode加以区分。这个属性的具体使用我会在下一节讲到,而合成质量与插值不是一回事。

根据我们上一节的算法,图像的合成是浮点运算,计算量非常大。此外,由于图像存储最后是需要被量化的,所以在量化的过程中会不可避免地出现锯齿的情况,为了平滑锯齿,又需要大量的计算。还有一个问题,如果我们有很多层不同的透明图像,需要进行合成,那么每一层都需要进行合成运算。其实这种合成运算式可以被优化的。CompositingQuality这个属性就是GDI+用来解决这些问题的。MSDN里面只是简单地说质量越高速度越慢,具体的算法不得而知。

HighQuality使用平滑技术去除在合成中出现的锯齿,并合成当前的Gamma灰度信息,这种计算是最慢的,并且出来的颜色与非GammaCorrected是不一样的。

GammaCorrected 合成当前Gamma灰度信息,但是不进行计算优化。

HighSpeed优化计算速度,出来的质量稍微有点差,如果不是对质量要求很高时看不出来的。

AssumeLinear的质量比Default稍好,速度稍慢,这种算法是假定合成中插值的像素变化是线性的。

Default就是最基本的计算方法。

Invalid未知,我也不知道,要是有知道的朋友可以告诉我。

其中HighQuality/GammaCorrected效果一样,其余四种一样。可以参考下图。