Skip to content

transparent backgrounds for FormsPlot and WpfPlot #286

Closed

Description

@envine

Is it possible to set transparent background on formsPlot control in WinForms?

formsPlot.plt.Style(figBg: Color.Transparent); 
formsPlot.plt.Style(dataBg: Color.Transparent);

This makes blue background as we see in the designer.
It seems that bitmap can be generated with transparency, but I need it in WinForms control.

Activity

  1. StendProg commented on Mar 7, 2020

    @StendProg
    Contributor

    Hi @envine.
    Your problem turned out to be more complicated than it seems at first glance.
    Winforms have poor transparency support.
    There is possible to simulate transparency for UserControl by some tricks:

    1. Add in UserControl constructor
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        SetStyle(ControlStyles.Opaque, true);
        this.BackColor = Color.Transparent;
    1. Override UserControl CreateParams method
     protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle = cp.ExStyle | 0x20;
                return cp;
            }
        }

    Main problem is PictureBox which has it own implementation. I tryed to apply this tricks on it too, but unsuccessfully.

    It's possible to remake FormsPlot control to not use PictureBox. plt.GetBitmap() manualy draw to control canvas in OnPaint method. @swharden can reveal the reasons why PictureBox is used now.

    Anyway, there is no true transparency solution for Winforms. Can you describe a bit more about
    problem which you actually want to solve. Do you want to place FormsPlot over another controls and see them, or something else?

  2. envine commented on Mar 7, 2020

    @envine
    Author

    Thanks @StendProg .
    I just want to put FormsPlot on my main Form (System.Windows.Forms.Form) of the WinForm application.
    The main Form has a BackgoundImage on it, and I want to make this image visible even over FormsPlot by setting the background of FormsPlot transparent.

  3. StendProg commented on Mar 7, 2020

    @StendProg
    Contributor

    If you need transparency only over Form BackgroundImage:

    formsPlot.plt.Style(figBg: Color.Transparent); 
    formsPlot.plt.Style(dataBg: Color.Transparent);
    ...
    formsPlot.BackColor = Color.Transparent;

    This will be possible after PR #287 will be merged. This PR set internal pbPlt.BackColor together with FormsPlot.BackColor.

    It will be look something like this (formsPlot overlap button control on right, but transparent over BackgroundImage).
    image

  4. envine commented on Mar 7, 2020

    @envine
    Author

    Thank you very much @StendProg .
    I have tested your PR, and it works perfectly!

    Overriding BackColor property of PictureBox used for FormPlot is enough for my use.
    Thank you again.

  5. swharden commented on Mar 7, 2020

    @swharden
    Member

    It's possible to remake FormsPlot control to not use PictureBox. plt.GetBitmap() manualy draw to control canvas in OnPaint method. @swharden can reveal the reasons why PictureBox is used now.

    @StendProg I think the reason I did this is because PictureBox has simple and robust support for double buffering. Drawing directly on the control may produce flickering. This is my understanding, but I haven't explored this topic in a while - if you are aware of a better way let me know!

  6. added a commit that references this issue on Mar 7, 2020
  7. swharden commented on Mar 7, 2020

    @swharden
    Member

    @StendProg and @envine sorry about my confusing initial response. @StendProg described the solution very well. I updated the example, and the code explains itself:

    formsPlot1.plt.Style(figBg: Color.Transparent, dataBg: Color.Transparent);
    formsPlot1.BackColor = Color.Transparent;

    image

  8. StendProg commented on Mar 7, 2020

    @StendProg
    Contributor

    This solution works only for Form background, any control hide under FormsPlot. As you explained there is only flicker ploblem you see in OnPaint render implementation. Maybe i will test this implementation later.

  9. envine commented on Mar 7, 2020

    @envine
    Author

    Thanks for your comment @swharden .
    I understand the issue on complete transparency is not the matter of ScottPlot implementation.
    And, thank you for merging the latest PR by @StendProg and adding nice demo for that.
    It works as I expected.

  10. added a commit that references this issue on Mar 7, 2020
  11. changed the title [-]WinForms transparent background[/-] [+]transparent backgrounds for FormsPlot and WpfPlot[/+] on Mar 7, 2020
  12. swharden commented on Mar 7, 2020

    @swharden
    Member

    For what it's worth, I added support (and a demo) for the WpfPlot control too.

    image

    It was hard to track down, but the only change needed was to modify the encoder for bmp.Save:

    https://github.com/swharden/ScottPlot/blob/22e7e23e4a6bb8877a0a801ce1a9557e620ceba8/src/ScottPlot.WPF/WpfPlot.xaml.cs#L82-L92

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions