Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mail2mhossain opened this issue Oct 2, 2022 · 7 comments
Labels

Comments

@mail2mhossain
Copy link

mail2mhossain commented Oct 2, 2022

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
Copy link
Contributor

jp2masa commented Oct 2, 2022

What is the value of rawImage.PixelFormat?

@mail2mhossain
Copy link
Author

mail2mhossain commented Oct 3, 2022

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
Copy link
Author

mail2mhossain commented Oct 10, 2022

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
Copy link
Member

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
Copy link
Member

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

@mail2mhossain
Copy link
Author

mail2mhossain commented Oct 10, 2022

"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
Copy link
Member

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

@AvaloniaUI AvaloniaUI locked and limited conversation to collaborators Nov 2, 2022
@timunie timunie converted this issue into discussion #9328 Nov 2, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

3 participants