Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not sure but i think gray scaled image is a 1d array. I have already used following code and i don't want to use it again.

Please suggest any thing else.
C#
public byte[] convertToArray(Bitmap grayscaledImage)
        {
            
            MemoryStream m1;
            byte[] imagetoArray;
           
            m1 = new MemoryStream();

            grayscaledImage.Save(m1, System.Drawing.Imaging.ImageFormat.Jpeg); m1.Close();
            imagetoArray = m1.ToArray();
            return imagetoArray;
            
        }
Posted
Comments
Sergey Alexandrovich Kryukov 12-May-14 19:44pm    
It depends on what you are using. Generally, if you ask about the class Bitmap, you should specify its exact full type name. But...
—SA
z3m 13-May-14 3:10am    
Sergey! I am sorry to say but I didn't got your point. :(
Sergey Alexandrovich Kryukov 13-May-14 12:06pm    
Please, which part of the expression "exact full type name" (or something else) you did not get?
—SA

1 solution

Please see my comment to the question. But its most likely that you mean System.Drawing.Bitmap and for this class the solution is the use of System.Drawing.Bitmap.LockBits:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].

Clink on the link on the first of the two methods referenced on the page referenced above; it has a good code sample.

Most important thing here is not to try to use GetPixel in a loop, as this method is prohibitively slow. That's all the whole idea of locking bits in memory is about. Note that you need to learn the memory layout for the PixelFormat you're using. See also: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx[^].

—SA
 
Share this answer
 
Comments
CPallini 13-May-14 3:36am    
5.
Sergey Alexandrovich Kryukov 13-May-14 12:02pm    
Thank you, Carlo.
—SA
z3m 13-May-14 4:24am    
I have tried the Bitmap.LockBits method, u can see code down here, but the problem I got is every fourth bit in array is 255 and all thers are 0 like this {0,0,0,255, 0,0,0,255,0,0,0,255}

what could be the reason? please help
"code example is here "
public byte [] returnArray(Bitmap bitmap){
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
IntPtr ptr= bmpData.Scan0;
int bytes= Math.Abs(bmpData.Stride)*bmpData.Height;

byte[] rgbvalues= new byte[bytes];

System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes);
return rgbValues;
}
Sergey Alexandrovich Kryukov 13-May-14 12:05pm    
Again, check your pixel format. Forth byte must be opacity, and I'm not sure the pixel format is grayscale. It could be ARGB format with actual gray pixels...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900