Black in my picturebox c# - 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.

Related

Transparency over multiple pictureBoxes

I have an array of picture boxes that are arranged in a square. I want to put a larger, mostly transparent picture box over the top. But when I do it covers the other picture boxes and just draws the background of the form.
Is there a way to get it to have all the other picture boxes show where it is transparent?
Transparency in WinForms isn't great. Some controls have transparency support, others don't. Some controls can be subclassed to enable this (by calling Control.SetStyle() with the SupportsTransparency flag). I believe this can be done with PictureBox.
However, transparency in all WinForms controls works by having the transparent control call its parent control to draw the background before the child control draws. This means that you cannot have two sibling controls and expect transparency on one to show through to the other. Sorry!
All that said, it would be possible to code your own workaround to support this. It would involve subclassing PictureBox and clever coding in the OnPaint override to locate sibling controls and manually trigger painting of them into in-memory bitmaps. Lots of gotchas with this approach.
Try WPF!
Here's a tip to get the desired result:
Create multiple copies of your top image.
Add each copy to the Controls of each picture box it should cover.
Adjust location of each copy according to the offset of each picture box to be covered.
So you will see every copy of your big image covering each picture box as if they were a single image.

How to display images on top of each other using C# (Forms or WPF)?

I need to display images on top of each other. This can either be a composite\layered image or separate images. This will typically be a larger image with smaller images on top. With the composite\layered approach the smaller images would each need to be a separate (and accessible) layer. With the separate images approach the smaller images are on top with a transparent background. With either approach the smaller images must be accessible i.e. can be moved (dragged) or deleted. The app needs to display these images together (as if it was one image) and keep track of the coordinates (position) of the smaller images.
The current (proof-of-concept) solution has a PictureBox control that displays the large image and a treeview. Nodes are dragged from the treeview to the picture box and rendered using the graphics DrawString or DrawImage methods – these draw the smaller images. The problem is that once the smaller image is drawn I cannot get back to it as a separate graphics object. The picture box “sees” it as part of the current image.
I need to do this in C# (WinForms or WPF). And the image type must be a common and open format i.e. not proprietary. Preferably no 3rd party controls.
Any suggestions\guidance?
WPF would be the technology of my choice for this project.
Drag & Drop way nicer, without flickering, better concept
Rendering directly on HW, way smoother for image manipulation
General idea of the implementation:
MVVM pattern
ViewModel holding your layer information
ItemsControl bound to the collection of Small overlay Images holding a DataTemplate which visualizes the images thour custom UserControl based on a Thumb (for drag & drop functionality) displayes the small images on top.
Easy to add functionality to drag, edit, hide each small image seperately.
The problem is that once the smaller image is drawn I cannot get back
to it as a separate graphics object. The picture box “sees” it as part
of the current image.
That behaviour is perfectly normal. GDI drawing does not work with objects. To do what you are asking, you need to track any event that can cause a change in the final image and when such an event occur, you need to draw the whole image from the begining, including the first image (assigning the background image, I guess).
May be create picture box per image. The transparent background will give the effect of single image.

How to colour a transparent part of an image programmatically in .NET

I have an image created and saved manually as a file, which has a shape (e.g. a heart) that is transparent in the image, and the rest of the image has other colour. How to make the transparent shape with a specified colour programmatically in .NET, leaving the rest of the image untouched?
For example, provided is an image, which I want to fill transparent part with a colour.
http://www.sendspace.com/file/an53a1
It's transparent. So draw the background first in the color you want with Graphics.Clear(), then Graphics.DrawImage() to draw the image.
What you need is a flood fill algorithm.
Please take a look at this:
http://www.codeproject.com/KB/GDI-plus/floodfillincsharp.aspx

C# winforms Picturebox, backgroundimage zoomed at the top?

I have a picturebox where I change the BackgroundImage frequently. I have a the BackgroundImageLayout set to Zoom.
The problem is that when an image does not have the same scale as the picturebox, the picture is drawn in the middle. That is, the top and the bottom padding of the picturebox is always the same.
I would like for the BackgroundImage to always be aligned at the top. What is the easiest and most performance efficient way of doing this?
I can add that I download the images from the internet. If you think that the best way to deal with this is to resize them at that point I can do that :)
By far the simplest solution is to just change the Size property of the picture box so that the image can be drawn without padding. Implementing your own Paint event handler is possible too, not exactly necessary here I presume.

Transparent PNG in PictureBox

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) ;

Categories