In the course of writing the program for the analysis of diffraction patterns, a dead end arose.
It does not work quickly and correctly with the pixels of the photo.
While there was a prototype, GetPixel suited, but now ... I tried 4 ways. All return an array, but not he is not a bit like.
The last and most sane result is through LockPits (at least the size of the array is correct), but the values ββare not the same (in the center 255 there should be a lot of black and white), but in the end it doesnβt enter one hundred square meters.
This is a function to convert.
public byte[] imageToByteArray(Bitmap bmp) { var pxf = System.Drawing.Imaging.PixelFormat.Format24bppRgb; // ΠΠΎΠ»ΡΡΠ°Π΅ΠΌ Π΄Π°Π½Π½ΡΠ΅ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ. var rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height); //ΠΠ»ΠΎΠΊΠΈΡΡΠ΅ΠΌ Π½Π°Π±ΠΎΡ Π΄Π°Π½Π½ΡΡ
ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² ΠΏΠ°ΠΌΡΡΠΈ BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, pxf); // ΠΠΎΠ»ΡΡΠ°Π΅ΠΌ Π°Π΄ΡΠ΅Ρ ΠΏΠ΅ΡΠ²ΠΎΠΉ Π»ΠΈΠ½ΠΈΠΈ. IntPtr ptr = bmpData.Scan0; // ΠΠ°Π΄Π°ΡΠΌ ΠΌΠ°ΡΡΠΈΠ² ΠΈΠ· Byte ΠΈ ΠΏΠΎΠΌΠ΅ΡΠ°Π΅ΠΌ Π² Π½Π΅Π³ΠΎ Π½Π°Π΄ΠΎΡ Π΄Π°Π½Π½ΡΡ
. // int numBytes = bmp.Width * bmp.Height * 3; //ΠΠ° 3 ΡΠΌΠ½ΠΎΠΆΠ°Π΅ΠΌ - ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ RGB ΡΠ²Π΅Ρ ΠΊΠΎΠ΄ΠΈΡΡΠ΅ΡΡΡ 3-ΠΌΡ Π±Π°ΠΉΡΠ°ΠΌΠΈ //ΠΠΈΠ±ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ Π²ΠΌΠ΅ΡΡΠΎ Width - Stride int numBytes = bmpData.Stride * bmp.Height; int widthBytes = bmpData.Stride; byte[] rgbValues = new byte[numBytes]; // ΠΠΎΠΏΠΈΡΡΠ΅ΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² ΠΌΠ°ΡΡΠΈΠ². Marshal.Copy(ptr, rgbValues, 0, numBytes); return rgbValues; } This is the main code.
ImageBrush ib = new ImageBrush(); var current_bimage = new BitmapImage(new Uri("(gray).jpg", UriKind.RelativeOrAbsolute)); int widthX=current_bimage.PixelWidth; int heightY=current_bimage.PixelHeight; byte[] bData = imageToByteArray(new Bitmap("(gray).jpg")); //System.Drawing.Image asdfa = new Bitmap("./(gray).jpg"); var count_bData=bData.Count(); //List<MyPoint> points = new List<MyPoint>(); byte max=255; Stack<int> X = new Stack<int>(); Stack<int> Y = new Stack<int>(); int ara; int c_pixels = 0; int y; int x; int i = -3; for (y = 0; y < heightY; y++) { x = 0; for (x = 0; x < widthX; x=x+1) { var mid = ( bData[y * x]); // var mid = (30 * bData[y * x] + 59 * bData[y * x + 1] + 11 * bData[y * x + 2]) / 100; //Console.WriteLine(y + " " + x + " " + mid + bData[y * x] + bData[y * x + 1] + bData[y * x + 2]); if (mid == max) { Y.Push(y); X.Push(x); c_pixels++; } } } var midX = X.Sum() / c_pixels; var midY = Y.Sum() / c_pixels;
bmp.PixelFormat? - VladDvar mid = ( bData[y * x]);- wrong. Must bevar midR = bData[y * Stride + x * 3]; var midG = bData[y * Stride + x * 3 + 1]; var midB = bData[y * Stride + x * 3 + 2];var midR = bData[y * Stride + x * 3]; var midG = bData[y * Stride + x * 3 + 1]; var midB = bData[y * Stride + x * 3 + 2];- VladDStridespecifies the size of a single line in bytes . If it worked, I will write in response. - VladD