0

当我执行此代码时,我正在使用 C# 的 ImageSharp lib

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Advanced;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace ImageSharp
{
    public class Program
    {
        public static void Main()
        {
            Image<Rgba32> img = null;
            using (var imageFileStream = new FileStream(/*Any jpg image.*/@"E:\cat\0.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                img = Image.Load(imageFileStream);
            }
            int length = img.Height * img.Width / 2;
            //Rgba32[] colors = typeof(Rgba32).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).Where(a => a.FieldType == typeof(Rgba32)).Select(a => (Rgba32)a.GetValue(null)).ToArray();
            Span<Rgba32> buffer = Span<Rgba32>.Empty;
            GCHandle bufferHandle = GCHandle.Alloc(img.DangerousGetPinnableReferenceToPixelBuffer(), GCHandleType.Pinned);
            unsafe
            {
                buffer = new Span<Rgba32>(bufferHandle.AddrOfPinnedObject().ToPointer(), length);
            }
            for (int i = 0; i < length; i++)
            {
                buffer[i] = Rgba32.Yellow;
                Console.WriteLine(i);//exception thrown here
            }
            buffer = Span<Rgba32>.Empty;
            bufferHandle.Free();
            GC.Collect();
            using (var imageFileStream = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), @"ImageSharp.jpg"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read))
            {
                img.SaveAsJpeg(imageFileStream);
                imageFileStream.Flush();
            }
            Console.WriteLine("Done!");
            Console.ReadLine();
        }
    }
}

我得到了这个异常(System.IO.IOException:'句柄无效。')。如果你删除抛出异常的行,程序就会挂起(我认为它会挂在循环内)。

所以我的问题是是什么导致了这个异常,为什么当你删除“Console.WriteLine”时程序会挂起?

项目中唯一的依赖是 ImageSharp nuget 包。框架版本:4.7.1

4

1 回答 1

1

看起来您正在尝试固定缓冲区,并使用Span<T>

你的错误就在这里。

GCHandle.Alloc(img.DangerousGetPinnableReferenceToPixelBuffer(), GCHandleType.Pinned);

您正在对表示图像第一个像素的结构进行装箱,并GCHandle为该装箱对象实例创建一个。

如果你真的需要固定缓冲区,我建议你这样做。

fixed (Rgba32* ptr = &img.DangerousGetPinnableReferenceToPixelBuffer())
{
    Console.WriteLine(ptr[42]); // no Span<T> needed!
}

但是......我真的不明白你为什么要这样做。Image<TPixel>[x,y]已经可以通过 API 使用像素区域可用的索引器和方法进行逐像素访问Fill。我强烈建议您使用可用的方法。

于 2018-02-07T01:13:32.663 回答