Altering a bitmap pixel by pixel using an array

I've been creating bitmaps using setpixel.  This is too slow.  After searching the net for days, about the only thing I've learned is that the fastest way is to convert a bitmap into a byte array and alter the array instead, then convert the array back into a bitmap.

The easiest way I've found to change a bitmap into an array is:

Dim bmBytes As Byte() = DirectCast(TypeDescriptor.GetConverter(bitmapTemp).ConvertTo(bitmapTemp, GetType(Byte())), Byte())

I figured I would just study the array and learn how to alter it myself, but bmBytes is a different length from one bitmap to another despite the images having the same dimensions and bpp.  What's going on here?  And they don't even appear long enough, 70,000-90,000 for a 320x240, which is about 1 byte per pixel.  How is a pixel assigned a 32bpp color from 1 byte???

So I'm stuck.  I should point out that I'm not loading a previous image and then altering, but instead creating bitmaps from scratch based on other values.  So, I don't even need to extract an array first, unless I need the header from a new bitmap?  I also HAVE to assign colors using RGB values.

This article is the closest I've found regarding my need, but it involves CImage and I can't figure out how to reference the class.  I'm using VB.NET4

http://www.codeproject.com/KB/graphics/Fast_Image_Manipulation.aspx
IkethAsked:
Who is Participating?
 
IkethConnect With a Mentor Author Commented:
found it here... great article

http://www.bobpowell.net/lockingbits.htm
0
 
CodeCruiserConnect With a Mentor Commented:
If you just want to convert a byte array pixel data to bitmap then check this

http://www.tek-tips.com/viewthread.cfm?qid=1264492&page=10
0
 
IkethAuthor Commented:
Ok good, but I also don't know how to modify the array correctly.  The article I linked is exactly what I'm looking for, but I need the CImage class, mainly for GetPitch I assume.  Do you have a link to do this using the Bitmap class instead?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
CodeCruiserConnect With a Mentor Commented:
0
 
IkethAuthor Commented:
The irony of that link is that the "...." in the first block of code is what I need an example of.

Here is what I need in C++, but this doesn't convert to VB:

    for (int i=0; i<bitmapzor.GetWidth();i++)
        for (int j=0; j<bitmapzor.GetHeight();j++)
        {
            //pointer arithmetics to find (i,j) pixel colors:
            R= *(byteptr+pitch*j+3*i);
            G= *(byteptr+pitch*j+3*i+1);
            B= *(byteptr+pitch*j+3*i+2); 

            //allter pixel G color:
            G=(int)((float)G*1.3);
            if (G>255) G=255;
            //write down the new G-Color
            *(byteptr+pitch*j+3*i+1)=G;
        }

Open in new window

0
 
IkethAuthor Commented:
specifically   *(byteptr+pitch*j+3*i)
0
 
CodeCruiserCommented:
>//pointer arithmetics
Pointers are not supported in .NET
0
 
IkethAuthor Commented:
so i'm forced to use setpixel in VB?
0
 
CodeCruiserCommented:
Looks like that unless you can find a Managed C++ library. I googled but could not find any.
0
 
IkethAuthor Commented:
CodeCruiser, this is my first time to this site... in your opinion, how do you think I should distribute the points?
0
 
CodeCruiserCommented:
My previous article was also about lockbits.
0
 
IkethAuthor Commented:
yea, but there was no mention of how to alter the array... Marshal class does this for VB
0
 
CodeCruiserCommented:
Up to you then regarding points.
0
 
IkethAuthor Commented:
I don't care about the points, I just want to mark this appropriately so others know what the best solution is.
0
 
IkethAuthor Commented:
I chose the wrong post as the solution.
0
 
IkethAuthor Commented:
My post gives the best solution to the question, which was found thanks to the information provided by CodeCruiser.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.