views:

166

answers:

2

I am trying to build a page that will allow me to take an input image and generate a mask from it. The input would be an indexed PNG with a transparent background. The resultant image would be be black where the original was transparent and transparent wherever the original image was opaque.

I've done some very basic image manipulation in asp.net but i'm unsure of how to proceed. I'm hoping that there is some solution faster than going pixel by pixel.

If anyone can point me in the right direction i would really appreciate it.

+1  A: 

You should probably look into transformations.

http://en.csharp-online.net/GDIplus_Graphics_Transformation%E2%80%94Image_Transformation

Chris Brandsma
Thank you very much That was precisely the push in the right direction I needed. I have a solution working now which i'll post below.
apocalypse9
A: 

Ok I have a working solution using transformations. To be perfectly honest I don't 100% understand what I'm doing with the color matrices - so the way I did this may be less than optimal. Code pasted below in case anyone else runs in to the same problem.

Basically the transformation makes the transparent pixels black and the colored pixels white. I then used MakeTransparent on the white pixels. There should be a way to do this in a single step but it is beyond me today unfortunately

Thanks again chris- I had been searching for hours to find a technique that would work and I hadn't come across anything on this type of transformation.

<%@ 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>
apocalypse9