locked
WIN Form and using images RRS feed

  • Question

  • hello,

    I have a question about background image in a win form, what image format is the best to use, witch best means i this case lichtset and the best quality, i use PNG and BMP form images

    If i use PNG format when i change things it flashes. and BMP images make the form.exe big. so is there any beter format out there witch works on Window XP and Vista without any unwanted effects

    Monday, March 23, 2009 10:19 PM

Answers

  • Suppress flicker by setting the DoubleBuffered property to True.  Speed up painting by using a bitmap with the 32bppPArgb format and pre-scaling it to fit the size of the form.  Thus:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          this.DoubleBuffered = true;
        }
        protected override void OnLoad(EventArgs e) {
          Bitmap bg = new Bitmap(this.ClientSize.Width, this.ClientSize.Height,
              System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
          using (Graphics gr = Graphics.FromImage(bg)) {
            gr.DrawImage(Properties.Resources.bgimage, new Rectangle(0, 0, bg.Width, bg.Height));
          }
          this.BackgroundImage = bg;
          this.BackgroundImageLayout = ImageLayout.Stretch;
          base.OnLoad(e);
        }
      }


    Hans Passant.
    • Marked as answer by Bruce.Zhou Monday, March 30, 2009 9:31 AM
    Tuesday, March 24, 2009 7:46 AM

All replies

  • There all the same when displayed; System.Drawing.Bitmap.  It's the particular image that you're displaying after it's decompressed and shown that's causing whatever you observe.
    Monday, March 23, 2009 10:54 PM
  • Suppress flicker by setting the DoubleBuffered property to True.  Speed up painting by using a bitmap with the 32bppPArgb format and pre-scaling it to fit the size of the form.  Thus:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          this.DoubleBuffered = true;
        }
        protected override void OnLoad(EventArgs e) {
          Bitmap bg = new Bitmap(this.ClientSize.Width, this.ClientSize.Height,
              System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
          using (Graphics gr = Graphics.FromImage(bg)) {
            gr.DrawImage(Properties.Resources.bgimage, new Rectangle(0, 0, bg.Width, bg.Height));
          }
          this.BackgroundImage = bg;
          this.BackgroundImageLayout = ImageLayout.Stretch;
          base.OnLoad(e);
        }
      }


    Hans Passant.
    • Marked as answer by Bruce.Zhou Monday, March 30, 2009 9:31 AM
    Tuesday, March 24, 2009 7:46 AM