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. GetConvert er(bitmapT emp).Conve rtTo(bitma pTemp, 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
The easiest way I've found to change a bitmap into an array is:
Dim bmBytes As Byte() = DirectCast(TypeDescriptor.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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;
}
ASKER
specifically *(byteptr+pitch*j+3*i)
>//pointer arithmetics
Pointers are not supported in .NET
Pointers are not supported in .NET
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
I don't care about the points, I just want to mark this appropriately so others know what the best solution is.
ASKER
I chose the wrong post as the solution.
ASKER
My post gives the best solution to the question, which was found thanks to the information provided by CodeCruiser.
ASKER