Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.73/5 (10 votes)
Hi guys,

i wanted to know if it possible to overlay a progress bar with a picture box.
I used the Tutorial for a Splash Screen[^] and then thought about a nice design for my progress bar.

I came to the idea, that overlaying it with a picture box (where some parts are transparent) would be good idea to make a "design" quite easy. Unfortunately i could not manage to display the progress bar since then.

I tried to put both on a form, progress bar in the background. Setting the picture box to transparent (backColor) just leaves me with showing the backColor of my form.

Putting the progress bar into the picture box (via code) and setting it into background shows the progress bar on top (nothing magical).

Putting the picture box into the progress bar shows the same result like the one before, just flipped. The pic is overlaying and not "transparent".

I actually don't know which code to post since i tried using the designer to cope with that problem but now got stuck.

Please feel free to ask if you need more information.

Regards and thanks
Posted
Updated 27-Mar-21 10:18am
v2
Comments
BillWoodruff 5-Jan-16 8:18am    
Is this Windows Forms ?
HobbyProggy 5-Jan-16 8:34am    
It is, and while trying on and on i fear this won't work properly with it right? Do i really have to switch to WPF?

1 solution

Create a UserControl.
The way I did mine was to have a "coloured"image and a "monochrome" version of the same picture, and then paint the appropriate parts of each so that the initially monochrome image changed to full colour as the progress completed, but it would be pretty simple to just draw a single image and use a transparent background for the rest.
This may help:
C#
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace UtilityControls
    {
    /// <summary>
    /// Draw a progress image from two images.
    /// </summary>
    /// <remarks>
    /// To use this control, add it to your form.
    /// Set the Max and Min properties to a sensible value for your task
    /// Set the ImageCompleted and ImageUncompleted images to the appropriate
    /// pictures. For example, set the Completed image to a full colour picture,
    /// and the Uncompleted image to a monochrome version.
    /// As your task progesses, set the Value proprty to the new status.
    /// Assuming Max is 100, Min is 0, and teh images are set as above, a
    /// gradual change of Value from 0 to 100 will convert the Monochrome 
    /// picture to full colour, showing the progress.
    /// </remarks>
    public partial class ProgressImage : UserControl
        {
        #region Constants
        #endregion
        #region Fields
        #region Internal Fields
        /// <summary>
        /// Internal, completed image at the size of the whole control.
        /// This saves having to re-size it in the paint event.
        /// </summary>
        private Bitmap bComp;
        /// <summary>
        /// Internal, uncompleted image at the size of the whole control.
        /// This saves having to re-size it in the paint event.
        /// </summary>
        private Bitmap bUncomp;
        #endregion
        #region Property Bases
        /// <summary>
        /// Internal, minimum value for progress
        /// </summary>
        private int min = 0;
        /// <summary>
        /// Internal, current value of progress
        /// </summary>
        private int value = 50;
        /// <summary>
        /// Internal, maximum value for progress
        /// </summary>
        private int max = 100;
        /// <summary>
        /// Internal, image to display in completed part of display
        /// </summary>
        private Image imageCompleted = null;
        /// <summary>
        /// Internal, image to display in completed part of display
        /// </summary>
        private Image imageUncompleted = null;
        #endregion
        #endregion
        #region Properties
        #region Visible Properties
        /// <summary>
        /// Minimum value for progress
        /// </summary>
        [Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Description("Minimum value for progress"),
        Category("Progress")]
        public int Min
            {
            get { return min; }
            set
                {
                min = value;
                Invalidate();
                }
            }
        /// <summary>
        /// Current value of progress
        /// </summary>
        [Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Description("Current value of progress"),
        Category("Progress")]
        public int Value
            {
            get { return this.value; }
            set
                {
                this.value = value;
                Invalidate();
                }
            }
        /// <summary>
        /// Maximum value for progress
        /// </summary>
        [Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Description("Maximum value for progress"),
        Category("Progress")]
        public int Max
            {
            get { return max; }
            set
                {
                max = value;
                Invalidate();
                }
            }
        /// <summary>
        /// Image to display in completed part of display
        /// </summary>
        [Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Description("Image to display in completed part of progress"),
        Category("Display")]
        public Image ImageCompleted
            {
            get { return imageCompleted; }
            set
                {
                imageCompleted = value;
                BuildImages();
                Invalidate();
                }
            }
        /// <summary>
        /// Image to display in uncompleted part of display
        /// </summary>
        [Browsable(true),
        EditorBrowsable(EditorBrowsableState.Always),
        Description("Image to display in uncompleted part of progress"),
        Category("Display")]
        public Image ImageUncompleted
            {
            get { return imageUncompleted; }
            set
                {
                imageUncompleted = value;
                BuildImages();
                Invalidate();
                }
            }
        #endregion
        #region Hide these properties
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override bool AllowDrop { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new AutoScaleMode AutoScaleMode { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override bool AutoScroll { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new Size AutoScrollMargin { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new Size AutoScrollMinSize { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override bool AutoSize { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public new AutoSizeMode AutoSizeMode { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override AutoValidate AutoValidate { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override ImageLayout BackgroundImageLayout { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override ContextMenuStrip ContextMenuStrip { get; set; }
        /// <summary>
        /// Hidden property, inherited from UserControl but irrelevant
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override RightToLeft RightToLeft { get; set; }
        #endregion
        #endregion
        #region Regular Expressions
        #endregion
        #region Enums
        #endregion
        #region Constructors
        /// <summary>
        /// Construct a ProgressImage instance
        /// </summary>
        public ProgressImage()
            {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BuildImages();
            }
        #endregion
        #region Events
        #region Event Constructors
        #endregion
        #region Event Handlers
        /// <summary>
        /// Draw the progress bar images.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProgressImage_Paint(object sender, PaintEventArgs e)
            {
            Graphics graphics = e.Graphics;
            DoHorizontalImageReveal(graphics);
            }
        /// <summary>
        /// Resize control: rebuild the base images
        /// This saves time in the paint event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProgressImage_Resize(object sender, EventArgs e)
            {
            BuildImages();
            }
        #endregion
        #endregion

        #region Public Methods
        #endregion

        #region Private Methods
        /// <summary>
        /// Draw an image as the bar component
        /// Reveal an image with the current value.
        /// I.e. If the uncompleted image is a monochrome version of the 
        /// completed image, then as the value increases, the progress bar 
        /// would fill with colour, but the images would not appear to move.
        /// </summary>
        /// <param name="graphics"></param>
        private void DoHorizontalImageReveal(Graphics graphics)
            {
            int border = (Width * value) / (max - min);
            int remain = Width - border;
            if (bComp != null)
                {
                // Completed part
                // Can use unscaled and clipped - it is quicker.
                graphics.DrawImageUnscaledAndClipped(bComp, new Rectangle(0, 0, border, Height));
                }
            if (bUncomp != null)
                {
                // Uncompleted part
                // Can't use unscaled as need to offset into image.
                graphics.DrawImage(bUncomp, new Rectangle(border, 0, remain, Height), new Rectangle(border, 0, remain, Height), GraphicsUnit.Pixel);
                }
            }
        /// <summary>
        /// Build the complete and incomplete images
        /// </summary>
        private void BuildImages()
            {
            bComp = BuildImage(imageCompleted, Width, Height);
            bUncomp = BuildImage(imageUncompleted, Width, Height);
            }
        /// <summary>
        /// Build an image at the correct size
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        private Bitmap BuildImage(Image image, int width, int height)
            {
            if (image == null)
                {
                return null;
                }
            return new Bitmap(image, width, height);
            }
        #endregion
        }
    }


[edit]I HATE F***ING MARKDOWN! Leave my code alone you stupid pile of cr@p![/edit]
 
Share this answer
 
v2
Comments
HobbyProggy 5-Jan-16 9:21am    
Thank you for that information OG :)
But i just "jumped over my shadow" and fiddled a WPF uc into a WindowsForms Form and this also works quite smooth.

Nonetheless, i'll try your solution!
BillWoodruff 5-Jan-16 9:49am    
+5 Wow !
Mycroft Holmes 7-Jan-16 2:39am    
I hope you just C&P that from an existing app hahah
OriginalGriff 7-Jan-16 3:33am    
No - from my UtilityControls Assembly - it seemed like something I might want to reuse! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900