【问题标题】:.NET Graphics - creating an ellipse with transparent background.NET Graphics - 创建具有透明背景的椭圆
【发布时间】:2010-11-16 22:54:09
【问题描述】:

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

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

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

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

编辑:

很抱歉造成混乱,但像这样......

【问题讨论】:

  • 恐怕这非常不清楚。您可以添加一张图片,以便我们查看完成的结果应该是什么?
  • @egrunin - 是的,我不清楚,但我想我想说的是在图像上创建一个透明点,下面没有任何东西。
  • 你只能访问 GDI,还是 WPF 有可能?
  • @Maupertuis - 我希望将其作为网站图像预处理的一部分
  • 您要做的是用图像填充 GraphicsPath(假设为 GDI)。我不确定这是否可行。

标签: c# graphics


【解决方案1】:

这是我的第二个答案,它使用图像而不是颜色画笔。不幸的是,没有 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);
            }
        }
    }
}

结果如下:

【讨论】:

    【解决方案2】:

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

    您可以使用 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 个重叠的圆圈,但我认为没有一个“透明点”。
    • 我知道。你和我对这个问题的解释不同;我不知道他真正想要什么。
    • @egrunin 和 @Greg - Greg 是正确的,也许我把这个问题表述错了(我一直在谷歌搜索,但没有收到指向解决方案的指针)我真的很想创建一个透明点下面什么都没有
    • 那么你想让图像在那个地方基本上透明吗?这意味着您将看到图像背后的任何内容(如果有的话)?
    • @climbage - 是的,我正在尝试创建一个拼图网络应用程序,我可以通过使部分图像透明来创建形状
    【解决方案3】:

    您需要使用 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);
    

    【讨论】:

    • 这是 WPF 的一部分吗?我可以将其用作网站上图像预处理的一部分吗?
    • 是的,这是 WPF 的一部分。如果你用我上面的代码替换你的 g.FillEllipse() 行,SourceImage.Save() 应该将图像保存到磁盘,以便它可以上传到网络。
    猜你喜欢
    • 2020-05-05
    • 2011-11-26
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多