高分示绘图算法问题。100分求代码

小弟万元户 2009-05-04 05:07:28

有正确代码立刻结贴

前提:在onpaint事件中要将一个字符串打印到一个方框内。如果字符串过长超出界外则只显示方框内的。

要求:在方框内可以换行,直至文本将方框全部填充。


最重要的一点:该字符串一分为二,分二批打印。但看起来像直接打印该字符串一样。
我意思是假如第一段字符串打印完,恰好打印了一行半;所以下一段字符要打印出来必须接上上一段字符串。


下面的代码实现直接将该字符打印出来:

StringFormat sf = StringFormatAlignment(this.TextAlign);
Rectangle rect = new Rectangle(0, 0, 100, 100);
g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), rect, sf);
...全文
234 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanjian0503 2009-05-15
  • 打赏
  • 举报
回复
dawnbear 2009-05-15
  • 打赏
  • 举报
回复
晕 DRAWING方法是可以控制从什么位置开始打印的 我判断不出字符串打印到什么位置了
个人认为把有可能出现的字符类型打印出来所占有的宽度算出来(我没有观察过)
计算出字符串的长度 截取到未超出打印范围的字符串长度

代码我是贴不出来了 在网吧呢

我这个方法可行 肯定有更好的方法 画图用的不多
小弟万元户 2009-05-06
  • 打赏
  • 举报
回复
哪位有具体详细的代码及解说啊。小弟不胜感激。
ProjectDD 2009-05-06
  • 打赏
  • 举报
回复
UP
null1 2009-05-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 xitangzi 的帖子:]

有正确代码立刻结贴

前提:在onpaint事件中要将一个字符串打印到一个方框内。如果字符串过长超出界外则只显示方框内的。

要求:在方框内可以换行,直至文本将方框全部填充。


最重要的一点:该字符串一分为二,分二批打印。但看起来像直接打印该字符串一样。
我意思是假如第一段字符串打印完,恰好打印了一行半;所以下一段字符要打印出来必须接上上一段字符串。


下面的代码实现直接将该字符打印出来:

[/Quote]

StringFormat 中就可以设置这个效果,你自己试一下就知道了。
yulien 2009-05-06
  • 打赏
  • 举报
回复
zzxap 2009-05-06
  • 打赏
  • 举报
回复
[code=C#]
using System;
using System.IO;//用于文件存取
using System.Data;//用于数据访问
using System.Drawing;//提供画GDI+图形的基本功能
using System.Drawing.Text;//提供画GDI+图形的高级功能
using System.Drawing.Drawing2D;//提供画高级二维,矢量图形功能
using System.Drawing.Imaging;//提供画GDI+图形的高级功能

namespace Drawpicture
{
/// <summary>
/// BarChart 的摘要说明。
/// </summary>
public class BarChart
{
private int Pic_Width = 410;
private int Pic_Height = 320;

public static bool hasNeg = false;
private const string FontWord = "Arial";
private const int FontSize = 9;
public int SIDE_WIDTH = 400;
public int SIDE_HEIGHT = 400;
private const int CHART_TOP = 60;
private int CHART_HEIGHT = 300;
private const int CHART_LEFT = 60;
private const int CHART_WIDTH = 300;
public void Render(string[] word,int[] data, out string fileName)
{

for(int i=0;i<data.Length;i++)
{
if(data[i]<0)
{
Pic_Height *=2;
hasNeg = true;
SIDE_HEIGHT = (int)(SIDE_HEIGHT*1.5);
CHART_HEIGHT =(int)(CHART_HEIGHT*0.3);
break;
}
}
ChartUtil cu = new ChartUtil();
PointF[] zuoBiaoStart;
PointF[] zuoBiaoEnd;
Graphics g;
Bitmap bm;
createCanvas(out bm, out g, out zuoBiaoEnd, out zuoBiaoStart,cu);
//画坐标轴
int chiDu = drawAxes(data, zuoBiaoStart, g, zuoBiaoEnd,cu);
//画条形图
float barWidth = CHART_WIDTH / (2 * 2);
PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2),0);
float barHeight = 2;
for(int i=0;i<2;i++)
{
barHeight = ((float)data[i]/chiDu) *(CHART_HEIGHT/5) ;
if(barHeight<0)
{
barOrigin.Y = CHART_TOP + CHART_HEIGHT;
}
else
{
barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
}

g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),barOrigin.X,barOrigin.Y,barWidth,Math.Abs(barHeight));
barOrigin.X = barOrigin.X + (barWidth * 2);
}


//画右上角的说明图形
drawInstruction(CHART_LEFT, CHART_HEIGHT, CHART_TOP, g, word, data);
//输出图形
fileName = cu.PicPath + Guid.NewGuid().ToString() + ".gif";
bm.Save(fileName, ImageFormat.Gif);

//资源回收
bm.Dispose();
g.Dispose();
}
private int drawAxes(int[] data, PointF[] zuoBiaoStart, Graphics g, PointF[] zuoBiaoEnd,ChartUtil cu)
{
int chiDu =1;
int[] t = cu.getZuoBiaoValue(data,out chiDu);
for(int i=0;i<zuoBiaoStart.Length;i++)
{
PointF txtPos = new PointF();
txtPos.X = zuoBiaoStart[i].X -50;
txtPos.Y = zuoBiaoStart[i].Y - 5;
g.DrawString(t[i].ToString(),new Font(FontWord,8),Brushes.Black,txtPos);
g.DrawLine(Pens.Black,zuoBiaoStart[i],zuoBiaoEnd[i]);
}
return chiDu;
}
private void createCanvas(out Bitmap bm, out Graphics g, out PointF[] zuoBiaoEnd, out PointF[] zuoBiaoStart,ChartUtil cu)
{
//建立一个Graphics对象实例
bm = new Bitmap(Pic_Width,Pic_Height);
g = Graphics.FromImage(bm);
//设置条图图形和文字属性
g.ScaleTransform((Convert.ToSingle(Pic_Width))/SIDE_WIDTH,(Convert.ToSingle(Pic_Height))/SIDE_HEIGHT);
g.SmoothingMode = SmoothingMode.Default;
g.TextRenderingHint = TextRenderingHint.AntiAlias;

//设定画布和边
g.Clear(Color.White);
g.DrawRectangle(Pens.Black,0,0,SIDE_WIDTH-1,SIDE_HEIGHT-1);

//设置条形图的边
g.DrawRectangle(new Pen(Color.Black,1),CHART_LEFT,CHART_TOP,CHART_WIDTH, CHART_HEIGHT);
if(hasNeg)
{
g.DrawRectangle(new Pen(Color.Black,1),CHART_LEFT,CHART_TOP+CHART_HEIGHT,CHART_WIDTH, CHART_HEIGHT);
}
zuoBiaoEnd = null;
zuoBiaoStart = cu.getZuoBiaoPoint(new PointF(CHART_TOP,CHART_LEFT),CHART_HEIGHT,CHART_WIDTH,out zuoBiaoEnd,hasNeg);
}
private void drawInstruction(int CHART_LEFT, int CHART_HEIGHT, int CHART_TOP, Graphics g, string[] word, int[] data)
{
PointF colsNamePoint = new PointF(CHART_LEFT + 55,CHART_HEIGHT + CHART_TOP + 2);
if(hasNeg)
{
colsNamePoint = new PointF(CHART_LEFT + 55,CHART_HEIGHT*2 + CHART_TOP + 2);
}

PointF boxOrigin = new PointF(Pic_Width*5/6-30,CHART_TOP*1/3);
PointF textOrigin = new PointF(Pic_Width*4/5+8,CHART_TOP*1/3 -3 );
for(int i=0;i<2;i++)
{
g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
//g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
g.DrawString(word[i],new Font(FontWord,FontSize),Brushes.DarkBlue,colsNamePoint);
g.DrawString(data[i].ToString(),new Font(FontWord,FontSize),Brushes.Black,textOrigin);
colsNamePoint.X += 150;
boxOrigin.Y += 15;
textOrigin.Y += 15;
}
}
}
public class ChartUtil
{

public readonly string PicPath = System.AppDomain.CurrentDomain.BaseDirectory + "Temp/";
public PointF[] getZuoBiaoPoint(PointF orign,int height,int width,out PointF[] endPoint,bool hasNeg)
{
int num = 6;
if(hasNeg) num = 11;
PointF[] startPoint = new PointF[num];
endPoint = new PointF[num];
for(int i =0;i<num;i++)
{
PointF pfStart = new PointF();
PointF pfEnd = new PointF();
pfStart.X = orign.X;
pfEnd.X = orign.X + width;
pfStart.Y = orign.Y + i*height/5;
pfEnd.Y = pfStart.Y;
startPoint[i] = pfStart;
endPoint[i] = pfEnd;
}
return startPoint;
}
public int[] getZuoBiaoValue(int[] data,out int chidu)
{
int[] result = new Int32[11];
int Max = data[0];

for(int i=0;i<data.Length;i++)
{
if(Max < data[i])
Max = data[i];
}
string strMax = Max + "";
chidu =(int)((Convert.ToInt32(strMax.Substring(0,1)) +1)* Math.Pow(10,strMax.Length-1))/5;
for(int i=0;i<result.Length;i++)
{
result[i] = chidu* (5-i) ;
}
return result;
}

#region get color
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch(itemIndex)
{
case 0:
selectedColor = Color.Blue;
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Yellow;
break;
case 3:
selectedColor = Color.Purple;
break;
default:
selectedColor = Color.Green;
break;
}
return selectedColor;
}
#endregion
}
}
[/CODE]
bbb332 2009-05-06
  • 打赏
  • 举报
回复
帮顶
PENYY 2009-05-05
  • 打赏
  • 举报
回复
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string ="ok";
SizeF size = graphics.MeasureString(a, font);
graphics.DrawString(str, font, this.GetForeColor(), 0, 0);
pricks 2009-05-05
  • 打赏
  • 举报
回复
to 13楼:
我曾经研究过获取字符串的宽度之和的问题,只可惜,最后没有结果。
如果不信,你自己也可以上网搜一搜,大部分都只是得到字符串的长度,而非字符的宽度之和。
这个问题还得请高人来解答。

不好意思。
pricks 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bejon 的回复:]
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string a="123sddddddddddddd";
SizeF size = graphics.MeasureString(a, font);//可以获得打印这段字符所需大小,使用循环逐低个测试所可以打印的字体内容
//将字符变成位图
graphics.DrawString(a, font, this.GetForeColor(), 0, 0);
[/Quote]

[font style="font-size:15px;color:red;"]
SizeF size = graphics.MeasureString(a, font);
这种方法是得不到一个字符串的真正长度的。
[/font]
小弟万元户 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 BiologyPianoProgram 的回复:]
引用 7 楼 bejon 的回复:
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string a="123sddddddddddddd";
SizeF size = graphics.MeasureString(a, font);//可以获得打印这段字符所需大小,使用循环逐低个测试所可以打印的字体内容
//将字符变成位图
graphics.DrawString(a, font, this.GetForeColor(), 0, 0);


[/Quote]

那怎么得到真正长度啊???
sushou2009 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bejon 的回复:]
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string a="123sddddddddddddd";
SizeF size = graphics.MeasureString(a, font);//可以获得打印这段字符所需大小,使用循环逐低个测试所可以打印的字体内容
//将字符变成位图
graphics.DrawString(a, font, this.GetForeColor()…
[/Quote]
牛!
wuyq11 2009-05-04
  • 打赏
  • 举报
回复
小弟万元户 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bejon 的回复:]
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string a="123sddddddddddddd";
SizeF size = graphics.MeasureString(a, font);//可以获得打印这段字符所需大小,使用循环逐低个测试所可以打印的字体内容
//将字符变成位图
graphics.DrawString(a, font, this.GetForeColor()…
[/Quote]

有没有完整的更详细点儿的。如果字符串过长,它要换行的,它的高度,以及到边时判断是否还有空间打印,还有就是怎么再次获得下次打印的起始坐标啊,看似挺复杂的啊。。。。
pricks 2009-05-04
  • 打赏
  • 举报
回复
可以使用graphics.drawPath(g1,g2,...)
这样很容易便可以将你的字符串连接起来打印。

具体的我也不记得了,好久没做C#。
你自己上网查一查,试一试,很简单!
阿牛138588 2009-05-04
  • 打赏
  • 举报
回复
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string a="123sddddddddddddd";
SizeF size = graphics.MeasureString(a, font);//可以获得打印这段字符所需大小,使用循环逐低个测试所可以打印的字体内容
//将字符变成位图
graphics.DrawString(a, font, this.GetForeColor(), 0, 0);
阿牛138588 2009-05-04
  • 打赏
  • 举报
回复
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string a="123sddddddddddddd";
SizeF size = graphics.MeasureString(a, font);//可以获得打印这段字符所需大小,使用循环逐低个测试所可以打印的字体内容
//将字符变成位图
graphics.DrawString(codeResult, font, this.GetForeColor(), 0, 0);


阿牛138588 2009-05-04
  • 打赏
  • 举报
回复
Bitmap bitmap = new Bitmap();
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font(字体名, 字体大小, FontStyle.Regular);
string a="123sddddddddddddd";
SizeF size = graphics.MeasureString(a, font);//可以获得打印这段字符所需大小,使用循环逐低个测试所可以打印的字体内容
//将字符变成位图
graphics.DrawString(codeResult, font, this.GetForeColor(), 0, 0);


小弟万元户 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cpio 的回复:]
那直接把2个字符串连起来打啊
[/Quote]

可是这两个字符串的格式不一样,有一个要有下划线。
加载更多回复(3)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧