1

Possible Duplicate:
Transparent images with C# WinForms

I am coding an app which will have a No Border. It will also have a BackgroundImage[which would be displayed as a slideshow; changed with a timer].

Sample Image [you may need to download this to experience the transparency] :

Image

I have tried the following two methods to accomplish this :

  • Using WindowsFormsApplication :

    I used the TransparencyKey property of the Form along with setting the same Background Colour.[I used Fuchsia]. Now some of the pixels in the border had the colour Fuchsia.

  • Using WindowsPresentationFoundation :

    I used the AllowsTransparency of the Window and set the Background to the Image and the image was displayed correctly with transparency. Now I have to add another image at a point [by setting the margin] which i noted before in Paint. For Example : I had the image cropped from the original background and then i have place it in the correct place in the form by setting the margin noted before. But this causes to set a wrong margin!

    Update :

    In WPF, If I use the BackgroundImage in the Grid, it solves the margin problem but the Transparency just does not work and makes the transparent regions white!

Please give some hints to get out of this problem!

Community
  • 1
  • 1
Writwick
  • 2,133
  • 6
  • 23
  • 54

2 Answers2

3

It is a problem caused by the image. You can see it when you load it in, say, Paint.NET and zoom in so you can see the individual pixels. The upper left corner looks like this:

enter image description here

Note how the pixels on the edge are partially transparent. So if you draw this image on top of a background of, say, Fuchsia then those edge pixels are no longer gray, they blend with the background and produce a different color. Which no longer matches the TransparencyKey so the video adapter won't filter them. You'll see them as a fringe of various shades of magenta.

You'll need to edit the image to give it a "hard" edge without any transparency.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Now This was a good answer which solved half of my problem. But Do you know why in WPF it is not working?? I actually personally use Paint.Net but wasn't known to this issue with the `TransparencyKey`. I want to mention about the `BoltBait's Transparency Adjustment` Plugin for Paint.Net which when set to hundred will solve this problem by converting all the semi-transparent pixels to its opaque form. But the WPF is still unanswered. So, +1. – Writwick Jun 12 '12 at 10:49
  • Not sure what you are talking about, a problem with a margin? The mechanism is the same in WPF, except that you can't directly control the color key. Set the alpha of the BackGround to 0 and the WindowStyle to None. – Hans Passant Jun 12 '12 at 11:35
  • You wont understand until you face it! I can't make you understand it better. Ok, My problem was solved before your last comment! I just used the Window Background as Null Brush and assigned the Image to the Background of the Grid. That's it! It worked even with the semi-transparency! Btw Thanks! – Writwick Jun 12 '12 at 11:41
0

In WPF window:

WindowStyle=None

AllowsTransparency=True

Background=Transparent

And then put an image with your content doesn't work?

Guilherme Duarte
  • 3,371
  • 1
  • 28
  • 35