C# Exercises

Home AgriMetSoft About Contact

Transparent Images and Forms in WinForms c#

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

namespace Transparent_images_and_Form
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      pictureBox1.ImageLocation = "c.jpg";
    }

    private void Transparent_Form_Click(object sender, EventArgs e)
    {
      this.BackColor = Color.Green;
      this.TransparencyKey = Color.Green;
    }

    private void Transparent_images_Click(object sender, EventArgs e)
    {
      Bitmap bitmap0 = new Bitmap(pictureBox1.Image);
      var bt = MakeTransparent(bitmap0, Color.White, 5);
      pictureBox1.Image = bt;
    }

    private Bitmap MakeTransparent(Bitmap bitmap, Color color, int tolerance)
    {
      Bitmap transparentImage = new Bitmap(bitmap);

      for (int i = transparentImage.Size.Width - 1; i >= 0; i--)
      {
        for (int j = transparentImage.Size.Height - 1; j >= 0; j--)
        {
          var currentColor = transparentImage.GetPixel(i, j);
          if (Math.Abs(color.R - currentColor.R) < tolerance &&
            Math.Abs(color.G - currentColor.G) < tolerance &&
            Math.Abs(color.B - currentColor.B) < tolerance)
            transparentImage.SetPixel(i, j, color);
        }
      }

      transparentImage.MakeTransparent(color);
      return transparentImage;
    }
  }
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to Transparent Background of Images and Forms in C# WinForms


List of Exercises