How to make picture box background transparent in c# - c#

Suppose i have a big Picture box that its image is like a grid.. and 5 other small picture boxes that have different images that i want to place them in the big one and their location is always changeable(by changing their location randomly and sometimes they rotate ).
However , the problem is that these picture boxes have the background white and not transparent everytime i compiled my code as i changed their property back color to transparent
i tried this general code (that i found it alot as a solution for my problem):
overImage.Parent = backImage;
overImage.BackColor = Color.Transparent;
overImage.Location = thePointRelativeToTheBackImage;
and tried this:
innerPictureBox.SendToBack();
innerPictureBox.Parent = outerPictureBox;
but they work so badd and they only work for one time...what to do if i want to make them always transparent with not changing their desired location where i want to put them

Related

What does MSPaint process to eliminate objects in background when PNG image is converted to 4-bit BMP image?

I opened a PNG image, clicked Save As, changed to 16 color Bitmap, and saved it.
Then, MSPaint warned about loss of information about transparent and image qualities and, I clicked OK.
As a result, letters behind the black glass object disappeared clearly like this image (Before->After).
By changing PixelFormat or removing transparent area on C# (.Net 4.6.2), I tried to implement this feature just like MSPaint, however, it wasn't removed clearly. In some way, Nothing changed.
Is that not about PixelFormat or ColorDepth?
Or, Is there a way to get rid of objects behind another on PNG?
What should I do to implement that?
EDIT 1
Thank you so much, #TaW. By following your method ApplyGamma, I could solve the issue.

How to show images from imagelist random in labels

I have to make a memorygame.
MSDN has already a tutorial about it: https://msdn.microsoft.com/nl-nl/library/dd553235.aspx
But thats with wingdings, and i would make it with my own images.
So my question is: how could i show images from an imagelist randomly in labels?
I have already tried different things but with no result. This is one of te ways i have tried to make a imagelist but i don't know how to place it randomly in labels, who are inside a TableLayoutPanel.
The icons the tutorial uses are strings. They can be shown in a label control. But yours are images. You might consider using picture box control.
You can replace
iconLabel.Text = icons[randomNumber];
with
iconLabel.Tag = randomNumber;
This stores the number of each card in its Tag property, a multi-purpose field.
Also set the size of the Label, and turn off AutoSize :
iconLabel.Size = yourImageList.ImageSize;
iconLabel.AutoSize = false;
Don't forget to set yourImageList.ImageSize and yourImageList.ColorDepth to values you like before loading the Images into the list!
Also: Instead of turning the ForegroundColor to Black when hiding them set the Image to null :
iconLabel.Image = null;
and when showing it use the Tag where you have stored the number to retrieve the image:
iconLabel.Image = yourImageList.Images[(int)iconLabel.Tag];
You could also add an extra image to the list that shows a nice pattern as the background of each card and set it as the image initially and when hiding..
To make its size flexible you can also replace the Labels with Panels and use the Panel.BackgroundImage. Then you can set the Panel to any size and chose e.g. BackgroundImageLayout = ImageLayout.Stretch

Transparent pixels aren't transparent

Let me start off by saying I tried to research this, but I'm unable to word it to where I wouldn't get irrelevant results.
So here's the issue. I have a button (actually, 6 buttons) which have a rounded corner appearance. They're saved as PNGs, and loaded into the button using button.BackgroundImage = [resource]. Five of them work perfectly fine. When they're clicked, the background image is changed and, again, looks fine - including changing back when the button is no longer pressed. Then there's the button in the screenshot below.
I have no clue where this green color is coming from - in fact, when I go pixel-by-pixel over the image using GetPixel, and check the ARGB values, they turn out to be purples with A = 0. Also confusing is that the green only shows up at run-time -- as shown in the image. According to PhotoShop and GiMP, the RGB is 46,139,86 .. GetPixel shows them to be various colors in the purple range, but as an example, at 0,0 it shows 164,119,182 A:0.
Here's the list of things I've tried (some pseudocode):
if (pixel.A == 0) { SetPixel(x,y,Color.Transparent); } -- No change
if (pixel.A == 0) { SetPixel(x,y,Form1.TransparencyKey); } -- See
through to desktop instead of form background (as expected).
Use a known good image (see screenshot) -- No change.
Delete control, copy and paste a known good control (see screenshot)
-- No change.
Add another control, hoping I could hide the flawed one and basically
just ignore it. -- All new buttons have this issue.
Move button to different area of form (long story). -- No change.
Set no background in designer, then set the background in Form1.Load. -- No
change.
Restart Visual Studio. -- No change.
Clean solution, rebuild. -- No change.
Restart computer. -- No change.
Anyone have this issue before, or know what's going on? I have a solution that works (use a panel as a button - I just tested it and the transparency is fine on a new panel) .. but I'd prefer to figure this out. Thanks in advance!
Here's the imgur:

Stopping effects from repeating on one another

I posted this question earlier but I have modified my code to a simple algorithm and I still have the same issue as before:
I created a picturebox which, when an effect is selected, it will then change the image in the picturebox using the color matrix.
The issues I'm having is if I choose another effect when one is selected the old effect will not disappear, instead it will just stay there and be underneath the new effect selected. The effects I'm using is sepia and greyscale for now, but can anyone help me so that once one effect is selected, the old effect is cleared rather than them just stacking up on one another. "
The key here is to cache the originalImage in a non-volatile area. For example, load it up into a hidden pictureBox that you don't touch.
When you want to apply an effect, copy the originalImage over into a displayImage picturebox and then apply the effect.
It also looks like you are setting your image to the image that is there. I am not sure but this may be taking into account what you have already set. For example when you set
Image originalImage = pictureBox.Image;
This may be taking the image you have displayed along with any effects you have already applied and setting that as your image to be modified. Like I said I am unsure of this since I can't test it at the moment.
EDIT
The following works for me:
Replace
Bitmap originalImage = (Bitmap)displayPictureBox.Image;
originalImage = (Bitmap)pictureBox.Image.Clone();
With
Image therealoriginalimage = Image.FromFile(#"C:\Users\Me\Desktop\testimg.png");
Bitmap originalImage = (Bitmap)therealoriginalimage;
As both answers mention it looks like you are setting your original image = what is currently in the picture box.
The above code is a quick fix so you can see exactly what was happening. You should modify this and have the originalimage saved as a varible as soon as your app starts. It will be much cleaner than the example above that sets the image each time you call the method

Strange Behavior with Pictureboxes

I am experiencing strange behavior with PictureBoxes and have narrowed down a test case.
I have four PictureBoxes on my test form. Two have a background color set ... one red, one blue:
If I add the following code, the red Picturebox correctly parents itself to the upper Picturebox:
this.redPictureBox.Parent = this.pictureBox1;
this.redPictureBox.Location = this.pictureBox1.Location;
this.redPictureBox.Height = this.pictureBox1.Height;
this.redPictureBox.Width = this.pictureBox1.Width;
This works as expected:
However, if I add code to do the exact same thing with the blue PictureBox, nothing happens. In fact, it appears that the second PictureBox from the top disappears altogether:
this.bluePictureBox.Parent = this.pictureBox2;
this.bluePictureBox.Location = this.pictureBox2.Location;
this.bluePictureBox.Height = this.pictureBox2.Height;
this.bluePictureBox.Width = this.pictureBox2.Width;
Why is this behavior occuring? I must be missing something obvious but the code between the two is identical ... so why the different behavior?
I suspect this is the problem:
this.bluePictureBox.Location = this.pictureBox2.Location;
You're setting the location of the blue picture box within picture box 2 to be the location of picture box 2 relative to the container. I suspect you want:
this.bluePictureBox.Location = new Point(0, 0);
The only reason this wasn't much of an issue for the red picture box is that picture box 1 is near the top of the screen. Even so, you can see that it's not the full height/width that it was, and it's not at the top-left of picture box 1.

Categories

Resources