image manipulation

T

Telmo Costa

Hi.
I'm trying to load an image and copy it to an array of bytes.
The image is a png 32bit format. The code I have is:

-------------------------------------------------------------
public void LoadImage(path) {
Bitmap image = new Bitmap(path);
byte[] pixels = new byte[image.Width*image.Height*4];
int offset;

for (int x=0; x<image.Width; x++) {
for (int y=0; y<image.Height; y++) {
Color c = image.GetPixel(x, y);
offset = (y*image.Width + x)*4;
pixels[offset] = c.R;
pixels[offset+1] = c.G;
pixels[offset+2] = c.B;
pixels[offset+3] = c.A;
)
);
}
 
M

Morten Wennevik

Hi Telmo,

It would be much faster to use an unsafe code block and access the raw pixel data.
Bob Powell's article should get you started.

"Using the LockBits method to access image data"
http://www.bobpowell.net/lockingbits.htm


Hi.
I'm trying to load an image and copy it to an array of bytes.
The image is a png 32bit format. The code I have is:

-------------------------------------------------------------
public void LoadImage(path) {
Bitmap image = new Bitmap(path);
byte[] pixels = new byte[image.Width*image.Height*4];
int offset;

for (int x=0; x<image.Width; x++) {
for (int y=0; y<image.Height; y++) {
Color c = image.GetPixel(x, y);
offset = (y*image.Width + x)*4;
pixels[offset] = c.R;
pixels[offset+1] = c.G;
pixels[offset+2] = c.B;
pixels[offset+3] = c.A;
)
);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top