C# Problem Loading an image in a 2D array

Thread Starter

San_Dendlek

Joined Mar 24, 2010
11
Hi I have a problem I want to load an image in a 2D array. I would appreciate it if any one knows how. Sorry I cannot give any help but what I have done so far didn't work.

Thanks in advance.
 

Maccara

Joined Aug 22, 2009
8
Since you didn't give out any details (so I do not have a clue what you're going to need the array for), I'm going to just point to: ImageConverter Class

Or you could just (didn't test, so fix typos, if any):
Rich (BB code):
System.Drawing.Image img = System.Drawing.Image.FromFile("someimagefile");
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
byte[] bytes = ms.ToArray();
That'll get you something you could pass to a web service, for example.
 

Thread Starter

San_Dendlek

Joined Mar 24, 2010
11
What I need to do is just put the image in a 2D array and once it is saved in that 2D array I can do whatever it is necessary like you said pass to a web service or for example display it in an LCD display with the use of a micro controller so all I need is to save the image in a 2D array I will try your code and see what happens I will let you know.

Thanks Maccara.
 

Maccara

Joined Aug 22, 2009
8
Hi,

Sorry, I completely missed the 2D part of your previous message, so my answer does not answer your question directly. :)

However, that too is trivial. Depending on your requirements (if it has to be fast, what format etc) there are several possibilities to do what you want.

The simplest would probably be something like this, if you need just raw bitmap data pixel values in 2D array:
Rich (BB code):
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(@"pathto.bmp", true);
byte[,] imgary = new byte[bitmap.Width,bitmap.Height];

int x,y;

for (x = 0; x < bitmap.Width; x++)
{
    for (y = 0; y < bitmap.Height; y++)
    {
        imgary[x,y] = bitmap.GetPixel(x,y);
    }
}
That should work for b&w images only - if you want to handle RGB, you need to separate the planes first etc, but the principle is the same.

Also, I wrote that just quickly now without testing, so there might be some typos (or required casting forgotten) so please test yourself. That also is an inefficient way of doing it, but should work.

Also, if you just need to manipulate some pixels, it might be easier to work with bitmap.Get/SetPixel only and not even convert to array - just convert in the end if you want to upload to webservice etc.

See for example http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx for reference for the bitmap class.

If you want something efficient (in case you want to do FFT etc), you probably should look into using pointer arithmetic instead (requires unmanaged code, so pragmas needed for that) to convert to array form. However, I don't have any idea about your experience level, so I won't delve more into that at this point.
 
Last edited:

Thread Starter

San_Dendlek

Joined Mar 24, 2010
11
The type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference)

what can this error be ?
 

Maccara

Joined Aug 22, 2009
8
Just as the error message says - you need to add a reference to System.Drawing in your project.

(right click the project file (assuming you're using VS) and select "add reference")
 
Top