Closed pzs7602 closed 1 year ago
Is there a similar method, constructor or factory in System.Drawing.Bitmap which you would use to achieve this in the past? https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap?view=windowsdesktop-7.0
The method would be implemented in macOS/Linux/Windows, so System.Drawing.Bitmap is not the right way, that's why I choose to use AnyBitmap.
@pzs7602 Please provide the equivalent method that existed in System.Drawing.Bitmap
// buffer size = width*height*3
public static Avalonia.Media.Imaging.Bitmap GetBitmapFromRGBBuffer(byte[] buffer, int width, int height)
{
System.Drawing.Bitmap b = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Rectangle BoundsRect = new System.Drawing.Rectangle();
BoundsRect.Width = width;
BoundsRect.Height = height;
BitmapData bmpData = b.LockBits(BoundsRect, ImageLockMode.WriteOnly, b.PixelFormat);
IntPtr ptr = bmpData.Scan0;
// add back dummy bytes between lines, make each line be a multiple of 4 bytes
int skipByte = bmpData.Stride - width*3;
byte[] newBuff = new byte[buffer.Length + skipByte*height];
for (int j = 0; j < height; j++)
{
Buffer.BlockCopy(buffer, j * width * 3, newBuff, j * (width * 3 + skipByte), width * 3);
}
// fill in rgbValues
Marshal.Copy(newBuff, 0, ptr, newBuff.Length);
b.UnlockBits(bmpData);
return ConvertToAvaloniaBitmap(b);
}
// conversion between System.Drawing.Bitmap and Avalonia.Media.Imaging.Bitmap
public static Avalonia.Media.Imaging.Bitmap ConvertToAvaloniaBitmap(System.Drawing.Bitmap bitmap)
{
if (bitmap == null)
return null;
System.Drawing.Bitmap bitmapTmp = bitmap;
var bitmapdata = bitmapTmp.LockBits(new Rectangle(0, 0, bitmapTmp.Width, bitmapTmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
Avalonia.Media.Imaging.Bitmap bitmap1 = new Avalonia.Media.Imaging.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();
return bitmap1;
}
// conversion between Avalonia.Media.Imaging.Bitmap and IronSoftware.Drawing.AnyBitmap
public static Avalonia.Media.Imaging.Bitmap ConvertToAvaloniaBitmap(AnyBitmap bitmap)
{
if (bitmap == null)
return null;
var bitmapdata = bitmap.ToStream();
bitmapdata.Position = 0;
Avalonia.Media.Imaging.Bitmap bitmap1 = new Avalonia.Media.Imaging.Bitmap(bitmapdata);
return bitmap1;
}
@pzs7602 This is fixed in the latest release.
I want to implement a method: AnyBitmap GetBitmapFromRGBBuffer(byte[] buffer, int width, int height) The buffer is image RGB data(length=widthheight3) Thanks in advance.