【发布时间】:2009-05-01 01:19:33
【问题描述】:
我正在尝试构建一个允许我获取输入图像并从中生成蒙版的页面。输入将是具有透明背景的索引 PNG。生成的图像将是黑色的,原始图像是透明的,而原始图像是不透明的则是透明的。
我已经在 asp.net 中完成了一些非常基本的图像处理,但我不确定如何进行。我希望有一些比逐个像素更快的解决方案。
如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】:
我正在尝试构建一个允许我获取输入图像并从中生成蒙版的页面。输入将是具有透明背景的索引 PNG。生成的图像将是黑色的,原始图像是透明的,而原始图像是不透明的则是透明的。
我已经在 asp.net 中完成了一些非常基本的图像处理,但我不确定如何进行。我希望有一些比逐个像素更快的解决方案。
如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】:
【讨论】:
好的,我有一个使用转换的可行解决方案。老实说,我不是 100% 理解我在用颜色矩阵做什么——所以我这样做的方式可能不是最佳的。代码粘贴在下面,以防其他人遇到同样的问题。
基本上,转换使透明像素变黑,彩色像素变白。然后我在白色像素上使用 MakeTransparent。应该有一种方法可以一步完成,但不幸的是,今天我无法做到这一点
再次感谢克里斯-我一直在寻找几个小时来寻找一种可行的技术,但我没有遇到任何关于这种类型的转换。
<%@ page language="vb" contenttype="image/png" %>
<%@ Import Namespace="System.IO" %>
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<%@ import namespace="system.drawing.drawing2d" %>
<script runat="server">
Sub Page_Load()
Dim tmpImage As Bitmap = Bitmap.FromFile(Server.MapPath("test.png"))
Dim input As Bitmap = New Bitmap(tmpImage.Width, tmpImage.Height, PixelFormat.Format32bppArgb)
Dim trans As New ColorMatrix(New Single()() _
{New Single() {0, 1, 1, 1, 0}, _
New Single() {1, 0, 1, 1, 0}, _
New Single() {1, 1, 0, 1, 0}, _
New Single() {1, 1, 1, 1, 0}, _
New Single() {0, 0, 0,255, 1}})
Dim attr As New ImageAttributes
Dim rc As New Rectangle(0, 0, input.Width, input.Height)
Dim out As New memorystream
Dim g As Graphics = Graphics.FromImage(input)
g.Clear(Color.Transparent)
attr.SetColorMatrix(trans, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap)
g.DrawImage(tmpImage, rc, 0, 0, input.Width, input.Height, GraphicsUnit.Pixel, attr)
input.makeTransparent(System.Drawing.Color.White)
input.Save(out, ImageFormat.Png)
g.Dispose()
input.Dispose()
tmpImage.Dispose()
out.WriteTo(Response.OutputStream)
End Sub
</script>
【讨论】: