2013-09-23 33 views
2

我想要做的是在现有图像上绘制一定程度的不透明度的纯色和/或图案。我相信从我读过的这将涉及位图掩码。我已经看到使用位图遮罩作为不透明遮罩的示例仅显示它们用于图像以某种方式裁剪它们,并且我想将其用于绘画。这基本上就是我想要做到:如何在图像上用alpha绘制颜色 - C#/。NET

1. image, 2. mask, 3. result

第一幅图像加载和绘制到使用的DrawImage派生Canvas类。我试图完成你在第三张图片中看到的内容,第二张是我可能使用的掩码的一个例子。两个关键点是第三个图像中的蓝色表面需要任意颜色,并且需要一些不透明度,以便仍然可以在底层图像上看到阴影。这是一个简单的例子,其他一些对象具有更多的表面细节和更复杂的蒙版。

+0

您正在研究牙科应用程序吗?棒极了! – sircapsalot

+1

目前尚不清楚为什么要使用面具。只需使用Graphics.FillRectangle(),使用您创建的SolidBrush,并使用alpha值小于255的Color创建。 –

+0

对此类事物的一个很好的研究是Paint.NET的最后一个开源版本 - https://代码.google.com/p/openpdn/source/checkout –

回答

2

彩色矩阵可以是此处有用:

private Image tooth = Image.FromFile(@"c:\...\tooth.png"); 
private Image maskBMP = Image.FromFile(@"c:\...\toothMask.png"); 

protected override void OnPaint(PaintEventArgs e) { 
    base.OnPaint(e); 

    e.Graphics.DrawImage(tooth, Point.Empty); 

    using (Bitmap bmp = new Bitmap(maskBMP.Width, maskBMP.Height, 
           PixelFormat.Format32bppPArgb)) { 

    // Transfer the mask 
    using (Graphics g = Graphics.FromImage(bmp)) { 
     g.DrawImage(maskBMP, Point.Empty); 
    } 

    Color color = Color.SteelBlue; 
    ColorMatrix matrix = new ColorMatrix(
     new float[][] { 
     new float[] { 0, 0, 0, 0, 0}, 
     new float[] { 0, 0, 0, 0, 0}, 
     new float[] { 0, 0, 0, 0, 0}, 
     new float[] { 0, 0, 0, 0.5f, 0}, 
     new float[] { color.R/255.0f, 
         color.G/255.0f, 
         color.B/255.0f, 
         0, 1} 
     }); 

    ImageAttributes imageAttr = new ImageAttributes(); 
    imageAttr.SetColorMatrix(matrix); 

    e.Graphics.DrawImage(bmp, 
         new Rectangle(Point.Empty, bmp.Size), 
         0, 
         0, 
         bmp.Width, 
         bmp.Height, 
         GraphicsUnit.Pixel, imageAttr); 
    } 
} 

在矩阵声明的0.5F值是α值。

enter image description here

+0

这正是我要找的,非常感谢你的信息!我试图调整它在WPF Canvas类的OnRender函数中工作,WPF Canvas类通过了DrawingContext而不是PaintEventArgs,而DrawingContext.DrawImage函数没有接受ImageAttributes的版本。你知道我怎么能从DrawingContext获得适当的功能吗? – amnesia

+0

@amnesia错过了这是WPF的部分。请参阅[WriteableBitmap](http://msdn.microsoft.com/zh-cn/library/system.windows.media.imaging.writeablebitmap.aspx)。我不能提供比这更多的东西 - 我的背景是在WinForms中。 – LarsTech

+0

这是我的错。我非常感谢你在这里的出色努力,你肯定让我朝着正确的方向前进。 – amnesia