0

I am making chess with c sharp and visual studio 2019. I managed to generate the chessboard with panels and I am planning to use PictureBox for the chess pieces. However, despite the chess images have a transparent background, it ignores the color of the panel and uses the default background color of the form. I suspect it is due to these lines.

...
Controls.Add(newPanel);
...
Controls.Add(picturebox);
...

What it is doing is adding each newly created panel to the form and adding the picturebox (which is used for showing a chess piece) to the form. I tried adding picturebox to the panel but the location of the picturebox becomes relative to that panel. When I drag the picturebox around (at runtime), the picturebox is not visible as soon as it leaves the panel. I have seen someone implemented this successfully before but I cannot figure out how.

This is what the background color looks like when moving the picturebox around (I have methods to handle mouse up, mouse down and mouse move). Any help would be appreciated.

enter image description here

Spooky
  • 39
  • 1
  • 9
  • 1
    This is not about image but about control transparency. It can only work well with __nested__ controls but as your board is made up of several panels the pbox pieces will __overlap__ instead and transparency will not work (Well there is a workaround, maybe @Jimi jumps in). But the consensus is: Don't do like this but paint, best both, board and pieces onto one control and keep track in memory structures. Or make the pieces jump to their destinations.. – TaW Aug 05 '22 at 13:54
  • - Also: What are you targeting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! – TaW Aug 05 '22 at 13:55
  • right I see. But what do you mean by Paint? Do you mean Control.Paint? – Spooky Aug 05 '22 at 14:01
  • No. I mean the Paint event of the control you use for the board. Using it s e.Graphics parameter you can draw stuff onto its surface. You can use Apicturebox, which avoids flicker and can also have a Background image you may want to use for the board pattern. – TaW Aug 05 '22 at 14:53
  • Should be as easy as: `newPanel.Controls.Add(picturebox)`. Wanna make it visible in another panel? then just move it to another panel. – Reza Aghaei Aug 05 '22 at 14:55
  • But if they are not nested for some reason, then you can use [this solution](https://stackoverflow.com/a/36102074/3110834). And finally, an interesting solution, like what mentioned by Taw, would be having a model, and render the model on a drawing surface. Then you need to support the [click/move for your drawings](https://stackoverflow.com/a/38749134/3110834). – Reza Aghaei Aug 05 '22 at 14:59
  • Have you tried considering using a library such as ImageMagick to do the image editing for you if you must use images? If not, there are plenty of game libraries out there to draw sprites for you, even using hardware acceleration. – FalcoGer Aug 10 '22 at 09:41

1 Answers1

0

Drawing the shapes and putting them into a single control seems like the way to go. If I draw the grid and chess pieces instead of using controls, the background of the chess pieces show up correctly as transparent.

Spooky
  • 39
  • 1
  • 9