Anti Alias DrawLineAa too strong and some usefull extensions

Aug 1, 2014 at 8:44 AM
Edited Aug 1, 2014 at 8:56 AM
Dear,
i use the magnificent WritableBitmapEx for plotting software, i also notice the Anti Alias method make lines too big, the antialias is set to strong.

There's a method to produce a soft anti alias lines? or for modify the DrawLineAa method?

Thanks a lot

i attach my WritableBitmap extensions for WPF,
Rene can you incorporate into the next release ? IF is usefull
Thanks

     public static int ConvertColor(Color color)
        {
            var a = color.A + 1;
            var col = (color.A << 24)
                     | ((byte)((color.R * a) >> 8) << 16)
                     | ((byte)((color.G * a) >> 8) << 8)
                     | ((byte)((color.B * a) >> 8));
            return col;
        }

        public static BitmapImage ToBitmapImage(this WriteableBitmap wbm)
        {
            BitmapImage bmImage = new BitmapImage();
            using (MemoryStream stream = new MemoryStream())
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(wbm));
                encoder.Save(stream);
                bmImage.BeginInit();
                bmImage.CacheOption = BitmapCacheOption.OnLoad;
                bmImage.StreamSource = stream;
                bmImage.EndInit();
                bmImage.Freeze();
            }
            return bmImage;
        }


   public static WriteableBitmap Crop(this WriteableBitmap bmpSource, System.Drawing.Rectangle recSource)
        {
            int stride = bmpSource.PixelWidth * (bmpSource.Format.BitsPerPixel / 8);
            byte[] data = new byte[stride * bmpSource.PixelHeight];
            bmpSource.CopyPixels(data, stride, 0);


            WriteableBitmap target = new WriteableBitmap(
             bmpSource.PixelWidth,
             bmpSource.PixelHeight,
             bmpSource.DpiX, bmpSource.DpiY,
             bmpSource.Format, null);

            return bmpSource;
            
        }

  public static void FillRectangleBlend(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, System.Drawing.Color color)
        {
            using (var context = bmp.GetBitmapContext())
            {
                var w = context.Width;
                var h = context.Height;

#endif

#if ANDROID
                int[] pixels = new int[(int)(bmp.Width * bmp.Height)];
                bmp.GetPixels(pixels, 0, bmp.Width, 0, 0, bmp.Width, bmp.Height);

#else
                var pixels = context.Pixels;
#endif

                // Check boundaries
                if ((x1 < 0 && x2 < 0) || (y1 < 0 && y2 < 0)
                 || (x1 >= w && x2 >= w) || (y1 >= h && y2 >= h))
                {
                    return;
                }

                // Clamp boundaries
                if (x1 < 0) { x1 = 0; }
                if (y1 < 0) { y1 = 0; }
                if (x2 < 0) { x2 = 0; }
                if (y2 < 0) { y2 = 0; }
                if (x1 >= w) { x1 = w - 1; }
                if (y1 >= h) { y1 = h - 1; }
                if (x2 >= w) { x2 = w - 1; }
                if (y2 >= h) { y2 = h - 1; }

                byte oneOverAlpha = (byte)(255 - color.A);

                unchecked
                {
                    for (int y = y1; y <= y2; y++)
                    {
                        for (int i = y * w + x1; i <= y * w + x2; i++)
                        {
                            int c = pixels[i];
                            int r = ((byte)(c >> 16) * oneOverAlpha + color.R * color.A) >> 8;
                            int g = ((byte)(c >> 8) * oneOverAlpha + color.G * color.A) >> 8;
                            int b = ((byte)(c >> 0) * oneOverAlpha + color.B * color.A) >> 8;

                            pixels[i] = 255 << 24 | r << 16 | g << 8 | b;


                        }
                    }

#if ANDROID
                bmp.SetPixels(pixels, 0, bmp.Width, 0, 0, bmp.Width, bmp.Height);
#else
                }
#endif
                }

        }


  public static void FillRectangleDeBlend(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)

        {
            using (var context = bmp.GetBitmapContext())
            {
                // Use refs for faster access (really important!) speeds up a lot!
                int w = bmp.PixelWidth;
                int h = bmp.PixelHeight;
                var pixels = context.Pixels;

                // Check boundaries
                if (x1 < 0) { x1 = 0; }
                if (y1 < 0) { y1 = 0; }
                if (x2 < 0) { x2 = 0; }
                if (y2 < 0) { y2 = 0; }
                if (x1 >= w) { x1 = w - 1; }
                if (y1 >= h) { y1 = h - 1; }
                if (x2 >= w) { x2 = w - 1; }
                if (y2 >= h) { y2 = h - 1; }

                byte oneOverAlpha = (byte)(255 - color.A);

                unchecked
                {
                    for (int y = y1; y <= y2; y++)
                    {
                        for (int i = y * w + x1; i <= y * w + x2; i++)
                        {

                            int c = pixels[i];

                            int r = (((byte)(c >> 16) << 8) / oneOverAlpha);
                            int g = (((byte)(c >> 8) << 8) / oneOverAlpha);
                            int b = (((byte)(c >> 0) << 8) / oneOverAlpha);

                            pixels[i] = 255 << 24 | r << 16 | g << 8 | b;


                        }
                    }
                }

            }
        }