We are migrate to .NET 6. After this converting image with Mat.FromStream() return exception:
Native object address is NULL
. I make small project where this exception is reproduce. Hope that you can help me with this.
Here is the link for small example: https://drive.google.com/file/d/1t-BM1uxUoZ1NOsGHIhPYnCCyvLtN_er2/view?usp=sharing
can you please paste your code here not on some dropbox ? thank you.
I’m loading image from file. This code is working fine on .NET Framework 4.8 but stop working when we migrate to .NET 6
var imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"ImagesTest\Test.jpg");
FileStream imageStream = new FileStream(imagePath, FileMode.Open);
OpenCvImageWorker worker = new OpenCvImageWorker();
var result = worker.LoadAsync(imageStream, CancellationToken.None).GetAwaiter().GetResult();
OpenCvImageWorker based on OpenCVSharp nuget:
public async Task<ImageContainer> LoadAsync(Stream imageStream, CancellationToken cancellationToken)
{
if (imageStream == null)
throw new ArgumentNullException(nameof(imageStream));
if (imageStream == Stream.Null)
throw new InvalidOperationException("Stream is empty");
imageStream.ToBegin();
cancellationToken.ThrowIfCancellationRequested();
return await Task.FromResult(new MatImageContainer(Mat.FromStream(imageStream, ImreadModes.AnyColor)));
}
public class MatImageContainer : IDisposable
{
private Lazy<BitmapSource> bitmapSource;
public MatImageContainer(Image image)
: this((Bitmap)image)
{
}
public MatImageContainer(Bitmap bitmap)
: this(bitmap.ToMat())
{
}
public MatImageContainer(Mat image)
{
OriginalImage = image;
bitmapSource = new Lazy<BitmapSource>(() => OriginalImage.ToBitmapSource());
}
public Mat OriginalImage { get; }
public BitmapSource Image
{
get => bitmapSource?.Value;
set => throw new NotImplementedException();
}
public BitmapSource Thumbnail
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public void Dispose()
{
bitmapSource = null;
if (!OriginalImage.IsDisposed)
OriginalImage.Dispose();
}
}
I try but its moved into a spam
I’m reading photo from file into a stream. This code is working well on .NET Framework 4.8 but not working on .NET Core 3.1, .NET 5, .NET 6
var imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"ImagesTest\Test.jpg");
FileStream imageStream = new FileStream(imagePath, FileMode.Open);
OpenCvImageWorker worker = new OpenCvImageWorker();
var result = worker.LoadAsync(imageStream, CancellationToken.None).GetAwaiter().GetResult();
OpenCvImageWorker:
public class OpenCvImageWorker
{
public ImageContainer[] Load(string fileName)
{
if (String.IsNullOrWhiteSpace(fileName))
throw new ArgumentNullException(nameof(fileName));
if (!File.Exists(fileName))
throw new FileNotFoundException("File not found", fileName);
if (!Cv2.ImReadMulti(fileName, out Mat[] mats))
throw new InvalidOperationException("OpenCV can't open file");
// ReSharper disable once CoVariantArrayConversion
return mats.Select(m => new MatImageContainer(m)).ToArray();
}
public async Task<ImageContainer[]> LoadAsync(string fileName)
=> await Task.FromResult(Load(fileName));
public async Task<ImageContainer> LoadAsync(Stream imageStream, CancellationToken cancellationToken)
{
if (imageStream == null)
throw new ArgumentNullException(nameof(imageStream));
if (imageStream == Stream.Null)
throw new InvalidOperationException("Stream is empty");
imageStream.ToBegin();
cancellationToken.ThrowIfCancellationRequested();
return await Task.FromResult(new MatImageContainer(Mat.FromStream(imageStream, ImreadModes.AnyColor)));
}
}
public class MatImageContainer : ImageContainer
{
private Lazy<BitmapSource> bitmapSource;
public MatImageContainer(Image image)
: this((Bitmap)image)
{
}
public MatImageContainer(Bitmap bitmap)
: this(bitmap.ToMat())
{
}
public MatImageContainer(Mat image)
{
OriginalImage = image;
bitmapSource = new Lazy<BitmapSource>(() => OriginalImage.ToBitmapSource());
}
public Mat OriginalImage { get; }
public override BitmapSource Image
{
get => bitmapSource?.Value;
set => throw new NotImplementedException();
}
public override BitmapSource Thumbnail
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override void Dispose()
{
base.Dispose();
bitmapSource = null;
if (!OriginalImage.IsDisposed)
OriginalImage.Dispose();
}
}
public class ImageContainer : IDisposable
{
public virtual BitmapSource Image { get; set; }
public virtual BitmapSource Thumbnail { get; set; }
public virtual void Dispose()
{
}
}