标题:请大家帮忙找错!
只看楼主
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
结帖率:100%
已结贴  问题点数:10 回复次数:3 
请大家帮忙找错!
ImageExample.rar (33.26 KB)

   
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using namespace ImageExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.CancelButton = btnExit;

        }

        private void btnAddImage_Click(object sender, EventArgs e)
        {
            int i=0,j=0,k=0,high=0;

            string title = "";
            int xs = 0, ys = 0;
            int xLength = 0, yLength = 0;
            int CONST_VALUE = 0;
            int minL = 0, minR = 0;
            int midL = 0, midR = 0;
            int maxL = 0, maxR = 0;
            int []data=new int[10000];
            string []s = new string[2];
            char []splitchar={','};

            StreamReader sr = new StreamReader("ImageExample.txt");

            title = sr.ReadLine();

            s = sr.ReadLine().Split(splitchar);
            xs = int.Parse(s[0]);
            ys = int.Parse(s[1]);

            s = sr.ReadLine().Split(splitchar);
            xLength = int.Parse(s[0]);
            yLength = int.Parse(s[1]);

            CONST_VALUE = int.Parse(sr.ReadLine());

            s = sr.ReadLine().Split(splitchar);
            minL = int.Parse(s[0]);
            minR = int.Parse(s[1]);

            s = sr.ReadLine().Split(splitchar);
            midL = int.Parse(s[0]);
            midR = int.Parse(s[1]);

            s = sr.ReadLine().Split(splitchar);
            maxL = int.Parse(s[0]);
            maxR = int.Parse(s[1]);

            while (sr.Peek()!=-1)
            {

                s= sr.ReadLine().Split(splitchar,StringSplitOptions.RemoveEmptyEntries);

               //j= int.Parse(s[0]);
               //high=int.Parse(s[1]);  //就是这两句

                for (k = 1; k <= j; k++)
                {
                    data[i] = high;
                    i++;
                }

            }

            sr.Close();

            Graphics draw = this.imageLable.CreateGraphics();
            Pen drawpenmin = new Pen(Color.Blue);
            Pen drawpenmmid = new Pen(Color.Green);
            Pen drawpenmax = new Pen(Color.Red);

            int x=0,y=0;
            i=0;

            for (x = 0; x < 100; x++)
            {
                for (y = 0; y < 100; y++)
                {
                    if (data[i]+CONST_VALUE <= minR)
                    {
                        draw.DrawRectangle(drawpenmin, x, y, 1, 1);
                    }
                    else
                        if (data[i] +CONST_VALUE<= midR)
                        {
                            draw.DrawRectangle(drawpenmmid, x, y, 1,1);
                        }
                        else
                        {
                            draw.DrawRectangle(drawpenmax, x, y, 1, 1);
                        }
                }
            }

            minLable.Text = minL.ToString() + "---" + minR.ToString();
            midLable.Text = midL.ToString() + "---" + midR.ToString();
            maxLabel.Text = maxL.ToString() + "---" + maxR.ToString();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
文本的格式如下:
程序代码:
ImageExample
0,0
100,100
0
0,50
50,100
100,250
20,25
30,75
50,150
利用一个文本文件的内容在一个Label里面画图!代码中有两个句子出错,去掉程序运行正常,但不是我想要的结果!实在想不出来为什么错了,大家帮忙!

[ 本帖最后由 qq1023569223 于 2011-4-13 21:31 编辑 ]
搜索更多相关主题的帖子: color 
2011-04-13 00:09
qubo1982
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:367
专家分:1132
注 册:2009-3-18
得分:10 
会错是因为你的“ImageExample.txt”文件最后有一个空行
程序代码:
                s= sr.ReadLine().Split(splitchar,StringSplitOptions.RemoveEmptyEntries);
                if (s.Length > 1)
                {
                    j = int.Parse(s[0]);
                    high = int.Parse(s[1]);

                    for (k = 1; k <= j; k++)
                    {
                        data[i] = high;
                        i++;
                    }
                }
2011-04-13 08:45
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
得分:0 
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using namespace ImageExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.CancelButton = btnExit;
        }

        private void btnAddImage_Click(object sender, EventArgs e)
        {
            int i = 0, j = 0, k = 0, high = 0;

            string title = "";
            int xs = 0, ys = 0;
            int xLength = 0, yLength = 0;
            int CONST_VALUE = 0;
            int minL = 0, minR = 0;
            int midL = 0, midR = 0;
            int maxL = 0, maxR = 0;
            int[] data = new int[10000];
            string[] s = new string[2];
            char[] splitchar ={ ',' };

            StreamReader sr = new StreamReader("ImageExample.txt");

            title = sr.ReadLine();

            s = sr.ReadLine().Split(splitchar);
            xs = int.Parse(s[0]);
            ys = int.Parse(s[1]);

            s = sr.ReadLine().Split(splitchar);
            xLength = int.Parse(s[0]);
            yLength = int.Parse(s[1]);

            CONST_VALUE = int.Parse(sr.ReadLine());

            s = sr.ReadLine().Split(splitchar);
            minL = int.Parse(s[0]);
            minR = int.Parse(s[1]);

            s = sr.ReadLine().Split(splitchar);
            midL = int.Parse(s[0]);
            midR = int.Parse(s[1]);

            s = sr.ReadLine().Split(splitchar);
            maxL = int.Parse(s[0]);
            maxR = int.Parse(s[1]);

            while (sr.Peek() != -1)
            {

                s = sr.ReadLine().Split(splitchar, StringSplitOptions.RemoveEmptyEntries);

                if (s.Length > 0)  //光标不小心闪到到最后一个空白行就出现了这个问题,汗! 这里的问题解决了!
                {
                    j = int.Parse(s[0]);
                    high = int.Parse(s[1]); 

                    for (k = 1; k <= j; k++)
                    {
                        data[i] = high;
                        i++;
                    }
                }

            }

            sr.Close();

            Graphics draw = this.imageLabel.CreateGraphics();
            Pen drawpenmin = new Pen(Color.Blue);
            Pen drawpenmmid = new Pen(Color.Green);
            Pen drawpenmax = new Pen(Color.Red);

            int x = 0, y = 0;
            i = 0;

            for (x = 0; x < 100; x++)
            {
                for (y = 0; y < 100; y++)  //不知道为什么原来应该有三种色彩的出现,为什么却只显示蓝色的!从上到下依次是蓝绿红,但是却没有!
                {
                    if (data[i] + CONST_VALUE <= minR)
                    {
                        draw.DrawRectangle(drawpenmin, x, y, 1, 1);
                    }
                    else
                        if (data[i] + CONST_VALUE <= midR)
                        {
                            draw.DrawRectangle(drawpenmmid, x, y, 1, 1);
                        }
                        else
                        {
                            draw.DrawRectangle(drawpenmax, x, y, 1, 1);
                        }
                    i++;
                }
            }

            minLable.Text = minL.ToString() + "---" + minR.ToString();
            midLable.Text = midL.ToString() + "---" + midR.ToString();
            maxLabel.Text = maxL.ToString() + "---" + maxR.ToString();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}


[ 本帖最后由 qq1023569223 于 2011-4-14 00:37 编辑 ]

   唯实惟新 至诚致志
2011-04-13 22:33
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
得分:0 
如果没人知道的话,结贴了!

   唯实惟新 至诚致志
2011-04-14 17:00



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-336481-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058044 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved