Transparent PNG in PictureBox - c#

I am trying to make simple app that allows one to compare image to transparent PNG templates, by dragging the template over picture. For this I need a way to create a PictureBox that will contain the PNG image and be transparent where the the png is transparent.
Everything works fine but the transparency part: When I load a PNG image to the PictureBox (background color is set to transparent) it shows the background color of the containing panel rather than the image that it hovers.
I searched but only found a way to make the PictureBox completely transparent.

It is difficult to make a control that is partially transparent.
What you should do is handle the lower PictureBox's Paint event (the one that does not need to be transparent), and draw the overlay image using e.Graphics.DrawImage(image, x, y). This will draw transparent and semi-transparent images correctly.
EDIT: In response to your comment, there's nothing wrong with calling the Invalidate method in the MouseMove event. However, you will notice some flickering. To solve the flickering, make a control that inherits PictureBox, and call SetStyle(ControlStyles.DoubleBuffered, true) in the constructor.

This works fine if you add the pictureBoxOnTop to the list of controls of the pictureBoxToBeHovered to be hovered.
pictureBoxToBeHovered.Controls.Add(pictureBoxOnTop);
pictureBoxOnTop.BackColor = Color.Transparent;
pictureBoxOnTop .Location = new Point(0,0) ;

Related

Transparent picturebox

I'm trying to add an icon to a Win Forms project, but I can't get the transparency working. I've tried with several different images but I'm not able to set the background color to blue.
What am I doing wrong?
Change back color of picturebox to Color.Transparent.
You can also try to use png file with transparency channel.
The PictureBox is transparent, you need to use a transparent image (such as a PNG), as already stated.

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.

C# transparent form causes image to have ugly borders

I have a transparent form in C# and on top of the form I have a splashscreen picturebox with an image. The form's backcolor is actually just Control gray. I also just set the form's transparencykey to Control gray as well. I used the tutorial here to make the form transparent: How to design a cool semi transparent splash screen?
The image is stretched over the picturebox to make it fit nicely on all computer screen resolution. Here is the original image, notice that it has a transparent left side with squares which does not have any borders.
OriginalImageInPhotoshop
However afterwards, the form has these ugly borders on the left side of the image that are the same color as the backcolor & transparencykey (in this case, Control gray). If I place the form over a black background, you can quite visibly see this. How can I get rid of this?
Problem
So far, only remedies I have found is to either not have the image set to stretch in the picturebox (but that would cause it to appear different on every computer screen!) or perhaps theres another way to make a form transparent...

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

How to make any control shape irregular in c#

how to make any control of win application transparent. i want that i will assign a background image for the target control and will invoke a routine and that routine will create that control transparent in such a way that only image will visible. as a example suppose image has assign to picture box. the picture shape is not square rather irregular. if i can make picture box transparent then user will see the image only. basically i want to make a picture box or any control irregular shape. how to achieve it through code in c#.
thanks
In WPF, transparency is quasi for free. For the image-element, assign a png-image with an alpha mask and the image will be rendered with active transparency.
For controls with an solid background, you generally have to set the Background to a transparent Brush:
If you want to make a whole window partial transparent, you have to remove the border, set the window style to none, set the background brush to a transparent brush, and set the AllowsTransparency-property of the window to true.
there is one for ordinary winform at
http://www.youtube.com/watch?v=K_JzL4kzCoE

Categories