c# - 如何动态填充图片框

标签 c# winforms

我最近开始学习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, 0, 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, 0, drawHeight, pictureBottle.Width, (int)(pixelsPerPercent * volume));
        pictureBottle.Image = bmp;

    }


我在上面的代码中尝试的是:我正在将三个参数传递给“ DrawVolume”方法,其中“ volume”参数是我要在图片框中填写的值。 “ volumeType”是关于该体积的颜色。图片框的“ ucSample.Volume”比例。

上面的代码无法正常工作。它没有填补我想要的。
例如,对于图片框,我的缩放比例从“ 0到100”,“体积”的缩放比例为“ 50”,那么它必须从“ 0到50”绘制,但是上面的代码绘制的比“ 0到60”更多。我不知道为什么会这样。我是否在“ drawheight”中使用了任何错误的计算。

编辑。

    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, VolumeTypes volumeType, 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, y, width, 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, 0, drawHeight, pictureBottle.Width, (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:这是图片瓶的比例。

我想这样实现。
最初,我将设置一些估计的传输量(我是将数量放入传感器的传输量),然后将其分配为图片框的比例。那正在工作。
现在我将开始从传感器读取数据。我将从传感器获得“体积”值。它将从“ 0增加到估计的传输量(最大比例)”。
例如:
我将估计的传输量设置为“ 500毫升”左右。然后,我的图片框比例将分配为“ 0到500”。然后,我将开始读取传感器的读数,然后将数量输入到体积传感器中。因此,起初我将转移100毫升水。因此,图片框必须在图片框内填充从“ 0到100ml”刻度的水彩。之后,我将“油”从“ 100 ml转移到200ml”,所以这次条件将有所不同,因此我需要像这样填充“从0到100 ml的水色,从100ml到200ml的油色”我需要根据状况和数量填充图片瓶。

从上面的代码中,我只能用一种颜色填充图片瓶。例如,我的水量为“ 0至100毫升”,那么我就可以完美地在图片瓶中填充水。然后从“ 100ml到200ml”填充油色形式“ 0到200”,而不是“ 0到100ml水彩色”和“ 100ml到200ml”油色。


编辑。
我认为许多用户误解了我的概念。非常抱歉,我的英语不好,所以解释不好。我已尽力解释自己的最佳表现,如下所示。


我正在尝试估计来自系统的进货数量。最初,我将设置系统的预期数量(ucSample.Volume),这将作为比例分配给pictureBox。这很完美。我有一定的条件估计收货数量。我有一个体积传感器,它将给出来自系统的多少数量(存储在变量“ volume”中)。我每秒更新一次图片框。我为每个数量指定了颜色。

例如:
我将估计的音量设置为“ 500”(ucSample.Volume = 500),然后我将启动系统。系统将缓慢倒入液体,每100毫升需要30颗薄荷糖。当系统在那时缓慢通过液体时,我将使用传感器读取液体的密度,压力,温度,并检查是否满足条件,根据条件它将选择一种颜色。因此,我有一个音量传感器,可以读取到目前为止通过系统的音量。它将每秒更新一次,例如直到现在,系统仅通过10毫升液体。因此,图片盒最多只需要更新10个有关颜色的比例尺即可。接下来,假设从10毫升到20毫升的流入液体发生了变化,因此条件将有所不同,因此现在我需要用10毫升至20毫升刻度不同的颜色填充pictureBox。 (不是从0到20毫升)。这应该是这样的,它不需要更改前一个的颜色,即从“ 0到10毫升”保持相同的颜色,而添加不同的颜色从“ 10到20毫升”。因此,概念是我需要保持原先的状态,并从上一个终点进行更新。
因此,我们不知道来自系统的是什么,因此最终在看到pictureBox之后,我们必须估计来自系统的数量(总计)以及每种类型的单个数量。
上面的代码没有像我上面描述的那样更新,而是像第一次更新时一样,它将用“ 0”到“ 100ml”的“绿色”颜色填充,如果数量从100更改为200,则用“ 0到200”的另一种颜色填充毫升”。 (不是从“ 100到200”。)我正在丢失先前的信息,因此完全错误。我需要保持初始颜色,因为它要取决于液体变化之前已经绘制的数量,如果有变化,则必须从该点开始。

我希望我能给出更清楚的解释。如果有人了解我的概念,请帮助我。

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

 public void DrawVolume(double volume, VolumeTypes volumeType, PictureBox pictureBottle)
    {
        int x, y, width, 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, y, width, myCurrentHeight);
            myRectangles.Add(new MyRectangle(rec,color));
            currentColor = color;
        }
        else 
        {
            Rectangle rec = new Rectangle(x,y,width,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,设置其位置,宽度,高度。需要时进行更新。

关于c# - 如何动态填充图片框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15294244/

相关文章:

javascript - 浏览器不在启用 CORS 的情况下跨域发送 cookie

c# - 运行 ibtool : object cannot be nil (key: family) 时出现 Xamarin.IOS 异常

c# - 结构图和 ASP.NET 核心

c# - 手动激活/停用自定义控件的对齐线

c# - DataGridView C#绑定(bind)多个List

c# - 如何在视频控件上方添加透明控件,如 mediaelement 上的 wpf 标签

c# - 如何在带有Prism的SplitView上使用帧导航?

c# - 在录制视频ffmpeg中增加fps

c# - 安装项目在安装过程中不创建文件系统文件夹

C# 从数据集中填充 DataGridViewComboBoxCell 并从数据库中获取选定的值