c# - Drawing lines to image from a txt file. Pen thickness is too large. -


let me describe issue taking me more 1 day solve far. inputting series of lines text file , want draw picture them. thing 2 of these lines close (1 pixel distance) problem if pen of thickness is=1 used. see problematic region below:

enter image description here

for information, bounding rectangle of lines in text file representing whole shape follows:

rectangle(xmin, ymin, (xmax - xmin), (ymax - ymin)) =  761, 236, 298, 344 

and trying draw them bitmap(20000, 15000) size of bitmap changed if necessary.

my questions are:

  • is there workaround make pen thickness smaller 1 avoid such overlap?
  • would possible modify input coordinates little bit (some sort of “dilatation”) issue not happen?

otherwise, can think solution solve issue?

thank much,

my code:

using system; using system.io; using system.drawing; using system.drawing.drawing2d; using system.windows.forms; using system.collections.generic; using system.linq; using system.text.regularexpressions;   namespace drawinglinestest { class program {      static void main(string[] args)     {         //define input txt file         system.io.streamreader file = new system.io.streamreader("input.txt");         //define bmp         bitmap bmp = new bitmap(20000, 15000);          graphics g = graphics.fromimage(bmp);         pen blackpen = new pen(color.fromargb(255, 0, 0, 0), 1);           // read file         int counter = 0;         string line;         var listx1 = new list<int>();         var listy1 = new list<int>();         var listx2 = new list<int>();         var listy2 = new list<int>();         var allpoints = new list<point>();           while ((line = file.readline()) != null)         {             string[] points = line.split(',');             int x1 = int.parse(points[0]), y1 = int.parse(points[1]), x2 = int.parse(points[2]), y2 = int.parse(points[3]);             int[] pint = new int[4] { x1, y1, x2, y2 };             listx1.add(x1); listy1.add(y1); listx2.add(x2); listy2.add(y2);              point = new point(int.parse(points[0]), int.parse(points[1]));             point b = new point(int.parse(points[2]), int.parse(points[3]));                             allpoints.add(a); allpoints.add(b);             g.drawline(blackpen, a, b);                             counter++;         }         file.close();         g.dispose();         //-----------------------------------------------------------------------------------------------------------------------------------------------         // find list's bounding box.         ienumerable<point> po = allpoints;         rectangle r = boundingbox(po);         console.writeline(string.format("bounding box {0},{1},{2},{3}", r.x, r.y, r.width, r.height));          bitmap nb = new bitmap(r.width, r.height);         graphics gr = graphics.fromimage(nb);         gr.drawimage(bmp, -r.x, -r.y);          //save input file image (output)         nb.save("outputpicture.png");         //-----------------------------------------------------------------------------------------------------------------------------------------------      }       public static rectangle boundingbox(ienumerable<point> points)     {         var x_query = point p in points select p.x;         int xmin = x_query.min();         int xmax = x_query.max();          var y_query = point p in points select p.y;         int ymin = y_query.min();         int ymax = y_query.max();          return new rectangle(xmin, ymin, (xmax - xmin), (ymax - ymin));        }  }//end program }//end namespace 

some background:

in graphics have deal several coordinate syetems:

  1. world coordinates, example in meters. in case these coordinates in text file
  2. canvas coordinates / bitmap coordinates, in pixels, these pixels in generated image.

then have coordinate-transformation world coordinates canvas coordinates. omitted step, generates problems.

if create coordinate conversion example 1 unit in world -> 10 pixels problems go away.

edit

in case multiplication 10 do:

        point = new point(int.parse(points[0]*10), int.parse(points[1]*10));         point b = new point(int.parse(points[2]*10), int.parse(points[3]*10));     

Comments