AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.15k stars 2.27k forks source link

Converting as image file from SIPSorceryMedia.Abstractions.RawImage to Avalonia.Media.Imaging.Bitmap #9071

Closed mail2mhossain closed 2 years ago

mail2mhossain commented 2 years ago

I am using SIPSorceryMedia.FFmpeg (https://github.com/sipsorcery-org/SIPSorceryMedia.FFmpeg) in one of my project. They exposed video frame as SIPSorceryMedia.Abstractions.RawImage. Becuase System.Drawing.Image is not a cross platform.

Now I want to convert SIPSorceryMedia.Abstractions.RawImage to Avalonia.Media.Imaging.Bitmap. So that I can display the image in Avalonia App. I have tried this way:

Bitmap avalonia_bitmap = new Bitmap(Avalonia.Platform.PixelFormat.Rgb565, Avalonia.Platform.AlphaFormat.Premul, rawImage.Sample, new Avalonia.PixelSize(rawImage.Width, rawImage.Height), new Avalonia.Vector(96, 96), rawImage.Stride);

but no luck.

If I change Avalonia.Platform.PixelFormat.Bgra8888 or Avalonia.Platform.PixelFormat.Rgba8888, getting error 'Unable to create bitmap from provided data'

Any help in this regards. [https://github.com/sipsorcery-org/SIPSorceryMedia.FFmpeg/issues/45]

jp2masa commented 2 years ago

What is the value of rawImage.PixelFormat?

mail2mhossain commented 2 years ago

SIPSorceryMedia.Abstractions.VideoPixelFormatsEnum.Rgb

public enum VideoPixelFormatsEnum { Rgb = 0, // 24 bits per pixel. Bgr = 1, // 24 bits per pixel. Bgra = 2, // 32 bits per pixel. I420 = 3, // 12 bits per pixel. NV12 = 4, // 12 bits per pixel. }

public class RawImage { public RawImage ();

public int Width { get; set; } public int Height { get; set; } public int Stride { get; set; } public IntPtr Sample { get; set; } public VideoPixelFormatsEnum PixelFormat { get; set; }

public byte[] GetBuffer (); }

mail2mhossain commented 2 years ago

I have converted SIPSorceryMedia.Abstractions.RawImage rawImage to System.Drawing.Bitmap with the following code:

Bitmap bitmap = new Bitmap(rawImage.Width, rawImage.Height, rawImage.Stride, PixelFormat.Format24bppRgb, rawImage.Sample);

After that converted this to Avalonia.Media.Imaging.Bitmap with the following code:

System.Drawing.Bitmap bitmapTmp = new System.Drawing.Bitmap(bitmap); var bitmapdata = bitmapTmp.LockBits(new Rectangle(0, 0, bitmapTmp.Width, bitmapTmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); Bitmap bitmap1 = new Bitmap(Avalonia.Platform.PixelFormat.Bgra8888, Avalonia.Platform.AlphaFormat.Premul, bitmapdata.Scan0, new Avalonia.PixelSize(bitmapdata.Width, bitmapdata.Height), new Avalonia.Vector(96, 96), bitmapdata.Stride); bitmapTmp.UnlockBits(bitmapdata); bitmapTmp.Dispose();

AND it is working in Windows platform. To work in cross-platform (Win, Linux, OSX), I want to convert SIPSorceryMedia.Abstractions.RawImage rawImage to Avalonia.Media.Imaging.Bitmap directly. Any help in this regards?

maxkatz6 commented 2 years ago

Try to create it with WriteableBitmap by copying pixel data to the locked buffer. Like here https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls.ColorPicker/Helpers/ColorPickerHelpers.cs#L606-L627

maxkatz6 commented 2 years ago

Also, important to find out correct PixelFormat that RawImage is using, by converting VideoPixelFormatsEnum I assume.

mail2mhossain commented 2 years ago

"Also, important to find out correct PixelFormat that RawImage is using, by converting VideoPixelFormatsEnum I assume."

I think here I am stuck. Need more help in this regards.

maxkatz6 commented 2 years ago

Unfortunately, I don't have experience with this library. If you have any more questions about Avalonia, I can try to help/answer.