在c#中固定对象时出现意外行为

时间:2017-11-25 17:23:25

标签: c# .net imagesharp

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

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

1 个答案:

答案 0 :(得分: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]索引器和Fill像素区域可用的方法,API已经可以通过API获得每像素访问权限。我强烈建议你使用现有的方法。