.NET Graphics - Create an ellipse with a transparent background

The following code draws an ellipse in the image and fills that ellipse with tomato color

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);

If I change the brush to transparent, it obviously will not be displayed, because the ellipse will be transparent, and the picture will be shown below.

How to set the background of an ellipse transparent so that the image contains a transparent spot?

EDIT:

Sorry for the confusion, but like this ... alt text

+3
source share
3 answers

. , RadialImageBrush ( ) . 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

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

+1

, .

Color.FromArgb(alpha, r, g, b), alpha .

, 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 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);
}
0

Source: https://habr.com/ru/post/1775311/


All Articles