2010-11-16 104 views
3

将下面的代码绘制的图像上的椭圆和填充的椭圆与番茄颜色.NET图形 - 创建具有透明背景的椭圆

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

+0

这恐怕是*非常*不清楚。你可以添加一张图片,以便我们看到最终结果应该是什么? – egrunin 2010-11-16 23:07:43

+0

@egrunin - 是的,我不清楚,但我想我想说的是创建一个图像上的透明斑点,没有任何内容。 – 2010-11-16 23:11:06

+0

你只能访问GDI,或者是WPF的可能性吗? – Eilistraee 2010-11-16 23:17:42

回答

2

这是我的第二个答案,并用图片代替的刷色工作。不幸的是没有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

+0

好东西谢谢! – 2010-11-18 09:06:36

0

你需要使用半透明色营造出刷。

你用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); 
} 
+0

这会画出3个重叠的圆圈,但我不认为有任何“透明点”。 – 2010-11-16 23:06:00

+0

我知道。你和我以不同的方式解释这个问题。我不知道他真的想要什么。 – egrunin 2010-11-16 23:08:56

+0

@egrunin和@Greg - 格雷格是正确的也许我错误地解释了这个问题(我一直在Google上搜索,但没有收到指向解决方案的指针)我真的很想创建一个透明点,但底下没有任何东西 – 2010-11-16 23:09:03

1

您需要使用放射渐变:

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

+0

是WPF的这一部分?我可以使用它作为在网站上预处理图像的一部分吗? – 2010-11-16 23:23:27

+0

是的,这是WPF的一部分。如果用上面的代码替换g.FillEllipse()行,则SourceImage.Save()应该将该图像保存到磁盘,以便它可以上传到网页。 – 2010-11-16 23:25:27