Moving PictureBox transparent background - c#

So I am trying to create an application in which I use png pictures in PictureBoxes. I am able to create those PictureBoxes with a transparent background, but when I move them around underneath the transparent places the background image looks kinda stuck.
So is there any way to leave the background as it is? I use the arrow keys as an input to choose direction, and I use a ticker to relocate the PictureBox.

Related

Winforms C#: problem with image transparency

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.
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.

Add PictureBox over Button

As can be seen in the image, I am trying to create a minesweeper game, whereby a flag is displayed on a square (a button) when the user right-clicks a box. I set the flag PictureBox to a transparent background, and brought it to the front by using the BringToFront() method.
For some reason, the transparent background cuts through the original colour of the green squares, which is not what I intended. Is there any way I could use the transparent background (so as to get rid of the original white background of the image) without it cutting through the BackColour of the green square?
Any help appreciated, thanks!

How can I XOR an image onto another image in WPF (C#)?

I'm writing an application where I need a crosshair cursor to pick points on an arbitrary image. Being as the image may contain both light and dark regions, I'm looking to XOR the cursor on top of the image, thus inverting the colours of the pixels where the cursor lies. In order to illustrate what I mean, please see the following image...
My application will be written using WPF and C#.NET. The image will be displayed in a Canvas within a ScrollViewer, where the cursor will be contained within a .png file. The cursor image file contains a white cursor on a black background so that it will produce the correct results once it's "blitted" onto the image with an XOR operation. I've also gone with the approach of having the cursor as an image file because I will need to show instances of the cursor at selected points within the image.
Of course, another important requirement is that the drawing operation should run as fast as possible, as it's basically a cursor that will move constantly as the user moves the mouse.
Thanks in advance!

Black in my picturebox c#

when I use an image to a URL in my picture box I have black in here
Image = http://prntscr.com/cx1nre
I am known it's because my panel is on Highlight but when I use " Transparent " it give me a black in my picture box... example = http://prntscr.com/cwu9ev
Anyone can help me please?
After understanding your question (one picture over other and the top with transparency), your problem is how .net transparency works, it's not real transparency, it copies the background of the container.
So, to make it work instead of two picture boxes add a panel and a picturebox inside the panel, set the backgroundimage of the panel to the image you wanted to use, and then set the overlay image on the picturebox. As .net copies the container's backgrond, as you added the picture box as a child of the panel the picture box will copy the image and the transparency would work as you expected.
Only caveat is that you need to load manually the image, but there are plenty of examples on how to load it.
Also, there's even a better approach: instead of two pictureboxes just add one, load into a Bitmap the background you want, retrieve a Graphics object from it, draw the second image with Graphics.DrawImage and then use the bitmap as the Image for the picturebox. It's more complicated but it's the most efficient way to do this.

Make a PictureBox transparent and allow it to be moved

I would need a picturebox that contains a graphic for an actor. The actor is placed on PictureBoxes that contain a tileset graphic. The actor should be able to move freely which i already did, but the transparent .PNG Actor-Image has a control-colored background when I load it into the form.
Question:
How could I make the background of this Image transparent AND allow to be moved?
The problem with windowsforms is that its control does not have true transparency
The transparency of a control is based on the BackgroundColor or BackgroundImage of its parent and that is why your PictureBox is Control-Colored
Now if you want to make it true transparency you have some options
Draw everything yourself
You can use the power of gdi+ or Graphics to draw everything, but only the needed ones
Handle the draw of the control
This could be a tricky one, you can override the method OnPaint and OnPaintBackground of the picture box so it do not redraw its background (Control-Colored), the bad side of this is that it will have a lot of flickering and buggy background on movement
Use another techology
I'm not sure but if I'm not mistaken you could use wpf for this, altough it could change your entire project

Categories