Picturebox transparent on Picturebox, Panel,... it can be?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Picturebox transparent on Picturebox, Panel,... it can be?

  1. #1
    Join Date
    Aug 2012
    Posts
    28

    Picturebox transparent on Picturebox, Panel,... it can be?

    I reconized: the picturebox only transparent on form, addition can not any more control. So, can i make it woule be? Picturebox can transparent with some control?, like Picturebox transparent on Picturebox.

  2. #2
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Picturebox transparent on Picturebox, Panel,... it can be?

    It's possible but it's a bit complicated, and a bit advanced (and a bit hackish on top of that) so if you just want to compose several transparent pictures, it's better to ditch the picture box control, and draw the images directly onto the form's surface one on top of the other, back to front.
    The Graphics class you were using in your ellipse program can draw images as well. You can use, for example, DrawImage(Image, int, int), or DrawImage(Image, Rectangle) to draw a bitmap. The Bitmap class derives from the Image class, so DrawImage will accept it. You can do this in the Paint event handler. For example:

    Code:
    //some member variables, initialized elsewhere
    private Bitmap img1;
    private Point img1Location = new Point(100, 200);
    
    private Bitmap img2;
    private Point img2Location = new Point(140, 220);
    
    //inside the Paint event handler
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // some other code...
    
        e.Graphics.DrawImage(img1, img1Location);    // the lowermost image
        e.Graphics.DrawImage(img2, img2Location);    // drawn on top of img1
    
        // some other code...
    }

  3. #3
    Join Date
    Aug 2012
    Posts
    28

    Re: Picturebox transparent on Picturebox, Panel,... it can be?

    Quote Originally Posted by TheGreatCthulhu View Post
    It's possible but it's a bit complicated, and a bit advanced (and a bit hackish on top of that) so if you just want to compose several transparent pictures, it's better to ditch the picture box control, and draw the images directly onto the form's surface one on top of the other, back to front.
    The Graphics class you were using in your ellipse program can draw images as well. You can use, for example, DrawImage(Image, int, int), or DrawImage(Image, Rectangle) to draw a bitmap. The Bitmap class derives from the Image class, so DrawImage will accept it. You can do this in the Paint event handler. For example:

    Code:
    //some member variables, initialized elsewhere
    private Bitmap img1;
    private Point img1Location = new Point(100, 200);
    
    private Bitmap img2;
    private Point img2Location = new Point(140, 220);
    
    //inside the Paint event handler
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // some other code...
    
        e.Graphics.DrawImage(img1, img1Location);    // the lowermost image
        e.Graphics.DrawImage(img2, img2Location);    // drawn on top of img1
    
        // some other code...
    }
    Oh, that right, thank you. So, in addtion, i have a question: could i have use bitmap in graphics c#, how benefits? it smooth than on form, panel, hub????

  4. #4
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Picturebox transparent on Picturebox, Panel,... it can be?

    I'm not sure that I understand your question - maybe you can find someone nearby to help you with your English, so you could explain better?
    If you're asking if it's possible to draw on an in-memory bitmap instead directly on the form's surface - then yes. You create a Bitmap of a certain size, and then create a Graphics object for that bitmap (which will draw on that bitmap) using the static Graphics.FromImage(Bitmap) method.
    As for whether it's smoother - if you're referring to anti-aliasing (AA): it doesn't matter if you draw on a bitmap or not. This is turned on by setting a property of the Graphics object called SmoothingMode (take a look at the Ellipse code I sent you in your other thread, I used it there near the top of the Main_Paint event handler method). Here's the list of possible values, but note that there's really only two (None and AntiAlias), since the rest are synonymous to one or the other.

    EDIT: If you're asking how to avoid flickering: the key is to draw only inside (or in a method called from) the Paint event handler - otherwise you're interfering with the standard drawing cycle used by the form (under the hood, there's a message loop that causes the form to redraw whenever a paint message comes in - you can send this message yourself when you call Invalidate(), and it will be processed in the next cycle). Also, before the Paint event is raised, the form clears the background, which might also cause some flicker: you can eliminate this by doing this inside the constructor (which you'll also find in your Ellipse project):
    SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
    Then you have to call Clear() yourself at the top of the Paint handler.

    There are some benefits for using a bitmap as a back buffer. For example, it provides an easy way to save the drawing to a file, then, as I already said, you can compose several bitmaps with transparent areas, you can apply transformations to them (translate, rotate, scale, shear), and even manipulate gamma and colors (slightly advanced), if you use one of the Graphics.Draw() overloads that takes an ImageAttributes, like for example this one.
    Last edited by TheGreatCthulhu; August 9th, 2012 at 10:24 AM.

  5. #5
    Join Date
    Aug 2012
    Posts
    28

    Re: Picturebox transparent on Picturebox, Panel,... it can be?

    Quote Originally Posted by TheGreatCthulhu View Post
    I'm not sure that I understand your question - maybe you can find someone nearby to help you with your English, so you could explain better?
    If you're asking if it's possible to draw on an in-memory bitmap instead directly on the form's surface - then yes. You create a Bitmap of a certain size, and then create a Graphics object for that bitmap (which will draw on that bitmap) using the static Graphics.FromImage(Bitmap) method.
    As for whether it's smoother - if you're referring to anti-aliasing (AA): it doesn't matter if you draw on a bitmap or not. This is turned on by setting a property of the Graphics object called SmoothingMode (take a look at the Ellipse code I sent you in your other thread, I used it there near the top of the Main_Paint event handler method). Here's the list of possible values, but note that there's really only two (None and AntiAlias), since the rest are synonymous to one or the other.

    EDIT: If you're asking how to avoid flickering: the key is to draw only inside (or in a method called from) the Paint event handler - otherwise you're interfering with the standard drawing cycle used by the form (under the hood, there's a message loop that causes the form to redraw whenever a paint message comes in - you can send this message yourself when you call Invalidate(), and it will be processed in the next cycle). Also, before the Paint event is raised, the form clears the background, which might also cause some flicker: you can eliminate this by doing this inside the constructor (which you'll also find in your Ellipse project):
    SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
    Then you have to call Clear() yourself at the top of the Paint handler.


    There are some benefits for using a bitmap as a back buffer. For example, it provides an easy way to save the drawing to a file, then, as I already said, you can compose several bitmaps with transparent areas, you can apply transformations to them (translate, rotate, scale, shear), and even manipulate gamma and colors (slightly advanced), if you use one of the Graphics.Draw() overloads that takes an ImageAttributes, like for example this one.
    Thanks you very much, it solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center