代码之家  ›  专栏  ›  技术社区  ›  Nicholas Murray

.NET图形-创建具有透明背景的椭圆

  •  4
  • Nicholas Murray  · 技术社区  · 14 年前

    下面的代码将在图像上绘制一个椭圆,并用番茄色填充该椭圆

    string imageWithTransEllipsePathToSaveTo = "~/Images/imageTest.png";
    Graphics g = Graphics.FromImage(sourceImage);
    
    g.FillEllipse(Brushes.Tomato, 50, 50, 200, 200);
    
    sourceImage.Save(Server.MapPath(imageWithTransEllipsePathToSaveTo), ImageFormat.Png);
    

    如果我将画笔更改为透明,它显然不会显示,因为椭圆将是透明的,下面的图像将显示。

    如何将椭圆的“背景”设置为透明,以便图像包含透明点?

    编辑:

    很抱歉这么混乱。。。 alt text

    3 回复  |  直到 14 年前
        1
  •  2
  •   Greg Sansom    14 年前

    这是我的第二个答案,使用图像而不是彩色画笔。不幸的是没有镭射刷子(我知道)。我包含了将图像保存到磁盘的代码,包括 usings 以确保导入正确的组件。这确实使用WPF,但它应该作为库或控制台应用程序的一部分工作。

    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Shapes;
    namespace WpfApplication30
    {
        class ImageEditor
        {
            public static void processImage(string loc)
            {
                ImageSource ic = new BitmapImage(new Uri(loc, UriKind.Relative));
                ImageBrush brush = new ImageBrush(ic);
                Path p = new Path();
                p.Fill = brush;
                CombinedGeometry cb = new CombinedGeometry();
                cb.GeometryCombineMode = GeometryCombineMode.Exclude;
                EllipseGeometry ellipse = new EllipseGeometry(new Point(50, 50), 5, 5);
                RectangleGeometry rect = new RectangleGeometry(new Rect(new Size(100, 100)));
                cb.Geometry1 = rect;
                cb.Geometry2 = ellipse;
                p.Data = cb;
    
                Canvas inkCanvas1 = new Canvas();
                inkCanvas1.Children.Add(p);
                inkCanvas1.Height = 96;
                inkCanvas1.Width = 96;
    
                inkCanvas1.Measure(new Size(96, 96));
                inkCanvas1.Arrange(new Rect(new Size(96, 96)));
                RenderTargetBitmap targetBitmap =
        new RenderTargetBitmap((int)inkCanvas1.ActualWidth,
                               (int)inkCanvas1.ActualHeight,
                               96d, 96d,
                               PixelFormats.Default);
                targetBitmap.Render(inkCanvas1);
    
                using (System.IO.FileStream outStream = new System.IO.FileStream( loc.Replace(".png","Copy.png"), System.IO.FileMode.Create))
                {
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
                    encoder.Save(outStream);
                }
            }
        }
    }
    

    结果如下: alt text

        2
  •  1
  •   egrunin    14 年前

    您需要使用半透明颜色创建画笔。

    你这样做 Color.FromArgb(alpha, r, g, b) ,其中 alpha 设置不透明度。

    Example copied from MSDN :

    public void FromArgb1(PaintEventArgs e)
    {
        Graphics     g = e.Graphics;
    
        // Transparent red, green, and blue brushes.
        SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(120, 255, 0, 0));
        SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(120, 0, 255, 0));
        SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(120, 0, 0, 255));
    
        // Base and height of the triangle that is used to position the
        // circles. Each vertex of the triangle is at the center of one of the
        // 3 circles. The base is equal to the diameter of the circles.
        float   triBase = 100;
        float   triHeight = (float)Math.Sqrt(3*(triBase*triBase)/4);
    
        // Coordinates of first circle's bounding rectangle.
        float   x1 = 40;
        float   y1 = 40;
    
        // Fill 3 over-lapping circles. Each circle is a different color.
        g.FillEllipse(trnsRedBrush, x1, y1, 2*triHeight, 2*triHeight);
        g.FillEllipse(trnsGreenBrush, x1 + triBase/2, y1 + triHeight,
            2*triHeight, 2*triHeight);
        g.FillEllipse(trnsBlueBrush, x1 + triBase, y1, 2*triHeight, 2*triHeight);
    }
    
        3
  •  1
  •   Greg Sansom    14 年前

    您需要使用RadialGradientBrush:

    RadialGradientBrush b = new RadialGradientBrush();
    b.GradientOrigin = new Point(0.5, 0.5);
    b.Center = new Point(0.5, 0.5);
    b.RadiusX = 0.5;
    b.RadiusY = 0.5;
    b.GradientStops.Add(new GradientStop(Colors.Transparent,0));
    b.GradientStops.Add(new GradientStop(Colors.Transparent,0.25));
    b.GradientStops.Add(new GradientStop(Colors.Tomato, 0.25));
    g.FillEllipse(b, 50, 50, 200, 200);
    

    alt text