Question

For this problem, I did comb through Stackoverflow similar questions for an answer. Although a lot of them were helpful, they didn't solve my problem. My program draws polygon on a winform using graphics.DrawLines method as follows.

g.DrawLines(thepen,pts);

But that keeps raising, "Parameter is not valid", error. So, I changed that line of code as follows to see if it makes any difference.

g.DrawLines(new pen(color.Black),pts);

Again, it is raising the same error. pts is an array of system.drawing.point and thepen is a system.drawing.pen.

If I completely comment it out, my program has no issue that it doesn't raise any error. However, what's weird is that the same code used to work just fine for the last 3 or 4 months. Since yesterday, I can't seem to get it working again.

Is there a property setting for the winform that needs to be set?

UPDATE Here is the actual draw method

method TMakerPoly.Draw;
var
  pts: Array of point;
  i:integer;
  theBrush1:HatchBrush;
  theBrush2:SolidBrush;
begin
  if (theBrushStyle = HatchStyle.Wave) then
     theBrush1 := new HatchBrush(theBrushStyle,Color.Transparent,color.Transparent)
  else if (theBrushStyle = HatchStyle.ZigZag) then
     thebrush2 := new SolidBrush(FillColor)
  else
     theBrush1 := new HatchBrush(theBrushStyle,FillColor,color.Transparent);

  if (thePen.DashStyle = DashStyle.Custom) then
    thepen.Color := Color.Transparent;

  pts := new point[pcount];

  if pcount >= 2 then
  begin
    if Active then
    begin
      for i := 0 to pcount-1 do
        pts[i] := point(points[i]);
      Translate(var pts,pcount);

      thepen.Color := EdgeColor(thepen.Color);
      fillColor := self.BackColor(FillColor);
      if visible then
      begin
        if filled then
        begin
            if theBrushStyle = HatchStyle.ZigZag then
                g.FillPolygon(theBrush2,pts)
            else 
                g.FillPolygon(thebrush1,pts);

            g.DrawPolygon(thepen, pts);
        end
        else
            g.DrawLines(thePen, pts);
      end;
    end
    else
    begin
      for i := 0 to pcount-1 do
        pts[i] := point(points[i]);

      if filled then
      begin
            if theBrushStyle = HatchStyle.ZigZag then
                g.FillPolygon(theBrush2,pts)
            else 
                g.FillPolygon(thebrush1,pts);

            g.DrawPolygon(thepen,pts);        
      end
      else
        g.DrawLines(new Pen(color.Black),pts);
    end;
  end;
end;

Any help or hints or clues will be greatly appreciated.

Était-ce utile?

La solution

If your pts array has less than 2 points, it will throw the parameter is not valid error.

Make sure your array has more then enough points to draw the lines.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top