1

I want to give a pictureBox the shape of an image I have. The image off course is rectangular, but there is the backgroundcolor that should be ignored, which leads to the desired form. So if there is a picture of a banana on a white background, I should have a pictureBox in shape of a banana at the end.. This is needed so that I can move shaped PictureBox in front of a AxWMPLib Mediaplayer without having the rectangle surrounding the banana covering all of the video.

My approach is to create a new class that extends PictureBox and then setting the region to the shape. Problem is, I don't know how to get the shape.

class ShapedPBox : PictureBox
{
    public ShapedPBox()
    {
         this.Paint += this.shapedPaint;
    }

    void shapedPaint(object sender, paintEventArgs e)
    {
        System.Drawing.Drawing2D.GraphicsPath gP = new System.Drawing.Drawing2D.GraphicsPath();
        //do something to give gP.addPicture? perhaps something with transparency key?

        this.Region = new Region(graphicsPath);
    }
    public Image image;
}
Salocin
  • 393
  • 6
  • 17
  • Is this a picture you have control over or is the content of the picture completely unknown to you? – Anthony Dec 17 '14 at 13:13
  • 1
    It depends. If there are other controls behind or you move it you have an issue. If however there is only the form with no movement, then `BackColor = transparent`and load the image normally. You need the `GraphicsPath` for hit test. – γηράσκω δ' αεί πολλά διδασκόμε Dec 17 '14 at 13:15
  • 1. The picture is unknown to me.. 2. yes there are other controls that move behind it and itself is gonna move. that's why BackColor = Color.Transparent doesn't work.. – Salocin Dec 17 '14 at 13:18
  • A simple answer, you can't do it. A complicated one, you need many "hacks" involving getting `dc's` and drawing the background your self. – γηράσκω δ' αεί πολλά διδασκόμε Dec 17 '14 at 13:22
  • This is clearly __NOT__ a duplicate as marked. The real question is how to extract a shape from an image into a graphicspath. Which is too broad ,-) – TaW Dec 17 '14 at 14:07
  • @TaW yes, I do agree, but I don't know how to remove the marking as a duplicate.. – Salocin Dec 17 '14 at 14:11
  • The problem of tracing around an image is not exactly a simple one. Looks like [this post](http://stackoverflow.com/questions/9752410/creating-a-graphicspath-from-a-semi-transparent-bitmap) contains two working solutions though.. – TaW Dec 17 '14 at 15:19
  • i found a solution and posted it here: http://stackoverflow.com/questions/27530407/c-sharp-how-to-create-pictureboxes-with-shapes-based-on-a-picture/27530408#27530408 – Salocin Dec 17 '14 at 16:40

1 Answers1

0

You can set the SizeMode property of PictureBox to StretchImage and that would remove default background colour. Of course, the image will shrink or grow based on image and control size.

If you cannot resize the image and you know where the background is, following code could be used. All it does is that it gets colour from edge of image and sets that as background color of picture box.

Image image = Image.FromFile(@"c:\Image.png");
Bitmap bitmap = new Bitmap(image);
pictureBox1.BackColor = bitmap.GetPixel(0, 1);
pictureBox1.Image = image;
danish
  • 5,550
  • 2
  • 25
  • 28
  • but where/how do you adapt the shape of the picturebox? – Salocin Dec 17 '14 at 13:22
  • We don't. Why do you need to change the shape? Just set the background colour same as image's background colour and it will appear as if there is seamless integration. If you want to change image's background to match with your form's background, that will involve a lot of image processing. – danish Dec 17 '14 at 13:24
  • I need to adapt the shape of the picturebox because i want to be able to position a lot of pictureboxes and other controls so that they do not cover each other with "their rectangles".. – Salocin Dec 17 '14 at 13:29
  • This is just anchoring and docking work then. Take a look at these properties and also a TableLayoutControl. This control is awesome when it comes to complex layouts. – danish Dec 17 '14 at 13:38
  • I hardly doubt that this works with the mentioned banana or similar over a AxWMPLib Mediaplayer, does it? – Salocin Dec 17 '14 at 13:43
  • This does not answer the question. – TaW Dec 17 '14 at 14:08
  • i found a solution and posted it here: http://stackoverflow.com/questions/27530407/c-sharp-how-to-create-pictureboxes-with-shapes-based-on-a-picture/27530408#27530408 – Salocin Dec 17 '14 at 16:39