coversion of image into pixel matrix [modified]
-
How can i convert a color image into pixel matrix using c#.net
modified on Wednesday, March 4, 2009 1:44 AM
rohan4040 wrote:
How can i convert a color image into pixel matrix
By writing code - in the language of your choice.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
-
How can i convert a color image into pixel matrix using c#.net
modified on Wednesday, March 4, 2009 1:44 AM
a slow but easy way to do this is to use the Bitmap class' GetPixel method. I am writing this code as I go and will probably contains error:
class ByteColor
{
public byte R;
public byte G;
public byte B;
}Bitmap myBMP=new Bitmap("myimage.jpg");
int iW=myBMP.Width;
int iH=myBMP.Height;
ByteColor[,] myPixelMatrix=new ByteColor[iW,iH];for(int w=0; w<iw;w++)>
{
for(int h=0; h<ih;h++)>
{
Color tmpColor=myBMP.GetPixel(w,h);byte cR=Convert.toByte(tmpColor.R); byte cG=Convert.toByte(tmpColor.G); byte cB=Convert.toByte(tmpColor.B); myPixelMatrix\[w,h\].R=cR; myPixelMatrix\[w,h\].G=cR; myPixelMatrix\[w,h\].B=cR;
}
}As I said this is a easy but slow way to do it. for some more speed you can use the LockBits and UnlockBits methodes and copy the byte array right from memory like this:
Rectangle rect = new Rectangle(0, 0, myBMP.Width, myBMP.Height);
BitmapData ImageColorData = myBMP.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);unsafe
{
IntPtr pointer = ImageColorData .Scan0;
int Bytesnmpr = ImageColorData .Stride * myBMP.Height;
byte[] ColorValues = new byte[Bytesnmpr];
Marshal.Copy(pointer, ColorValues , 0, Bytesnmpr);for (int i= 0; i< ColorValues .Length; i++) { //copy the byte to your buffer or do what you want with it here } Marshal.Copy(ColorValues , 0, pointer , Bytesnmpr);
}
myBMP.UnlockBits(ImageColorData);this one is much faster but uses unsafe code. also remember, I worte the code on the fly, so there is most likely errors in this code
-
rohan4040 wrote:
How can i convert a color image into pixel matrix
By writing code - in the language of your choice.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
-
If you had magnified between the lines of his post like times 100 you would have seen: "Send me the codes pleezzzzz, it's urgent"
I urgently ignored the subtext.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
a slow but easy way to do this is to use the Bitmap class' GetPixel method. I am writing this code as I go and will probably contains error:
class ByteColor
{
public byte R;
public byte G;
public byte B;
}Bitmap myBMP=new Bitmap("myimage.jpg");
int iW=myBMP.Width;
int iH=myBMP.Height;
ByteColor[,] myPixelMatrix=new ByteColor[iW,iH];for(int w=0; w<iw;w++)>
{
for(int h=0; h<ih;h++)>
{
Color tmpColor=myBMP.GetPixel(w,h);byte cR=Convert.toByte(tmpColor.R); byte cG=Convert.toByte(tmpColor.G); byte cB=Convert.toByte(tmpColor.B); myPixelMatrix\[w,h\].R=cR; myPixelMatrix\[w,h\].G=cR; myPixelMatrix\[w,h\].B=cR;
}
}As I said this is a easy but slow way to do it. for some more speed you can use the LockBits and UnlockBits methodes and copy the byte array right from memory like this:
Rectangle rect = new Rectangle(0, 0, myBMP.Width, myBMP.Height);
BitmapData ImageColorData = myBMP.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);unsafe
{
IntPtr pointer = ImageColorData .Scan0;
int Bytesnmpr = ImageColorData .Stride * myBMP.Height;
byte[] ColorValues = new byte[Bytesnmpr];
Marshal.Copy(pointer, ColorValues , 0, Bytesnmpr);for (int i= 0; i< ColorValues .Length; i++) { //copy the byte to your buffer or do what you want with it here } Marshal.Copy(ColorValues , 0, pointer , Bytesnmpr);
}
myBMP.UnlockBits(ImageColorData);this one is much faster but uses unsafe code. also remember, I worte the code on the fly, so there is most likely errors in this code