Link to home
Start Free TrialLog in
Avatar of Iketh
Iketh

asked on

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
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Iketh
Iketh

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Iketh

ASKER

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

Avatar of Iketh

ASKER

specifically   *(byteptr+pitch*j+3*i)
>//pointer arithmetics
Pointers are not supported in .NET
Avatar of Iketh

ASKER

so i'm forced to use setpixel in VB?
Looks like that unless you can find a Managed C++ library. I googled but could not find any.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Iketh

ASKER

CodeCruiser, this is my first time to this site... in your opinion, how do you think I should distribute the points?
My previous article was also about lockbits.
Avatar of Iketh

ASKER

yea, but there was no mention of how to alter the array... Marshal class does this for VB
Up to you then regarding points.
Avatar of Iketh

ASKER

I don't care about the points, I just want to mark this appropriately so others know what the best solution is.
Avatar of Iketh

ASKER

I chose the wrong post as the solution.
Avatar of Iketh

ASKER

My post gives the best solution to the question, which was found thanks to the information provided by CodeCruiser.