c# – 如何动态填写图片框

我最近开始学习c#编程.我正在尝试使用 Windows窗体应用程序创建GUI.
我需要根据一些条件更新不同颜色的图片瓶.我从电子板获得四个传感器读数,这些读数是温度,压力,密度,体积.基于这些值我必须更新图片瓶.我设计了windows窗体,如下所示.

我已经为四个传感器读数创建了四个复选框.我正在手动输入预期的传输量,并将该值设置为图像瓶上的最大刻度.温度,密度的检查框用于估计卷中存在哪个数量.要估计体积数量,用户可以使用温度传感器或密度传感器,或者只使用其中的两个.如果我单击一个检查,那么我将只使用那样的传感器读数.卷的复选框表示获取到目前为止传输的卷的数量.
我有这样的条件估计数量.

1)Liquid 
 Temperature = 0 to 30 c
 Pressure =    0 to 200 bar
 Density  = 0.5 to 0.95 g/cc.
2)Gas
 Temperature = 31 to 60 c
 Pressure =    201 to 400 bar
 Density  =    0 to 0.5 g/cc.
3)Water
 Temperature = 61 to 90 c
 Pressure =    401 to 600 bar
 Density  =    0.956 to 1,15 g/cc.
4)Oil
 Temperature = 91 to 120 c
 Pressure =    601 to 800 bar
 Density  =    1.2 to 1.35 g/cc.
5)Mud
 Temperature = 121 to 150 c
 Pressure =    801 to 1000 bar
 Density  =    1.15 to 1.3 g/cc.
6)Not identified
 all the conditions Failed.

程序是如果我勾选所有三个传感器复选框,然后我将使用三个传感器读数,并检查这个条件,什么条件满足,并填充瓶子关于颜色.目前我会收到多少转移量,并检查这些条件,并在图片瓶中填充多少颜色.当我手动输入值但不像使用传感器值和条件时,我有绘图画框的代码.

private void DrawPercentages(int[] percentages,Color[] colors,float[] volumetransfer)
    {
        Bitmap bmp = new Bitmap(pictureBox1.Width,pictureBox1.Height);
        Graphics G = Graphics.FromImage(bmp);
        // Create a Graphics object to draw on the pictureBox
        //Graphics G = pictureBox1.CreateGraphics();

        // Calculate the number of pixels per 1 percent
        float pixelsPerPercent = pictureBox1.Height / volumetransfer[0];

        // Keep track of the height at which to start drawing (starting from the bottom going up)
        int drawHeight = pictureBox1.Height;

        // Loop through all percentages and draw a rectangle for each
        for (int i = 0; i < percentages.Length; i++)
        {
            // Create a brush with the current color
            SolidBrush brush = new SolidBrush(colors[i]);
            // Update the height at which the next rectangle is drawn.
            drawHeight -= (int)(pixelsPerPercent * percentages[i]);
            // Draw a filled rectangle
            G.FillRectangle(brush,drawHeight,pictureBox1.Width,pixelsPerPercent * percentages[i]);
        }
        pictureBox1.Image = bmp;
    }

请帮我如何做这个任务.我有复选框的代码如下所示(所有复选框).

private void chkTransferTemp_CheckedChanged(object sender,EventArgs e)
    {
        if (chkTransferTemp.Checked)
        {
            Tempvalue = SystemNames.Temp.Data;
        }
    }

请帮助我这个任务.

任务是我将从卷中获取数量复选框,所以我需要在图片瓶中填充很多的数量.通过考虑上述条件,总量为液体或气体或水或其他物质.例如,我从体积传感器读取“50”,那么我应该用彩色图片瓶更新到50(刻度).同时应由上述条件决定哪种颜色.我会得到温度,密度值,我需要检查是“气体”还是“液体”或“水”.并满足任何条件,然后用该颜色填满“50”.

我已经这样尝试过了

public void DrawVolume(double volume,VolumeTypes volumeType,PictureBox pictureBottle)
    {
        ucSample sample = new ucSample();

        Color color = pictureBottle.BackColor;

        switch (volumeType)
        {
            case VolumeTypes.Gas: color = Color.Lime;
                break;
            case VolumeTypes.HydrocarboneLiquid: color = Color.Red;
                break;
            case VolumeTypes.Water: color = Color.Blue;
                break;
            case VolumeTypes.WaterBasedMud: color = Color.Saddlebrown;
                break;
            case VolumeTypes.OliBasedMud: color = Color.Chocolate;
                break;
            case VolumeTypes.NotIdentified: color = Color.Gray;
                break;

        }
        Bitmap bmp = new Bitmap(pictureBottle.Width,pictureBottle.Height);
        Graphics G = Graphics.FromImage(bmp);

        float pixelsPerPercent = (float)(pictureBottle.Height * (volume / ucSample.Volume));

        int drawHeight = (int)volume;
        SolidBrush brush = new SolidBrush(color);
        G.FillRectangle(brush,pictureBottle.Width,(int)(pixelsPerPercent * volume));
        pictureBottle.Image = bmp;

    }

我从上面的代码中尝试的是:我将三个参数传递给“DrawVolume”方法,“volume”参数是我想填充图片框的值. “volumeType”是关于该卷的颜色. “ucSample.Volume”尺度的图片框.

上面的代码不工作我想要的它不是我想要的填充.
例如,我将图片框的“0到100”缩放为“50”,然后从“0到50”绘制,但是上面的代码比“0到60” .我不知道为什么这样发生.我在“goheight”中使用任何错误的计算.

编辑.

public enum VolumeTypes { Water,Gas,HydrocarboneLiquid,OliBasedMud,WaterBasedMud,NotIdentified,None }
    public VolumeTypes GetVolumeType(double density,double temprature,double pressure)
    {
        bool isDensityChecked = true;
        bool istempratureChecked = true;
        bool isPressureChecked = true;

        bool IsGasePhase = (isDensityChecked) ? (density >= 0 && density <= 0.4) : true && (istempratureChecked) ? (temprature >= 0 && temprature <= 30) : true &&
            (isPressureChecked) ? (pressure >= 0 && pressure <= 100) : true;
        bool isHydrocarbanliquid = (isDensityChecked) ? (density >= 0.5 && density <= 1) : true && (istempratureChecked) ? (temprature >= 31 && temprature <= 60) : true &&
           (isPressureChecked) ? (pressure >= 101 && pressure <= 200) : true;
        bool isWater = (isDensityChecked) ? (density >= 1 && density <= 2) : true && (istempratureChecked) ? (temprature >= 61 && temprature <= 90) : true &&
           (isPressureChecked) ? (pressure >= 201 && pressure <= 300) : true;
        bool isWaterBasedMud = (isDensityChecked) ? (density >= 2.1 && density <= 3) : true && (istempratureChecked) ? (temprature >= 91 && temprature <= 120) : true &&
           (isPressureChecked) ? (pressure >= 301 && pressure <= 400) : true;
        bool isOilBasedMud = (isDensityChecked) ? (density >= 3.1 && density <= 4) : true && (istempratureChecked) ? (temprature >= 121 && temprature <= 150) : true &&
           (isPressureChecked) ? (pressure >= 401 && pressure <= 500) : true;
        bool isNotIdentified = (isDensityChecked) ? (density >= 4.1 && density <= 5) : true && (istempratureChecked) ? (temprature >= 151 && temprature <= 200) : true &&
           (isPressureChecked) ? (pressure >= 501 && pressure <= 600) : true;

        VolumeTypes volumeType = VolumeTypes.None;

        if (IsGasePhase) volumeType = VolumeTypes.Gas;
        if (isHydrocarbanliquid) volumeType = VolumeTypes.HydrocarboneLiquid;
        if (isWater) volumeType = VolumeTypes.Water;
        if (isWaterBasedMud) volumeType = VolumeTypes.WaterBasedMud;
        if (isOilBasedMud) volumeType = VolumeTypes.OliBasedMud;
        if (isNotIdentified) volumeType = VolumeTypes.NotIdentified;

        return volumeType;


    }

    public void DrawVolume(double volume,PictureBox pictureBottle)
    {
        int x,y,width,height;
        x = 0;
        ucSample sample = new ucSample();


        y = (int)((1 - (volume / ucSample.Volume)) * pictureBottle.Height);
        // MessageBox.Show(ucSample.Volume +"");
        width = pictureBottle.Width;
        height = (int)((volume / ucSample.Volume) * pictureBottle.Height);

        Color color = pictureBottle.BackColor;

        switch (volumeType)
        {
            case VolumeTypes.Gas: color = Color.Lime;
                break;
            case VolumeTypes.HydrocarboneLiquid: color = Color.Red;
                break;
            case VolumeTypes.Water: color = Color.Blue;
                break;
            case VolumeTypes.WaterBasedMud: color = Color.Saddlebrown;
                break;
            case VolumeTypes.OliBasedMud: color = Color.Chocolate;
                break;
            case VolumeTypes.NotIdentified: color = Color.Gray;
                break;

        }
        Graphics graphics = pictureBottle.CreateGraphics();
        pictureBottle.Refresh();
        Rectangle rec = new Rectangle(x,height);
        graphics.FillRectangle(new SolidBrush(color),rec);

        //Bitmap bmp = new Bitmap(pictureBottle.Width,pictureBottle.Height);
        //Graphics G = Graphics.FromImage(bmp);

        //float pixelsPerPercent = (float)(pictureBottle.Height * (volume / ucSample.Volume));

        //int drawHeight = (int)volume;
        //SolidBrush brush = new SolidBrush(color);
        //G.FillRectangle(brush,(int)(pixelsPerPercent * volume));
        //pictureBottle.Image = bmp;

    }


    private void UpdateTable(AnalogSensorData data)
    {
        DaTarow row = _table.Rows.Find(_recs);
        if (row == null)
        {
            row = _table.NewRow();
            row["Index"] = _recs;
            row["DateTime"] = data.Time;
            row["Time"] = data.Time.ToLongTimeString();                
            row[data.SystemName] = data.Eng;

            _logs = 1;
            _table.Rows.Add(row);
        }
        else
        {
            row[data.SystemName] = data.Eng;
            if (++_logs >= SensorUC.NumberOfActive)
            {
                int i = 1;
                double density = 0,temprature = 0,pressure = 0,volume = 0;
                foreach (var item in row.ItemArray)
                {
                    sheet.Cells[(int)_recs + 3,i].Value = item;

                    //if (i == 4)
                    //{
                    //    object densityCell = item;
                    //    density = (densityCell != null) ? (double)densityCell : 0;
                    //    MessageBox.Show("density is : " + density + "");
                    //}
                    if (i == 8)
                    {
                        object pressureCell = item;
                        pressure = (pressureCell != null) ? (double)pressureCell : 0;
                        //MessageBox.Show("pressure is : " + pressure + "");
                    }
                    else if (i == 12)
                    {
                        object tempratureCell = item;
                        temprature = (tempratureCell != null) ? (double)tempratureCell : 0;
                       // MessageBox.Show("temprature is : "+ temprature + "");
                    }
                    if (i == 11)
                    {
                        object volumeCell = item;
                        volume = (volumeCell != null) ? (double)volumeCell : 0;
                        //MessageBox.Show("Volume is : "+ volume + "");
                    }

                    i++;

                }
              VolumeTypes volumeType = GetVolumeType(density,temprature,pressure);

              DrawVolume(volume,volumeType,pictureBottle);
                book.SaveAs(_logFile);
                _recs++;

            }
        }
        if (!_list.Columns[data.SystemName].HeaderText.Equals(data.SensorName))
        {
            _table.Columns[data.SystemName].Caption = data.SensorName;
            _list.Columns[data.SystemName].HeaderText = data.SensorName;
        }
        _list.FirstdisplayedCell = _list.Rows[_list.Rows.Count - 1].Cells[0];
        _list.Update();
    }

这个任务背后的概念是
主要取决于两个变量
1)音量:这是原始音量,我需要画在相框中与颜色每个数量有不同的条件.一次只能满足一个条件.
2)ucSample.Volume:这是图片瓶的规模.

我想这样实现
最初我将设置一些估计的传输量(我是将数量放入传感器的那个),然后将其分配为pictureBox的比例.这是工作.
现在我将从传感器开始读数.我将从传感器获取“音量”值.它将从“0到估计转移量(最大规模)”增加.
例如:
我估计传输量约为“500毫升”.然后我的图片Box scale将从“0到500”分配.然后我将开始传感器的读数,我将数量放入体积传感器.所以最初我会转移100毫升的水.所以图片框必须在图片框中填充“0到100ml”的水色.之后,我将“油”从“100毫升”转移到200毫升“,所以这个时候条件会有所不同,所以我需要像这样填充”0到100毫升的水颜色和从100ml到200毫升的油颜色“我需要根据条件和体积数量填写图片瓶.

从上面的代码,我只能填充一个颜色的图片瓶.例如我有水从“0到100ml”,那么我完全填满图片瓶,再加上颜色为水.然后从“100ml到200ml”,然后用“0〜200”的油色填充,不像油颜色“0〜100ml”和“100ml〜200ml”.

Edited.
I think so many users misunderstand my concept. I am very sorry for the bad explanation may be because of my English. I have tried to explain my best as shown below.

我正在估计来自系统的进入数量.最初我将设置系统的预期数量量(ucSample.Volume),这将分配为pictureBox的缩放比例.这是完美的工作.我有一定的条件估计进来的数量.我有一个音量传感器,它将给出系统已经有多少数量(存储在可变“音量”)中.我每秒更新图片框.我已经为每个数量分配了颜色.

例如:
我将估计的音量设置为“500”(ucSample.Volume = 500),然后我将启动系统.系统将缓慢倒液,100ml用量为30瓶.当系统缓慢地通过液体时,我将使用传感器读取该液体的密度,温度,并检查哪种条件符合条件,它将选择一种颜色.所以我有一个音量传感器,读取通过系统到现在的音量.它将每秒更新一次,例如到目前为止,系统只通过了10ml液体.因此,图片框只能在颜色上仅更新10个刻度.接下来假设从10毫升到20毫升进入的液体改变了条件将是不同的,所以现在我需要填充不同的颜色从10毫升到20毫升的图片框. (不是从0到20ml).这应该是这样的,它不需要改变从0到10毫升之前的颜色保持相同的颜色,并添加不同的颜色从“10到20毫升”.所以这个概念是我需要保持原来的一样,并保持从上一个终点更新.
所以我们不知道系统是怎么来的,所以最终看到pictureBox后,我们必须估算出来的系统的数量(总数)以及每种类型的个人数量.
上面的代码没有像上面所说的那样更新,它像是第一次更新,它将填充“从”0到100ml“的”绿色“颜色,如果数量从100变为200,那么它将填充另一个颜色从”0到200“ ML”. (不是从“100到200”)我失去了以前的信息,所以完全错了.我需要保持初始颜色,因为它在液体的任何变化之前已经绘制了多少,如果有任何改变必须从这一点开始.

我希望我有更明确的解释.如果有人明白我的理念,请帮助我

最后我有这个问题的答案,但我有小问题.我可以根据使用以下代码的条件动态更新pictureBox.

public void DrawVolume(double volume,height;
        x = 0;

        y = (int)((1 - (volume / ucSample.Volume)) * pictureBottle.Height) ;

        width = pictureBottle.Width;
        height = (int)((volume / ucSample.Volume) * pictureBottle.Height);

        Color color = pictureBottle.BackColor;

        switch (volumeType)
        {
            case VolumeTypes.Gas: color = Color.Lime;
                break;
            case VolumeTypes.HydrocarboneLiquid: color = Color.Red;
                break;
            case VolumeTypes.Water: color = Color.Blue;
                break;
            case VolumeTypes.WaterBasedMud: color = Color.Saddlebrown;
                break;
            case VolumeTypes.OliBasedMud: color = Color.Chocolate;
                break;
            case VolumeTypes.NotIdentified: color = Color.Gray;
                break;

        }

        int myCurrentHeight = height - lastHeight;
        if (color != currentColor)
        {  
            Rectangle rec = new Rectangle(x,myCurrentHeight);
            myRectangles.Add(new MyRectangle(rec,color));
            currentColor = color;
        }
        else 
        {
            Rectangle rec = new Rectangle(x,myCurrentHeight+myRectangles.Last<MyRectangle>().rectangle.Height);
            myRectangles.Last<MyRectangle>().rectangle = rec;
        }
        lastHeight = height;
        Bitmap bitmap = new Bitmap(Log.frmSample.PictureBottle.Width,Log.frmSample.PictureBottle.Height);
        Graphics graphics = Graphics.FromImage(bitmap);
        foreach (MyRectangle myRectangle in myRectangles)
        {
            graphics.FillRectangle(new SolidBrush(myRectangle.color),myRectangle.rectangle); 
        }
        Log.frmSample.PictureBottle.Image = bitmap;

    }

以上代码正在更新图片框,如下图所示.

我现在要做的是“初始图片框正在填充绿色,如果颜色发生变化,则下一个颜色将填充在上一个颜色的顶部,如上图所示,现在我需要的是如果颜色变化,那么现在颜色应该充满图片底部,以前的颜色应该向上移动,这个概念是如果有任何新的数量,那么它应该从底部,所以最后第一个更新的颜色将转到图片瓶顶部,最后更新的颜色应该在底部图片.

请任何人帮助我如何做到这一点.

解决方法

对于这种简单的形状,我只是使用面板.改变他们的BackColor,设置他们的位置,宽度,高度.需要时更新

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...