将 ColorMatrix 应用于 .Net、WinForms 中的图像时出现奇怪的行为
下面的方法采用颜色矩阵并将其应用到提供的图像。有几点需要注意:
(1)它不是一个函数
(2) 相同的图像用于创建图形对象,并作为DrawImage 方法的源。
Public Sub ApplyMatrixToImage(ByVal matrix As ColorMatrix, ByVal image As Image)
Using atts As New ImageAttributes
atts.SetColorMatrix(matrix)
Using g As Graphics = Graphics.FromImage(image)
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim rect As New Rectangle(0, 0, width, height)
g.DrawImage(image, rect, 0, 0, width, height, GraphicsUnit.Pixel, atts)
End Using
End Using
End Sub
我不知道不创建另一个位图来渲染最终图像是否是不好的做法,但奇怪的是该方法对于颜色平衡调整(Matrix30、31 和 32)效果很好,但对于不透明度调整没有任何作用(矩阵33)。
这是怎么回事?
The method below, takes a colour matrix and applies it to the supplied image. There are a couple of things to note:
(1) It is not a function
(2) The same image is used to create the graphics object, and as the source of the DrawImage method.
Public Sub ApplyMatrixToImage(ByVal matrix As ColorMatrix, ByVal image As Image)
Using atts As New ImageAttributes
atts.SetColorMatrix(matrix)
Using g As Graphics = Graphics.FromImage(image)
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim rect As New Rectangle(0, 0, width, height)
g.DrawImage(image, rect, 0, 0, width, height, GraphicsUnit.Pixel, atts)
End Using
End Using
End Sub
I don't know if it's bad practice not to create another bitmap to render the final image into, but the strange thing is the method works fine for a colour balance adjustment (Matrix30, 31 and 32), but does nothing for an opacity adjustment (Matrix33).
What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解你的问题:你是在问为什么不能用这种方法改变 alpha 通道? (为什么它应该是一个函数而不是一个子函数,我完全无法理解。)
但是为什么它不能像你所期望的不透明/透明一样工作,我完全理解。 :-)
.DrawImage 方法(与 ImageAttributes 结合)会将每个更改的像素绘制到其自身上(因为宽度和高度相同)。请注意,它将绘制 - 而不是替换。这意味着原始像素值将与新计算的像素值混合。除其他外,这意味着如果原始像素完全不透明,则无法改变。在不透明的物体上使用部分透明的颜色进行绘画仍然会产生不透明的颜色。
If I understand your question correctly: you are asking why you cannot alter the alpha channel with this method? (Why it should be a function instead of a sub elludes me completely.)
But why it doesn't work as you may expect with opactiy/transparency I understand perfectly. :-)
The .DrawImage method (combined with the ImageAttributes) will DRAW each altered pixel onto itself (since the width and height are the same). Note that it will draw - not replace. This means that the original pixel value will blend with the newly calculated pixel value. Among other things, this means that if the original pixel if completely opaque, there is no way that that will change. Painting with a partially transpararent color over something opaque will still yieald an opaque color.