Reputation: 23
got an error in this code.
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# The type 'T' must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter in the generic type or method
Im using C#7.3, .Net-Framework 4.6 and wpf this code works with winforms
[EDIT] Image is from 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);
}
}
}
this Program reads an Image from an other File which is in DDS-Format Im Using the SixlaborsImagesharpFramework to get an Image and to read the image the PfimFramework
Upvotes: 2
Views: 1855
Reputation: 141940
Try using unmanaged
type constraint:
private void Save<T>(string file)
where T : unmanaged, IPixel<T>
It seems that Image<T>
comes from SixLabors.ImageSharp
, you can check which constraints they are using at their github page.
Upvotes: 4