GDI + How to change Line SmoothingMode?

Can I change PowerPacks.LineShape smoothingMode?

I tried using this code (a class that inherits LineShape ):

  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim g As Graphics = e.Graphics ' no difference when changing the SmoothingMode ' g.SmoothingMode = SmoothingMode.AntiAlias Using pen As New Pen(Color.Blue, 3) g.DrawLine(pen, X1, Y1, X2, Y2) End Using ' MyBase.OnPaint(e) ' End Sub 

I always have the same result: alt text http://lh6.ggpht.com/_1TPOP7DzY1E/S3v1IbxlbCI/AAAAAAAADD4/q1Y9kP8wJ0g/s800/Capture2.png

=======

EDIT

updated test:

  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim g As Graphics = e.Graphics Dim oldmode As SmoothingMode = g.SmoothingMode Using pen As New Pen(Color.Blue, 3) g.SmoothingMode = SmoothingMode.AntiAlias g.DrawLine(pen, X1, Y1, X2, Y2) g.SmoothingMode = SmoothingMode.None g.DrawLine(pen, X1 + 50, Y1, X2 + 50, Y2) End Using g.SmoothingMode = oldmode g.Flush() 'MyBase.OnPaint(e)' End Sub 

Result (do not include marks and circles):

alt text http://lh3.ggpht.com/_1TPOP7DzY1E/S447qYvTqzI/AAAAAAAADE8/eP3kCLqQJbk/s800/Capture2.png

obviously smoothing mode is not taken as inattention ...

+6
graphics powerpacks
source share
4 answers

The question was in my development mode: through a connection to a remote desktop on a virtual PC.

In my case, the RDC does not take into account the AntiAlias ​​graphic property.

more: Do you have problems with development on a virtual PC?

Thanks to everyone for participating, sorry this was not a .NET problem.

0
source share

SmoothingMode should definitely affect your output.

Here are some settings that I recently used to resize an image with minimal loss of quality:

 graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 

InterpolationMode is probably not suitable for your example, but a PixelOffsetMode may be. Let me deploy a quick test application.

Update: In this quick test application, SmoothingMode definitely affects the lines I draw.

 private void Form1_Load(object sender, EventArgs e) { foreach (var value in Enum.GetValues(typeof(SmoothingMode))) { _ComboBoxSmoothingMode.Items.Add(value); } foreach (var value in Enum.GetValues(typeof(PixelOffsetMode))) { _ComboBoxPixelOffsetMode.Items.Add(value); } _ComboBoxPixelOffsetMode.SelectedIndex = 0; _ComboBoxSmoothingMode.SelectedIndex = 0; } private void _ButtonDraw_Click(object sender, EventArgs e) { using (Graphics g = _LabelDrawing.CreateGraphics()) { g.Clear(Color.White); if (_ComboBoxPixelOffsetMode.SelectedItem != null && (PixelOffsetMode)_ComboBoxPixelOffsetMode.SelectedItem != PixelOffsetMode.Invalid) { g.PixelOffsetMode = (PixelOffsetMode)_ComboBoxPixelOffsetMode.SelectedItem; } if (_ComboBoxSmoothingMode.SelectedItem != null && (SmoothingMode)_ComboBoxSmoothingMode.SelectedItem != SmoothingMode.Invalid) { g.SmoothingMode = (SmoothingMode)_ComboBoxSmoothingMode.SelectedItem; } using (Pen pen = new Pen(Color.Blue, 3)) { g.DrawLines(pen, new[] { new Point(0, 0), new Point(25, 50), new Point(_LabelDrawing.Width - 25, _LabelDrawing.Height - 50), new Point(_LabelDrawing.Width, _LabelDrawing.Height), }); } } } 

  SmoothingMode: AntiAlias ​​None 

SmoothingMode.AntiAlias ​​http://www.ccswe.com/temp/SmoothingMode_AntiAlias.png SmoothingMode.None http://www.ccswe.com/temp/SmoothingMode_None.png

Update:. As Morbo remarked, if the Graphics object presented to you in PaintEventArgs is not the same Graphics object that will ultimately be used for display, then anti-aliasing may not have any effect. Although I would not expect such a sharp difference if it were a Graphics object from Image memory or something like that.

I would like to offer more. Perhaps if I had better understood that I was giving you LineShape and your reasoning for using it using only one of the Graphics.DrawLine () methods.

The reason I posed a question about your use of LineShape is because you override it with OnPaint and draw your own line. It looks like you could simplify your application and align LineShape , but maybe I missed something.


Update: Well, that makes sense why you are using LineShape . The only suggestion I can offer at this moment is to override OnPaint in your panel or LineShape, try setting the anti-aliasing mode there before the base event is triggered. Something like:

 protected override void OnPaint(PaintEventArgs e) { e.Graphichs.SmoothingMode = SmoothingMode.AntiAlias; base.OnPaint(e); } 
+3
source share

From what I can say in Reflector, the PowerPack LineShape component draws using the Graphics object from the original Paint event. Changing the properties of the Graphics object that you specified can affect everything else in the container that will be drawn after your form.

What are your LineShape objects contained in your example? (If this is a custom control, are you creating a Bitmap at some point? How?) If they are inside a custom control with a low color depth, this can be the source of the problem. LineShape by default disables anti-aliasing on my machine.

+2
source share

What display settings are installed? At least 24 bits of color?

I can confirm that the output of the class from LineShape and the OverPaint redefinition, as you showed, really affects the rendering of the strings as expected. I can also confirm that both Power Pack 3.0 and version in Visual Studio 2010 specifically use AntiAlias ​​in the LineShape.DrawInternal method.

I started with a clean .NET 2.0 Windows Forms application. I added the following class, which is almost identical to yours and to a shape that contains only a ShapeContainer and a MyLine diagonal shape.

 Imports Microsoft.VisualBasic.PowerPacks Public Class MyLine Inherits LineShape Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics 'g.SmoothingMode = SmoothingMode.AntiAlias ' Using pen As New Pen(Color.Blue, 3) g.DrawLine(pen, X1, Y1, X2, Y2) End Using 'MyBase.OnPaint(e) ' End Sub End Class 

If I run the project with the above code as is, the line will be smoothed (uneven). If I uncomment the SmoothingMode parameter, the line will become smoothed (smooth).

So this should definitely work. I know this seems like a dumb question, but did you check to make sure your code gets into the debugger? Have you tried calling Graphics.Flush () right after DrawLine?

Also, which version of PowerPack are you using? As I said, I clearly see in Reflector that LineShape.DrawInternal specifically sets SmoothingMode for AntiAlias ​​in a try / finally block. It restores the old anti-aliasing mode before returning. But in your example, you should never beat this because you are not calling the base method.

+2
source share

All Articles