private void CDA() { x1 = int.Parse(first_x.Text) + 100; y1 = int.Parse(first_y.Text) + 100; x2 = int.Parse(second_x.Text) + 100; y2 = int.Parse(second_y.Text) + 100; int dx = x2 - x1, dy = y2 - y1, steps, k, xf, yf; float xIncrement, yIncrement, x = x1, y = y1; if (Math.Abs(dx) > Math.Abs(dy)) { steps = Math.Abs(dx); } else { steps = Math.Abs(dy); } xIncrement = dx / (float)steps; yIncrement = dy / (float)steps; PixelFunc func = new PixelFunc(SetPixel); for (k = 0; k < steps; k++) { x += xIncrement; xf = (int)x; y += yIncrement; yf = (int)y; try { pictureBox1?.Invoke(func, xf, yf, Color.Blue); } catch (InvalidOperationException) { return; } } }
/// <summary> /// Execute a function for every pixel in the image and apply changes to the pixel to the image directly. /// </summary> /// <typeparam name="T">the pixel format of the image</typeparam> /// <param name="img">the image to use</param> /// <param name="pixelFunction">the function to execute for every pixel</param> /// <returns>the modified image (equal to img param)</returns> public static Image <T> DirectChangeEachPixel <T>(this Image <T> img, PixelFunc <T> pixelFunction) where T : struct, IPixel <T> { for (int px = 0; px < img.Width - 1; px++) { for (int py = 0; py < img.Height - 1; py++) { img[px, py] = pixelFunction(px, py, img[px, py]); } } return(img); }
/// <summary> /// Execute a function for every pixel in the image and apply changes to the pixel to a copy of the image. /// </summary> /// <remarks>uses Parallel.For to use multi- core- processors better</remarks> /// <typeparam name="T">the pixel format of the image</typeparam> /// <param name="img">the image to use</param> /// <param name="pixelFunction">the function to execute for every pixel</param> /// <param name="disposeOld">if true, the image img is disposed before the function returns the new image</param> /// <returns>the modified image</returns> public static Image <T> ChangeEachPixelParallel <T>(this Image <T> img, PixelFunc <T> pixelFunction, bool disposeOld) where T : struct, IPixel <T> { //create output image Image <T> output = new Image <T>(img.Width, img.Height); //enumerate all pixels Parallel.For(0, img.Width - 1, (px) => { for (int py = 0; py < img.Height - 1; py++) { output[px, py] = pixelFunction(px, py, img[px, py]); } }); //dispose old image if (disposeOld) { img.Dispose(); } return(output); }
private void Render() { int xInitial = 10, yInitial = 20, xFinal = 150, yFinal = 200; int dx = xFinal - xInitial, dy = yFinal - yInitial, steps, k, xf, yf; float xIncrement, yIncrement, x = xInitial, y = yInitial; if (Math.Abs(dx) > Math.Abs(dy)) { steps = Math.Abs(dx); } else { steps = Math.Abs(dy); } xIncrement = dx / (float)steps; yIncrement = dy / (float)steps; PixelFunc func = new PixelFunc(SetPixel); for (k = 0; k < steps; k++) { x += xIncrement; xf = (int)x; y += yIncrement; yf = (int)y; try { pictureBox1.Invoke(func, xf, yf, Color.Blue); } catch (InvalidOperationException) { return; } } }