0

I have a PictureBox (map) on that I added other PictureBoxes(pins). (on winforms application). I set pin backcolor transparent. Every pin's parent is the map. The problem is that pins transparency not working properly. Every pin has the map background. There are more than 100 pins, and where there are more pins close, you can see the pin corners, if the map background is different of pin background. I tried also with panel and label instead of PictureBox, but without success. Please give me an idea how to solve the problem. Thank you.

turdeanu
  • 45
  • 6
  • _a PictureBox (map) on that I added other PictureBoxes_ did you actually __nest__ them? You __need__ to do that in code: `pbPin01.Parent = pbMap;` etc.. For 100 pins I would consider painting them. Do they need to be interactive?? – TaW Apr 07 '16 at 12:58
  • If you need to have transparent `PictureBox` with layering support, you may find this post helpful: [How to make two transparent layer with c#?](http://stackoverflow.com/a/36102074/3110834). But for hundreds of pins, I also recommend using GDI+ to render them. You can also simply hit test for them and make them interactive. – Reza Aghaei Apr 07 '16 at 13:09
  • You wrote _Every pin's parent is the map_ If that were true transparency would work. PictureBox is different than containers like Panel in that __moving a control over it__ will __not__ create a Parent.Chil relationship. You need to do it __in code__! – TaW Apr 07 '16 at 13:26
  • Ah, you mean the the pins can overlap. Well that can't work properly as they are not nested. So: Draw them on top of the map pictureBox! - Also: when you ask question here you should watch for comments and immediately answer any questions! Failing to do so is rather rude, imo. – TaW Apr 07 '16 at 15:17
  • TaW- Thank you for answer. I apologize that I did not answer immediately. I did not mean to be rude, I asked the question at the end of working hours, and when I got home I answered. Even if I bring pins in top, if there is a pin very close, it will be covered by pin over edges. – turdeanu Apr 07 '16 at 16:44
  • I add every pin in pbMap control. pbMap.Controls.Add(pbPin01). Doing so, each pin's parent is pbMap. And yes, they need to be interactive. – turdeanu Apr 07 '16 at 16:58
  • Thank you Reza for answer. I will study your proposed solution. – turdeanu Apr 07 '16 at 17:06
  • @turdeanu You are welcome :) – Reza Aghaei Apr 07 '16 at 20:06
  • @Reza Aghaei, thank you again, and please help me with an other answer. I applied the solution in the link proposed by you. The functionality is correct, but because I have more than 100 pins time is too long for paint all pins, and I this solution is difficult to applied. It could be a solution that does not take so long? You said "But for hundreds of pins, I also recommend Using GDI + to render Them". Can you give me more details how could I do that? I do not have much experience working with images. Thank you. – turdeanu Apr 08 '16 at 09:38

1 Answers1

1

Well, duh. Windows isn't Photoshop. Or Flash. Or Silverlight. Or WPF.

Windows uses a very simple layering system that allows for transparency under certain conditions, but it's really more of a workaround than a full-blown layered system. Basically, when you want to render an image transparently, you first ask the control under you to render itself, and then you render over that.

Due to the way the whole hierarchy works, this means that by default, transparency only works in a parent-child relationship - a child can easily render the parent as part of itself, and it can even omit rendering its own background under certain circumstances. So if you make your pins children of your background, transparency is going to work as expected.

However, PictureBox is only really designed for showing pictures anyway. If you need to render a map with hundreds of pins, you almost certainly don't want to use windows controls to do that. An owner-drawn control that handles rendering the background and the pins will probably work better. If that's too hard for your use case, you might want to have a look at WPF - you can easily embed a WPF control inside a Winforms control, and WPF gives you a lot better tools for what you're trying to do.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • 1
    Most important point here, IMO: "don't abuse the control...draw it yourself". True far more often than not in WinForms. Not quite so daunting as folks make it seem. – DonBoitnott Apr 07 '16 at 15:03
  • Thank you Luaan. I've already used a WPF control in my application, in that I put a bing map, but for financial reasons had to give up bing map. But I will try to do that map with WPF, and put it instead of bing map. I have not thought about this option :(. I thought I could solve the problem with picturebox. I do not have much experience working with images. Thank you again. Thank you all for answers. – turdeanu Apr 07 '16 at 17:16