//画线语句
 g.DrawLine(new Pen(sb.Color, width), x + width / 2, 10, x + width / 2, 40);//画出来的效果?不知道为什么高度不一样呢!?

解决方案 »

  1.   

    g.DrawLine(new Pen(sb.Color, width), x + width / 2, 10, x + width / 2, 40); 
    楼主只给出一条线,这个问题与其他线有关。(1) 核实四个参数排列顺序,纵坐标是否相等。
    x1 第一个点的 x 坐标。 
    y1 第一个点的 y 坐标。 
    x2 第二个点的 x 坐标。 
    y2 第二个点的 y 坐标。(2)参数类型 y1,y2 的值 是否是经过计算得到的,坐标值,float浮点型,是不准确数值,计算过程中和表示上有误差。
    坐标值使用前采用相同的精度。 或最后直接转化成int32类型。这样保证值是相等的。
      

  2.   

    x + width / 2 在外边转换成 int看看如果不行Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;看看
      

  3.   

    我画的是竖线,y1:10,y2:40我是写死进行测试的,图片也就是通过这个语句画出来的X1,X2是根据线宽来进行变化的?问题奇怪的的是,当width变大后,线就一样长了!
      

  4.   

    //画线语句 
    g.DrawLine(new Pen(sb.Color, width), x + width / 2, 10, x + width / 2, 40); 就是这一语句呀!其它的代码就是计算width的的值的
    width:是保留二位的小数的!
      

  5.   

    Graphics g = this.CreateGraphics();            for (int width = 6; width < 40; width+=4)
                {
                    g.DrawLine(new Pen(Color.Black, 2), width , 10, width, 40);
                }没有出现你的这种现象
      

  6.   

    我测试了,是长短不齐,细的长. 怀疑边界线处理时与浮点数误处理有关,与线宽有关系.可以换一个方法,这样不存在边界线的问题SolidBrush myBrush = new SolidBrush(Color.Blue);
    g.FillRectangle(myBrush, new RectangleF( 3, 1, 1, 100));
    g.FillRectangle(myBrush, new RectangleF( 6, 1, 2, 100));
    g.FillRectangle(myBrush, new RectangleF(11, 1, 3, 100));
    g.FillRectangle(myBrush, new RectangleF(20, 1, 2, 100));
    g.FillRectangle(myBrush, new RectangleF(25, 1, 5, 100));
      

  7.   

    15楼的代码 在windowxp + C#2005环境 测试通过
      

  8.   

    你的代码太少了,没发现你说的问题,你是在测试一个人家写的代码吗?
    如果是,条形码本来都是有长有短的,多出来的地方可能是其他地方画的你都定死了 10-40            int x = 0;
                float width = 10.99F;
                e.Graphics.DrawLine(new Pen(Brushes .Red,width) , x + width / 2, 10, x + width / 2, 40);            x = 12;
                width = 20.99F;
                e.Graphics.DrawLine(new Pen(Brushes.Red, width), x + width / 2, 10, x + width / 2, 40);            x = 34;
                width = 30.99F;
                e.Graphics.DrawLine(new Pen(Brushes.Red, width), x + width / 2, 10, x + width / 2, 40);
      

  9.   

    你让长度向其中一个看齐不就好了吗?
    计算好第一个的Height后,其他都是用这个.
      

  10.   

    你用 g.FillRectangle 试了吗?
      

  11.   

    g.DrawLine(new Pen(sb.Color, width), x + width / 2, 10, x + width / 2, 40); Y值已固定!!!!
      

  12.   

    把g.DrawLine 换成 g.FillRectangle 一样能画出条码,应该更合理.
      

  13.   

    g.FillRectangle这个能画出来,但就精度来说,DrawLine比FillRectangle的精度高
      

  14.   

    g.FillRectangle的精度够用。条码是用来打印和扫描的,打印机输出的最小单位是1像素,你能计算出0.03像素也没用。
      

  15.   

    up26F
    的确,企业用的打印机输出最小单位是一像素.FillRectangle精度够用了.
      

  16.   

    你发贴的标题是:相同高度用DrawLine画出来的线不一样长?“现在我是条码打印机,打印最小规格的少一条线,我现在在想办法解决, 
    有什么办法可以提高精度的?” 应该是另一个问题吧.你已经确认是精度不够吗?不会是别的原因?
      

  17.   

    请注意 g.DrawLine(new Pen(sb.Color, width), ...) 中, Pen 的宽度由 width 指明, 所以只需要变更 width 的值, 即可改变线条的宽度
      

  18.   

    sorry, 看错了, 丢人ing...
    不过楼主也有误导嫌疑, 问题问的也忒模糊了
      

  19.   


            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);            Graphics g = e.Graphics;            for (int i = 0; i < 100; i += 5)
                {
                    float width = i / 20.0f;
                    if (width > 2.0f) {
                        width = 2.0f;
                    }
                    g.DrawLine(new Pen(Color.Black, width), (float)i , (float)10, (float)i, (float)80);
                } 
                for (int i = 0; i < 100; i += 5) {
                    float width = i / 20.0f;
                    if (width > 2.0f) {
                        width = 2.0f;
                    }
                    g.FillRectangle(new SolidBrush(Color.Black), (float)i, (float)100, width, (float)80);
                } 
            }试了下, FillRectangle 精度比 DrawLine 精度高点