Painting on transparent background multiple times
-
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
-
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
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-->**
-
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-->**
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.
-
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.
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 -
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 KreskowiakDoesn'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.
-
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.
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.
-
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.
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.
-
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.
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.