2009-05-01 97 views
1

我正在嘗試構建一個頁面,該頁面允許我拍攝輸入圖像並從中生成一個蒙版。輸入將是具有透明背景的索引PNG。在原始圖像不透明的任何地方,原始圖像都是透明且透明的,所得圖像將爲黑色。在ASP.net中反轉圖像透明度

我在asp.net中做了一些非常基本的圖像處理,但我不確定如何繼續。我希望有一些解決方案比逐像素更快。

如果任何人都可以指出我正確的方向,我會非常感激。

回答

0

好吧,我有使用轉換的工作方案。說實話,我並不是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>