【问题标题】:Inverting Image Transparency in ASP.net在 ASP.net 中反转图像透明度
【发布时间】:2009-05-01 01:19:33
【问题描述】:

我正在尝试构建一个允许我获取输入图像并从中生成蒙版的页面。输入将是具有透明背景的索引 PNG。生成的图像将是黑色的,原始图像是透明的,而原始图像是不透明的则是透明的。

我已经在 asp.net 中完成了一些非常基本的图像处理,但我不确定如何进行。我希望有一些比逐个像素更快的解决方案。

如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

    标签: asp.net image-processing


    【解决方案1】:

    【讨论】:

    • 非常感谢这正是我需要的正确方向的推动。我现在有一个可行的解决方案,我将在下面发布。
    【解决方案2】:

    好的,我有一个使用转换的可行解决方案。老实说,我不是 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>
    

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 2011-06-02
      • 2020-01-03
      • 2016-02-27
      • 1970-01-01
      相关资源
      最近更新 更多