• 0

[C#] Transparent Images


Question

I just spent the last two hours trawling the internet trying to find out how to have images with a transparent background.

Apparently when you set the background of a picturebox to transparent it only sets it to the colour of its parent control.

This is what im trying to do...

post-16640-1230576379_thumb.jpg

But this is what is happening...

post-16640-1230576316_thumb.jpg

This website seems to do what I want but i cant implement it correctly.

http://www.doogal.co.uk/transparent.php

Any suggestions... Thanks

Link to comment
https://www.neowin.net/forum/topic/715010-c-transparent-images/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

The quick and dirty option that I can think of is to have the image on a seperate form and have that set to transparent as well. The image and the form's transparent color should be set to the same color and that should work out fine. (I believe... sorry, Im stuck on an XML issue and throught I'd offer a quick hand)

  • 0

Ok, Im still working on the code but at least i have got the basics.

Now that i have created this transparentImage class i can overlay a transparent picture in a separate form over the current form.

I use the following code in the class in want to declare the transparent image on...

transparentImage tImage= new transparentImage(this, 300, 20);
tImage.Show();

This seems works well - it takes the current form, and places the image relative to the forms location.

However... this does not show up in the class design view (allowing for direct manipulation)... When i go to place this code in the designer.cs file i get an error.

I know that the this is the part that is screwing it up... but how would i remedy the situation?

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 MyProgram
{
	public partial class transparentImage : Form
	{
		private int oldFormX = 0;
		private int oldFormY = 0;

		private int newFormX = 0;
		private int newFormY = 0;

		private int offsetX = 0;
		private int offsetY = 0;

		Form controllingForm = null;


		public transparentImage(Form controllingForm, int X, int Y)
		{
			InitializeComponent();

			this.Location = new System.Drawing.Point(controllingForm.Location.X + X, controllingForm.Location.Y + Y);
			this.controllingForm = controllingForm;

			controllingForm.ResizeBegin += new System.EventHandler(ResizeBegin);
			controllingForm.ResizeEnd += new System.EventHandler(ResizeEnd);
			controllingForm.FormClosed += new FormClosedEventHandler(FormClosed);

		}


		private void ResizeBegin(object sender, EventArgs e)
		{
			// get the initial form size
			oldFormX = controllingForm.Location.X;
			oldFormY = controllingForm.Location.Y;

			this.Hide();
		}


		private void ResizeEnd(object sender, EventArgs e)
		{
			newFormX = controllingForm.Location.X;
			newFormY = controllingForm.Location.Y;

			calculateFormOffset();

			this.Location = new System.Drawing.Point(this.Location.X + offsetX, this.Location.Y + offsetY);

			this.Show();
		}


		private void FormClosed(object sender, EventArgs e)
		{
			this.Close();
		}


		private void calculateFormOffset()
		{
			// get the offset for the X axis
			if (newFormX > oldFormX)
			{
				offsetX = newFormX - oldFormX;
			}
			else if (newFormX < oldFormX)
			{
				MessageBox.Show("OLD" + oldFormX.ToString());
				MessageBox.Show("NEW" + newFormX.ToString());

				offsetX = newFormX - oldFormX;
			}
			else
			{
				offsetX = 0;
			}


			// get the offset for the Y axis
			if (newFormY > oldFormY)
			{
				offsetY = newFormY - oldFormY;
			}
			else if (newFormY < oldFormY)
			{
				offsetY = newFormY - oldFormY;
			}
			else
			{
				offsetY = 0;
			}

		}

	}
}

Thanks

  • 0

Sorry to drag this up again... I completely scraped the code that i was working on above since i found the C# graphics class...

The code below gives me the results i want.

private void login_paint(object sender, PaintEventArgs e)
{
	// Get Graphics Object
	Graphics g = e.Graphics;

	g.DrawImage((Image)Properties.Resources.oops, 290, 0);

}

All i need to know now is how to anchor one of these images to the form...

Can anyone help?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.