Problem in 2D array

Discussion in 'C#' started by inspiration, Apr 8, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Hi I have a problem I want to load an image in a 2D array!
    how to?
    :)
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    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:
    http://msdn.microsoft.com/en-us/library/system.drawing.imageconverter.aspx

    Or you could just (didn't test, so fix typos, if any):

    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.
     
  3. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    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.
     
  4. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    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:
    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.
     
  5. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Thanks pankaj!
    :D
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice