Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Painting on transparent background multiple times

Painting on transparent background multiple times

Scheduled Pinned Locked Moved C#
graphicshelptutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fdsfsa76f7sa6
    wrote on last edited by
    #1

    I have a panel with transparent background, that I want to paint on multiple times. It paints alright the first time, but each time after that the drawing gets thicker. This problem is only with transparent backgrounds, drawing anything on it multiple times will make a drawing thicker on each painting event. Any ideas how to overcome this problem?

    modified on Sunday, September 12, 2010 8:10 AM

    A 1 Reply Last reply
    0
    • F fdsfsa76f7sa6

      I have a panel with transparent background, that I want to paint on multiple times. It paints alright the first time, but each time after that the drawing gets thicker. This problem is only with transparent backgrounds, drawing anything on it multiple times will make a drawing thicker on each painting event. Any ideas how to overcome this problem?

      modified on Sunday, September 12, 2010 8:10 AM

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Just use the same panel and draw the content only on it.

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      Visit My Website-->**

      www.abhisheksur.com

      F 1 Reply Last reply
      0
      • A Abhishek Sur

        Just use the same panel and draw the content only on it.

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        Visit My Website-->**

        www.abhisheksur.com

        F Offline
        F Offline
        fdsfsa76f7sa6
        wrote on last edited by
        #3

        The problem is that I have the panel inside a custom-made ScrollableControl. The program needs to draw some "drawing" on it as many times as user wants, each time a different drawing. As I've said the issue is only with transparent background and it seems it's a common problem. If you're just painting strings, then you can fix it by writing:

        e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

        But this makes the text look "ugly" and I also need to draw shapes. I fixed it for now by removing the panel from the scrollable-control, creating a new panel and adding it back to the control...

        this.someControl.SuspendLayout();
        this.someControl.Controls.Remove(this.someControl.aPanel);
        this.someControl.aPanel = new Panel();
        this.someControl.aPanel.BackColor = Color.Transparent;
        this.someControl.aPanel.Paint += new //...
        //...
        this.someControl.Controls.Add(this.someControl.aPanel);
        this.someControl.ResumeLayout();

        Of course I had to make the panel public.

        D 1 Reply Last reply
        0
        • F fdsfsa76f7sa6

          The problem is that I have the panel inside a custom-made ScrollableControl. The program needs to draw some "drawing" on it as many times as user wants, each time a different drawing. As I've said the issue is only with transparent background and it seems it's a common problem. If you're just painting strings, then you can fix it by writing:

          e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

          But this makes the text look "ugly" and I also need to draw shapes. I fixed it for now by removing the panel from the scrollable-control, creating a new panel and adding it back to the control...

          this.someControl.SuspendLayout();
          this.someControl.Controls.Remove(this.someControl.aPanel);
          this.someControl.aPanel = new Panel();
          this.someControl.aPanel.BackColor = Color.Transparent;
          this.someControl.aPanel.Paint += new //...
          //...
          this.someControl.Controls.Add(this.someControl.aPanel);
          this.someControl.ResumeLayout();

          Of course I had to make the panel public.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Don't keep destroying and creating panels. That's expensive and inefficient. Try drawing to a Bitmap object (your offscreen buffer), then paint the panel with the bitmap image.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          F 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Don't keep destroying and creating panels. That's expensive and inefficient. Try drawing to a Bitmap object (your offscreen buffer), then paint the panel with the bitmap image.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            F Offline
            F Offline
            fdsfsa76f7sa6
            wrote on last edited by
            #5

            Doesn't work, same issue. Image (the drawings) get thicker on each painting event. So destroying and creating panels is only option that I know of that works.

            L 1 Reply Last reply
            0
            • F fdsfsa76f7sa6

              Doesn't work, same issue. Image (the drawings) get thicker on each painting event. So destroying and creating panels is only option that I know of that works.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I agree with Dave, purchasing a new panel (or anything else) inside the Paint handler is not a great idea and should be avoided. I'm no expert on transparency graphics, and I am aware WinForms treats it in strange ways. My guess is selecting a transparent background color causes Windows not to erase the Control (i.e. not to repaint the background), so whatever gets painted never gets removed. What you could try is start your Paint handler with something based on Graphics.FillRectangle(Brushes.Transparent, Panel.Bounds), mind you I haven't tested this. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              F 1 Reply Last reply
              0
              • L Luc Pattyn

                I agree with Dave, purchasing a new panel (or anything else) inside the Paint handler is not a great idea and should be avoided. I'm no expert on transparency graphics, and I am aware WinForms treats it in strange ways. My guess is selecting a transparent background color causes Windows not to erase the Control (i.e. not to repaint the background), so whatever gets painted never gets removed. What you could try is start your Paint handler with something based on Graphics.FillRectangle(Brushes.Transparent, Panel.Bounds), mind you I haven't tested this. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                F Offline
                F Offline
                fdsfsa76f7sa6
                wrote on last edited by
                #7

                Nope, still same problem. I don't see how this would work, because I think that anything you paint, it gets painted over the background and not the background itself (tried overriding OnPaintBackground, doesn't work either). It seems like it's an issue with clear-type, because I can notice red and green (or blue) outline around the drawn shapes getting thicker - same outline as if you'd take a screenshot and zoomed-in or use the magnifier tool on a clear-type text.

                L 1 Reply Last reply
                0
                • F fdsfsa76f7sa6

                  Nope, still same problem. I don't see how this would work, because I think that anything you paint, it gets painted over the background and not the background itself (tried overriding OnPaintBackground, doesn't work either). It seems like it's an issue with clear-type, because I can notice red and green (or blue) outline around the drawn shapes getting thicker - same outline as if you'd take a screenshot and zoomed-in or use the magnifier tool on a clear-type text.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  That is too bad. I'm afraid I can't help you any further. You'll need more Googling and some luck. FWIW: the way I understand it, transparency is handled entirely different in WPF; I haven't tested it, and I was told the learning curve is rather steep... :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups