How to do a transparent form

Arch4ngel

Well-known member
Joined
Mar 22, 2004
Messages
940
Location
Montreal, QC
I found this code on the internet and thought that it would be nice to share with everyone who want to make a nice spash screen or something like it.

files :
- Bitmap2Region.cs
- Mouse.bmp

namespace : BitmapToRegion
How it work:

C#:
[size=2]Bitmap bmp = [/size][size=2][color=#0000ff]new[/color][/size][size=2] Bitmap(@"C:\mouse.bmp");
Region reg = BitmapToRegion.BitmapToRegion.Convert(bmp, Color.White, BitmapToRegion.TransparencyMode.ColorKeyTransparent);
 
[/size][size=2][color=#0000ff]this[/color][/size][size=2].BackgroundImage = Image.FromFile(@"C:\mouse.bmp");
 
[/size][size=2][color=#0000ff]this[/color][/size][size=2].Region = reg;[/size]

This is my code that I used with the current bmp. Just try it ! Youll probably find many utility to this little piece of code.

N.B.: It contains UNSAFE CODE.
To allow compilation of Unsafe code :
  1. Project / Properties
  2. Configuration Properties
  3. Under "Code Generation" set Allow unsafe code blocks to true.
Have fun !

-------------------------------------

[edit]I found that some "white" stay or that the image got cut sometime. I dont know whats the bug and if some find it... tell me ![/edit]
 

Attachments

Last edited by a moderator:
A code is called unsafe when it dont use the garbage collector.
This is the case for API function called from DLL. But if the code is well built... you can call it safe if you want :). The only danger with unsafe code is that it can cause memory leak. Unless that its np.
 
Unsafe code does get garbage collected, you just have to follow a few rules before you are allowed to use memory in this way. Unsafe refers to the fact that you are calling code from outside the .Net managed environment and as such the .Net security policies cannot be applied and the resulting IL cannot be verified i.e. it may do unexpected (possibly illeagal operations) things. If the code attempts to do things the .Net application doesnt have permission to do then there is nothing to stop it executing.
 
Back
Top