Search code examples
c#.netsystem.drawing

c# draw - Add random lines in a image


I have an image which I just want to add some random lines in. I am not so familiar with c# draw features.

Is there any simple way to add somewhere between 4-7 different length lines different places in an already bitmap object ?


Solution

  • Try something like this:

    Random rnd = new Random();
    Graphics g = Graphics.FromImage(bitmap);
    for (int i=0; i<n; i++) {
        // calculate line start and end point here using the Random class:
        int x0 = rnd.Next(0, bitmap.Width);
        int y0 = rnd.Next(0, bitmap.Height);
        int x1 = rnd.Next(0, bitmap.Width);
        int y1 = rnd.Next(0, bitmap.Height);
        g.DrawLine(Pens.White, x0, y0, x1, x1);
    }