如何在 WPF C# 中剪切出精灵表的某个部分并在没有精灵表背景的情况下显示它?

  • 本文关键字:精灵 背景 情况下 显示 WPF 个部 c# wpf
  • 更新时间 :
  • 英文 :


我正在 WPF 中制作一个小游戏,我想使用从另一个游戏的安装文件夹中提取的精灵表使角色的移动连续。

我不知道如何在没有"分离"黑色背景的情况下剪切精灵表图像的所需部分。

这是精灵表展示我的角色的各种动作

我只能使用从 OR System.Windows.Media.Visual System.Windows.Media.Drawing 类继承的可视化类

(这是为了大学的任务,他们让我们从其中之一中进行选择(

如果不需要在代码中解决此问题,一个快速的解决方案是使用图像编辑应用程序:

  1. 确保背景具有 Alpha 通道。
  2. 使用魔棒工具选择每个孤立的背景部分。
  3. 删除所选内容。

将背景转换为透明度。我在短短几秒钟内使用 paint.net 将魔杖容差设置为 0 对您的图像执行此操作。

否则使用不透明蒙版。

虽然不符合作业的条件,但另一种解决方案是在运行时替换所有背景像素。如果执行此操作,请将结果保存到磁盘,以便仅产生一次费用。这本质上是我描述的第一个解决方案的DIY。即

// someBitmap is the bitmap you want to manipulate.
// Ensure we're reading from a bitmap in Bgra32 format.
var srcBitmap = someBitmap.Format == PixelFormats.Bgra32 ?
    (BitmapSource)someBitmap : new FormatConvertedBitmap(someBitmap, PixelFormats.Bgra32, null, 0);
// Copy the pixels of the source image to a buffer we can use.
var width = srcBitmap.PixelWidth;
var height = srcBitmap.PixelHeight;
var stride = width * 4;
var byteCount = height * stride;
var srcData= new byte[byteCount];
srcBitmap.CopyPixels(srcData, stride, 0);
var destBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
var destData = new byte[byteCount];
// The channel offsets for PixelFormats.Bgra32.
const int BlueOffset = 0;
const int GreenOffset = 1;
const int RedOffset = 2;
const int AlphaOffset = 3;
// Copy the image, filtering out the background.
for (var y = 0; y < height; y++) { // Each column.
    for (var x = 0; x < width; x++) { // Each row.
        var i = (y * width + x) * 4; // The offset of this pixel in both srcBytes and destBytes.
        var b = srcData[i + BlueOffset];
        var g = srcData[i + GreenOffset];
        var r = srcData[i + RedOffset];
        var a = srcData[i + AlphaOffset];
        // The "filter".
        if (b == 0 && g == 0 && r == 0 && a == 255) {
            // The pixel is solid black(the background color), set to transparent.
            a = 0;
        }
        destData[i + BlueOffset] = b;
        destData[i + GreenOffset] = g;
        destData[i + RedOffset] = r;
        destData[i + AlphaOffset] = a;
    }
}
// Update the final image with the filtered buffer.
destBitmap.WritePixels(new Int32Rect(0, 0, width, height), destData, stride, 0);
// Finally, convert destBitmap to a BitmapImage and use that in place of someBitmap.

请参阅此答案 将destBitmap转换为BitmapImage .

最新更新