我写了一个图片上加字的功能,但是感觉不稳定,因为如果遇到字超出图片宽度就换行,但是换行了,但是换行的断点不正确,使得中间的几个字不见了。感觉好像有记忆或者缓存在捣乱。
而当我重新运行这些数据时,又没有这个问题了,所以很奇怪,郁闷。
if (System.Windows.Forms.TextRenderer.MeasureText(text_temp.Substring(from, j - from), f).Width > image_width)
                        {
                            from = j - 1;                          
                            image_text[hcount,0] = strtemp;
                            image_text[hcount,1] = height.ToString();
                          
                            // imageheith[hcount] = height;
                            hcount++;
                            height = height + fontheight;                        }                        strtemp = text_temp.Substring(from, j - from);                        if (j == text_temp.Length)
                        {
                          
                            image_text[hcount,0] = strtemp;
                            image_text[hcount,1] = height.ToString();
                        
             
                            // imageheith[hcount] = height;
                            hcount++;
                            height = height + fontheight;
                        }

解决方案 »

  1.   

    使用Rectangle
                _Graphics.DrawString("11111111111111111111", this.Font, Brushes.Red, new Rectangle(0, 0, 10, 100));真不行后面还有个参数 StringFormat ..
      

  2.   

    以前研究GDI+的时候会搞,现在忘了.....Sorry
      

  3.   

    这个在web上也可以这样用吗?            _Graphics.DrawString("11111111111111111111", this.Font, Brushes.Red, new Rectangle(0, 0, 10, 100)); 
      

  4.   

    建议楼主还是用GDI把文字绘制在图片上吧。
    效果与可控性要好很多。
      

  5.   

    谢谢各位的回答,因为我这个要求文字超出宽度或者高度,文字的fontsize就必须小一点,以便能在这个图片上都显示出来,而不是被截断。不知道        _Graphics.DrawString("11111111111111111111", this.Font, Brushes.Red, new Rectangle(0, 0, 10, 100));
    这个可以实现我这个需求不?
    或者计算出在已指定的fontsize的高度?
      

  6.   

    Graphics.DrawString不会自动绕行,超出图像范围的就没了
      

  7.   

    谢谢啦,用g.MeasureString去计算高度,今天好像正常了。等过几天看看结果。