gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:26:47 31 4
gpt4 key购买 nike

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

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

最佳答案

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

关于c# - .NET 图形 - 创建具有透明背景的椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199897/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com