C# 如何动态填充图片框

C# 如何动态填充图片框,c#,winforms,C#,Winforms,我最近开始学习c#编程。我正在尝试使用windows窗体应用程序创建GUI。 我需要更新图片瓶与不同的颜色根据一些条件。我从电子板上得到四个传感器的读数,它们是温度、压力、密度和体积。基于这些值,我必须更新图片瓶。我设计了windows窗体,如下所示。 我为四个传感器读数创建了四个复选框。最初,我手动输入预期转移体积,并将该值设置为图片瓶上的最大刻度。使用温度、压力、密度复选框来估计体积中存在的数量。要估算体积,用户可以使用温度传感器或密度传感器,或者全部使用,或者仅使用两个。如果我单击一个复选

我最近开始学习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;
    }
请帮我完成这项任务

任务是我将从体积复选框中获取体积数量,所以我需要在图片瓶中填充那么多数量。考虑到上述条件,总量为液体、气体、水或其他。例如,我从体积传感器读取“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();
    }
public enum VolumeTypes{水、天然气、碳氢液体、寡基泥浆、水基泥浆、未识别、无}
公共容积表GetVolumeType(双密度、双温度、双压力)
{
bool isDensityChecked=true;
bool isTempratureChecked=true;
bool isPressureChecked=真;
bool IsGasePhase=(是否已检查灵敏度)?(密度>=0&&density=0&&temprature=0&&pressure=0.5&&density=31&&temprature=101&&pressure=1&&density=61&&temprature=201&&pressure=2.1&&density=91&&temprature=301&&pressure=3.1&&density=121&&pressure=401&&pressure=4.1&&density=151&&temprature=501&&pressure=SensorUC.NumberOfA)(活动的)
{
int i=1;
双密度=0,温度=0,压力=0,体积=0;
foreach(row.ItemArray中的var项)
{
表.单元格[(int)u recs+3,i].值=项;
//如果(i==4)
//{
//对象密度单元=项目;
//密度=(densityCell!=null)?(双)密度单元:0;
//显示(“密度为:“+density+”);
//}
如果(i==8)
{
对象压力塞尔=项目;
压力=(压力容器!=零)?(双)压力容器:0;
//MessageBox.Show(“压力为:“+pressure+”);
}
否则如果(i==12)
{
对象温度单元=项目;
temprature=(tempratureCell!=null)?(双)tempratureCell:0;
//Show(“temprature是:“+temprature+”);
}
如果(i==11)
{
对象volumeCell=项目;
卷=(volumeCell!=null)?(双)volumeCell:0;
//显示(“卷为:+Volume+”);
}
i++;
}
volumeType volumeType=GetVolumeType(密度、温度、压力);
DrawVolume(卷、卷类型、图片框);
book.SaveAs(_日志文件);
_recs++;
}
}
if(!\u list.Columns[data.SystemName].HeaderText.Equals(data.SensorName))
{
_table.Columns[data.SystemName].Caption=data.SensorName;
_list.Columns[data.SystemName].HeaderText=data.SensorName;
}
_list.FirstDisplayedCell=\u list.Rows[\u list.Rows.Count-1]。单元格[0];
_list.Update();
}
这项任务背后的概念是 主要取决于两个变量 1) 体积:这是我需要在图片框中绘制的原始体积,关于颜色,每个数量有不同的条件。一次只能满足一个条件。 2) 体积:这是图片瓶的比例

我想小鬼
    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();
    }
 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;

    }