如何使用另一个图像删除图像的一部分
How to remove a part of a image using another image
我有两张图片,我将使用 Graphics
(一个简单的 circle/ellipse)创建其中一张。
现在我想使用另一张图片删除圆圈的一部分。它也应该支持删除 alpha 值。
I hope the link works, if not please write it into comments & I'll fix it.
感谢任何建议
编辑:
图2真的没有边框,只是为了显示边框大小...
执行以下操作:
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var image1 = new BitmapImage(new Uri("1.png", UriKind.Relative));
var image2 = new BitmapImage(new Uri("2.png", UriKind.Relative));
var bitmap1 = BitmapFactory.ConvertToPbgra32Format(image1);
var bitmap2 = BitmapFactory.ConvertToPbgra32Format(image2);
var width = 256;
var height = 256;
var bitmap3 = BitmapFactory.New(width, height);
var transparent = Color.FromArgb(0, 0, 0, 0);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var color1 = bitmap1.GetPixel(x, y);
var color2 = bitmap2.GetPixel(x, y);
Color color3;
if (color1.Equals(transparent))
{
color3 = transparent;
}
else
{
if (color2.Equals(transparent))
{
color3 = color1;
}
else
{
color3 = transparent;
}
}
bitmap3.SetPixel(x, y, color3);
}
}
Image1.Source = bitmap3;
}
}
}
我已经使用 https://www.nuget.org/packages/WriteableBitmapEx/ 使事情变得更简单,请注意 32 位 PNG 以及透明颜色,因为在 WPF 中它实际上是透明的白色。
如果您使用的是 Forms,您应该能够轻松地将其转换为 Forms,https://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx。
编辑:您本可以使用不透明蒙版,但由于 pic.2 的外部不是黑色,所以它不会起作用。
最后还是自己写了代码。就是这样:
public static Bitmap RemovePart(Bitmap source, Bitmap toRemove)
{
Color c1, c2, c3;
c3 = Color.FromArgb(0, 0, 0, 0);
for (int x = 0; x < source.Width; x++)
{
for (int y = 0; y < source.Height; y++)
{
c1 = source.GetPixel(x, y);
c2 = toRemove.GetPixel(x, y);
if (c2 != c3)
{
source.SetPixel(x, y, Color.FromArgb(c2.A, c1));
}
}
}
}
我有两张图片,我将使用 Graphics
(一个简单的 circle/ellipse)创建其中一张。
现在我想使用另一张图片删除圆圈的一部分。它也应该支持删除 alpha 值。
I hope the link works, if not please write it into comments & I'll fix it.
感谢任何建议
编辑:
图2真的没有边框,只是为了显示边框大小...
执行以下操作:
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var image1 = new BitmapImage(new Uri("1.png", UriKind.Relative));
var image2 = new BitmapImage(new Uri("2.png", UriKind.Relative));
var bitmap1 = BitmapFactory.ConvertToPbgra32Format(image1);
var bitmap2 = BitmapFactory.ConvertToPbgra32Format(image2);
var width = 256;
var height = 256;
var bitmap3 = BitmapFactory.New(width, height);
var transparent = Color.FromArgb(0, 0, 0, 0);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var color1 = bitmap1.GetPixel(x, y);
var color2 = bitmap2.GetPixel(x, y);
Color color3;
if (color1.Equals(transparent))
{
color3 = transparent;
}
else
{
if (color2.Equals(transparent))
{
color3 = color1;
}
else
{
color3 = transparent;
}
}
bitmap3.SetPixel(x, y, color3);
}
}
Image1.Source = bitmap3;
}
}
}
我已经使用 https://www.nuget.org/packages/WriteableBitmapEx/ 使事情变得更简单,请注意 32 位 PNG 以及透明颜色,因为在 WPF 中它实际上是透明的白色。
如果您使用的是 Forms,您应该能够轻松地将其转换为 Forms,https://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx。
编辑:您本可以使用不透明蒙版,但由于 pic.2 的外部不是黑色,所以它不会起作用。
最后还是自己写了代码。就是这样:
public static Bitmap RemovePart(Bitmap source, Bitmap toRemove)
{
Color c1, c2, c3;
c3 = Color.FromArgb(0, 0, 0, 0);
for (int x = 0; x < source.Width; x++)
{
for (int y = 0; y < source.Height; y++)
{
c1 = source.GetPixel(x, y);
c2 = toRemove.GetPixel(x, y);
if (c2 != c3)
{
source.SetPixel(x, y, Color.FromArgb(c2.A, c1));
}
}
}
}