C# Problem Loading an image in a 2D array

Discussion in 'Programmer's Corner' started by San_Dendlek, Mar 29, 2010.

  1. San_Dendlek

    Thread Starter New Member

    Mar 24, 2010
    11
    0
    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.
     
  2. Maccara

    New Member

    Aug 22, 2009
    8
    0
    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):
    Code ( (Unknown Language)):
    1.  
    2. System.Drawing.Image img = System.Drawing.Image.FromFile("someimagefile");
    3. MemoryStream ms = new MemoryStream();
    4. img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    5. byte[] bytes = ms.ToArray();
    6.  
    That'll get you something you could pass to a web service, for example.
     
  3. San_Dendlek

    Thread Starter New Member

    Mar 24, 2010
    11
    0
    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.
     
  4. Maccara

    New Member

    Aug 22, 2009
    8
    0
    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:
    Code ( (Unknown Language)):
    1.  
    2. System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(@"pathto.bmp", true);
    3. byte[,] imgary = new byte[bitmap.Width,bitmap.Height];
    4.  
    5. int x,y;
    6.  
    7. for (x = 0; x < bitmap.Width; x++)
    8. {
    9.     for (y = 0; y < bitmap.Height; y++)
    10.     {
    11.         imgary[x,y] = bitmap.GetPixel(x,y);
    12.     }
    13. }
    14.  
    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: Apr 1, 2010
  5. San_Dendlek

    Thread Starter New Member

    Mar 24, 2010
    11
    0
    Thanks for the reply Maccara. I will try some and let you know.
     
  6. San_Dendlek

    Thread Starter New Member

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

    what can this error be ?
     
  7. Maccara

    New Member

    Aug 22, 2009
    8
    0
    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")
     
  8. redimage

    New Member

    May 8, 2013
    2
    0
Loading...