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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
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
CodeCruiserCommented:
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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:
found it here... great article

http://www.bobpowell.net/lockingbits.htm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.