Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a function that returning Bitmap's alpha channel.
VB
Private Function CreateAlphaMap(ByVal Image As Bitmap) As Byte()
    Dim TempBytes(Image.Width * Image.Height - 1) As Byte

    For W As Int32 = 0 To Image.Width - 1
        For H As Int32 = 0 To Image.Height - 1
            TempBytes(H * Image.Width + W) = Image.GetPixel(W, H).A
        Next
    Next
    Return TempBytes
End Function

But I don't think this way is the best way.
Is there have any better ways to do this work more efficiently?
Posted

1 solution

You are quite right in your concern about efficiency. This way is no-go, but it's easy to fix.

I did not pay attention for the algorithm itself, only to one big problem: the use of GetPixel. This method is prohibitively slow. Instead, you should use lock the bitmap data into an array and work with the whole bitmap data in memory. Use System.Drawing.Bitmap.LockBits:
http://msdn.microsoft.com/en-us/library/5ey6h79d%28v=vs.110%29.aspx[^].

In these two MDSN articles, you will find a simple code sample.

—SA
 
Share this answer
 
Comments
SlaneR 10-Dec-13 4:18am    
Thanks for your answer!
Very helpful to me :D
Sergey Alexandrovich Kryukov 10-Dec-13 10:47am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900