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”尺度的图片框. 上面的代码不工作我想要的它不是我想要的填充. 编辑. 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(); } 这个任务背后的概念是 我想这样实现 从上面的代码,我只能填充一个颜色的图片瓶.例如我有水从“0到100ml”,那么我完全填满图片瓶,再加上颜色为水.然后从“100ml到200ml”,然后用“0?200”的油色填充,不像油颜色“0?100ml”和“100ml?200ml”.
我正在估计来自系统的进入数量.最初我将设置系统的预期数量量(ucSample.Volume),这将分配为pictureBox的缩放比例.这是完美的工作.我有一定的条件估计进来的数量.我有一个音量传感器,它将给出系统已经有多少数量(存储在可变“音量”)中.我每秒更新图片框.我已经为每个数量分配了颜色. 例如: 我希望我有更明确的解释.如果有人明白我的理念,请帮助我 最后我有这个问题的答案,但我有小问题.我可以根据使用以下代码的条件动态更新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,设置他们的位置,宽度,高度.需要时更新
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |