Skip to content
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

Saving as image file from SIPSorceryMedia.Abstractions.RawImage #45

Closed
mail2mhossain opened this issue Sep 29, 2022 · 5 comments
Closed

Comments

@mail2mhossain
Copy link

How to save a image file from SIPSorceryMedia.Abstractions.RawImage?

Is it possible to create MemoryStream from SIPSorceryMedia.Abstractions.RawImage?

Actually want to convert SIPSorceryMedia.Abstractions.RawImage to Avalonia.Media.Imaging.Bitmap.

Any help in this regard.

@mail2mhossain
Copy link
Author

Tried with:

        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'

@mail2mhossain
Copy link
Author

Tried with:

        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'

@mail2mhossain
Copy link
Author

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?

@mail2mhossain
Copy link
Author

If I can get IList bgraPixelData from SIPSorceryMedia.Abstractions.RawImage then it can be converted to Avalonia.Media.Imaging.WriteableBitmap

public static WriteableBitmap CreateBitmapFromPixelData (
IList bgraPixelData,
int pixelWidth,
int pixelHeight)
{
Vector dpi = new Vector(96, 96);

        var bitmap = new WriteableBitmap(
            new PixelSize(pixelWidth, pixelHeight),
            dpi,
            Avalonia.Platform.PixelFormat.Rgba8888,
            AlphaFormat.Premul);

        // Warning: This is highly questionable
        using (var frameBuffer = bitmap.Lock())
        {
            Marshal.Copy(bgraPixelData.ToArray(), 0, frameBuffer.Address, bgraPixelData.Count);
        }

        return bitmap;
    }

In SIPSorceryMedia.Abstractions.PixelConverter class there are several pixel format conversion functions. But I am looking for RGBToBGRA. Any Help?

@ChristopheI
Copy link
Collaborator

Nearly the same question than #47

Please read my answer: #47 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants