A Form with Wallpaper and Transparent Label (C#)

vegaseat 0 Tallied Votes 3K Views Share

Shows a basic way to create a form with a wallpaper image on it. Also shows how to add a transparent label to blend in with the wallpaper. Getting the label to be transparent wasn't all that commodious! The wallpaper is generated using a texture brush and bitmap image. The image can actually be a .bmp .jpg .gif .png .wmf image type.

// create a wallpaper using Brush/Image and put a transparent label on it
// allow controls to be transparent on the form and select
// label1.BackColor = transparent from the Web choices 
// MS Visual C# .NET by vegaseat  22jan2005 
//
// this is a Windows Application

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WallPaper1
{
  public class MainForm : System.Windows.Forms.Form
  {
    
    private System.Windows.Forms.Label label1;
    public MainForm()
    {
      this.Paint += new PaintEventHandler(TextureBrush1);
      InitializeComponent();
    }
		
    public static void Main()
    {
      Application.Run(new MainForm());
    }

    
    #region Windows Forms Designer generated code
    private void InitializeComponent() 
    {
      this.label1 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.BackColor = System.Drawing.Color.Transparent;
      this.label1.Font = new System.Drawing.Font("Comic Sans MS", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192)));
      this.label1.Location = new System.Drawing.Point(16, 40);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(456, 104);
      this.label1.TabIndex = 0;
      this.label1.Text = "Viva Las Vegas!";
      // 
      // MainForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.BackColor = System.Drawing.SystemColors.Control;
      this.ClientSize = new System.Drawing.Size(488, 264);
      this.Controls.Add(this.label1);
      this.Name = "MainForm";
      this.Text = "Transparent Label on WallPaper";
      this.ResumeLayout(false);

    }
    #endregion
		
		   
    public void TextureBrush1(object sender,PaintEventArgs e) 
    {
      Graphics g = e.Graphics;
      int width = this.Size.Width;   // form width
      int height = this.Size.Height; // form height
      
      // the background image file has to be in the working folder with the .exe file, 
      // or give it a full path to your image folder, change the image file and path 
      // to something you have (can be .bmp .jpg .gif .png .wmf image format) like
      // "D:/Image/BackGround/FrauBush.bmp"
      // ( the rule is to use foreward slashes rather than the usual \\ )
      // or pick something from the windows folder
      Image bgimage = new Bitmap("C:/Windows/Santa Fe Stucco.bmp");
      Brush bgbrush = new TextureBrush(bgimage);

      // make rectangle at least the size of the window form
      g.FillRectangle(bgbrush,0,0,width,height);
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.