tags:

views:

38

answers:

1

Hi, I have 3 bitmap images each of around 28-30 MB. My application needs to access pixels from these images frequently.

Currently I am creating BitmapData of all 3 files, locking them and then reading all the pixels in 3 different byte[]'s. Following is the code for one image:

System.Drawing.Imaging.BitmapData bmpData = result.LockBits(new System.Drawing.Rectangle(0, 0, result.Width , result.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite,((System.Drawing.Bitmap)Properties.Resources.bmp1).PixelFormat);
int pixelBytes = System.Drawing.Image.GetPixelFormatSize(((System.Drawing.Bitmap)Properties.Resources.silk_box).PixelFormat) / 8;
System.IntPtr ptr = bmpData.Scan0;
int size = bmpData.Stride * result.Height;
byte[] pixels = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, size);

All the 3 bitmaps are added to the application resources.

Coming to the problem... Although the above method is working fine. The only problem is the performance. It takes around 10-12 seconds for converting the bitmap to byte[] and accessing the specific pixel.

What I guess is the following line which is taking much time:

System.Runtime.InteropServices.Marshal.Copy(ptr, pixels, 0, size);

For the purpose, I am thinking to add the byte[] of all the 3 images to the application resources so that it can be directly accessed and conversion of the images to byte[] will be not required. The performance should increase then.

Will the above method will to the trick?

I need to know that how to convert the images to byte[] and then add them to application resources?

Is there any other solution to the problem?

Thanks

+3  A: 

What I'd do is:

Add the bmp file directly as a file resource of your dll/exe. Then you can read the BMP header from the file. That's not too much work, especcially if you don't need to support all possible BMP format variations. If you want to read individual pixels you can Seek in the file resource to the appropriate index. Otherwise you can read the entire array in memory from the resource file.

jdv
thanks. I got the idea!
Vinod Maurya