如何在C#中获得不可为空的T以便在函数中使用它?

比约恩·尼克尔(Bjorn Nickel)

这段代码有错误。

    private void Save<T>(string file)
        where T : struct, IPixel<T>
    {
        Image<T> image = Image.LoadPixelData<T>(
            _image.Data, _image.Width, _image.Height);
        image.Save(file);
    }

CS8377 C#类型“ T”必须是非空值类型,以及任何嵌套级别的所有字段,才能将其用作泛型类型或方法中的参数

我使用C#7.3,.Net-Framework 4.6和WPF,此代码适用于Winforms

[编辑]图片来自Sixlabors

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.IO;

namespace DDSReader
{
public class LoadImage
{
    private readonly Pfim.IImage _image;

    public byte[] Data
    {
        get
        {
            if (_image != null)
                return _image.Data;
            else
                return new byte[0];
        }
    }

    public LoadImage(string file)
    {
        _image = Pfim.Pfim.FromFile(file);
        Process();
    }

    public LoadImage(Stream stream)
    {
        if (stream == null)
            throw new Exception("DDSImage ctor: Stream is null");

        _image = Pfim.Dds.Create(stream, new Pfim.PfimConfig());
        Process();
    }

    public LoadImage(byte[] data)
    {
        try
        {
            if (data == null || data.Length <= 0)
                throw new Exception("DDSImage ctor: no data");

            _image = Pfim.Dds.Create(data, new Pfim.PfimConfig());
            Process();
        }
        catch { }
    }

    public void Save(string file)
    {
        try
        {
            if (_image.Format == Pfim.ImageFormat.Rgba32)
                Save<Bgra32>(file);
            else if (_image.Format == Pfim.ImageFormat.Rgb24)
                Save<Bgr24>(file);
            else
                throw new Exception("Unsupported pixel format (" + _image.Format + ")");
        }
        catch { }
    }

    private void Process()
    {
        if (_image == null)
            throw new Exception("DDSImage image creation failed");

        if (_image.Compressed)
            _image.Decompress();
    }


    private void Save<T>(string file)
        where T : struct, IPixel<T>
    {
        Image<T> image = Image.LoadPixelData()

            Image.LoadPixelData<T>(
            _image.Data, _image.Width, _image.Height);
        image.Save(file);
    }

}
}

此程序使用SixlaborsImagesharpFramework从其他DDS格式的文件中读取图像,以获取图像并读取图像PfimFramework

大师斯特隆

尝试使用unmanaged类型约束:

private void Save<T>(string file)
        where T : unmanaged, IPixel<T>

似乎Image<T>来自SixLabors.ImageSharp,您可以在他们的github页面上检查他们正在使用哪些约束

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用SELECT * INTO时获得不可为空的警告

如何在c++中重载=以便在创建对象时使用它?

如何在脚本中使用random.choice,以便在每次关闭按钮后都不会关闭Tkinter窗口而获得不同的结果?

如何在SQL Server中使用DACPAC将可为空的列更新为不可为空

如何制作Stream的副本,以便在使用它时在Java 8中使用副本?

如何访问在公共方法中声明的变量以便在私有方法中使用它

如何在Flutter中默认使用不可为空的?

如何在F#中使用C#可为空的日期时间

如何提取用户在闪亮的应用程序中选择的滑块的值,以便在函数中使用它?

如何使用类型引用可为空或如何在 .NET Core 中预览时启用它

如何将查询中的DateTime转换为String以便在Google可视化图表中使用它?

如何更改 Kafka Rest Proxy CURL 命令以便在浏览器中使用它

如何从数组中提取变量以便在查询参数中使用它们?我在SQL Server Management Studio中使用T-SQL

如何在swiftui中绑定到可为空和不可为空的值

如何在C ++中的类外部定义lambda函数并在类中使用它?

如何改进此日志类,以便可以在C#多个项目中使用它?

如何分解此SVG字体,以便在Chrome中以编程方式使用它?

无法获得不可为空的功能来处理颤振

如何对两个Maxima / LISP列表进行元素求和,以便在Moodle STACK考试中嵌入的JSXgraph中使用它们?

VueJS / Quasar:如何在“安装的”函数中传递从FirebaseDB获取的数据,并在“数据”中使用它,以便可以在HTML中显示它

如何在WPF(C#/ XAML)中获得不同的高级触摸手势?

如何将VBA long&HFFFFFFF0转换为uint以便在C#中使用?

如何在Kotlin中使用可为空的“ beanName”实现BeanPostProcessor?

如何在Kotlin中声明可为空的函数参数

如何在 QGraphicsItem 中插入指针,以便在获得选定指针时访问它们?

如何将文件复制到目标,以便在运行任务中使用它们?

如何在 C# 中传入可为空的修饰符作为输出参数

如何在选择器中获得不同的状态或如何在选择器中使用选择器

c# - 如何在c#中的非异步函数中使用await调用异步函数?