I am very new to C sharp programming. I have the design like as shown below attached image.

My concept is i have to set some volume in "Transfer volume" text box (for example 100) and then press "set" button. It automatically sets the scale of picture box, it is working fine.

Now i want to fill the picture box with colors when i click "Regenerate" button. The percentage of color to be filled in picture box should be the number in the TextBox of that regarding color or liquid.

for example
if i set GasPhase =5 ; Hydrocarbon Liquid =5; Water= 5; Oil Based Mud =5; Water Based Mud =5 ; Not Identified = 75.

then the picture has to fill with 75 % with "Not Identified" color and GasPhase color with 5 % e.t.c.

I have the written some code as shown below.

  using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

      namespace test
        public partial class Form1 : Form
        public Form1()

        private void txtTransferVolume_TextChanged(object sender, EventArgs e)


        private void txtTransferSet_Click(object sender, EventArgs e)
            string value = txtTransferVolume.Text;
            double midvalue = Convert.ToDouble(value);
            lblTransferBottleMax.Text = value;
            lblTransferBottleMid.Text = (midvalue / 2).ToString();


        private void chkTransferManual_CheckedChanged(object sender, EventArgs e)
            if (chkTransferManual.Checked)
                txtTransferGas.Enabled = true;
                txtTransferHydrocarbonLiq.Enabled = true;
                txtTransferOilBasedMud.Enabled = true;
                txtTransferWater.Enabled = true;
                txtTransferWaterBasedMud.Enabled = true;
                txtTransferNotIdentified.Enabled = true;

                txtTransferGas.Enabled = false;
                txtTransferHydrocarbonLiq.Enabled = false;
                txtTransferOilBasedMud.Enabled = false;
                txtTransferWater.Enabled = false;
                txtTransferWaterBasedMud.Enabled = false;
                txtTransferNotIdentified.Enabled = false;


        private void btnTransferBottleRegenerate_Click(object sender, EventArgs e)



Please help me how to fill as i want.

My design image
Thanks in advance.

Edited by vebi1000

4 Years
Discussion Span
Last Post by ddanbe

I don't see your design image. But it should be a fairly simple taskto fill your PictureBox with coloured rectangles, with a size depending on the percentage.


form1 I have added my design please have a look at this.
I have the concept that some suggested me like this

 private void DrawPercentages(int[] percentages, Color[] colorsToUse)
            // 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 / 100f;

            // 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(colorsToUse[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]);

but i am facing problem with how to collect data from TextBoxes and passing through array.

Edited by vebi1000


Use the int.Parse or int.TryParse methods to convert the TextBox.Text to an int and fill up your percentages array.
Don't create your own Graphics object.
Use the PictureBox Paint event: See here


Yes, that int.parse method is working perfectly.
I have one more doubt, How can i save the image of the picturebox in harddisk. it has to save when i click "Regenerate" button. Please help me with this.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.