У меня есть цикл, который получает данные изображения в виде объекта Bitmap, которым мне нужно быстро манипулировать, чтобы обработать следующий кадр.
Рассмотрим следующий код:
private static void Main ()
{
var opacity = (byte) 127;
var channelAlpha = (int) 3;
var pixelFormat = PixelFormat.Format32bppArgb;
var boundsSource = Screen.PrimaryScreen!.Bounds;
var copyPixelOperation = CopyPixelOperation.SourceCopy;
var boundsTarget = new Rectangle (x: 0, y: 0, width: boundsSource.Width, height: boundsSource.Height);
using (var bitmap = new Bitmap (boundsSource.Width, boundsSource.Height, pixelFormat))
{
using (var graphics = Graphics.FromImage (bitmap))
graphics.CopyFromScreen (boundsSource.X, boundsSource.Y, boundsTarget.X, boundsTarget.Y, boundsSource.Size, copyPixelOperation);
var bitmapData = bitmap.LockBits (rect: boundsTarget, flags: ImageLockMode.ReadWrite, format: pixelFormat);
var buffer = new byte [Math.Abs (bitmapData.Stride) * bitmapData.Height];
// Reduce opacity.
Marshal.Copy (source: bitmapData.Scan0, destination: buffer, startIndex: 0, length: buffer.Length);
for (int i = 0; i < buffer.Length; i += 4) { buffer [i + channelAlpha] = opacity; }
Marshal.Copy (source: buffer, destination: bitmapData.Scan0, startIndex: 0, length: buffer.Length);
bitmap.UnlockBits (bitmapData);
}
}
Я хотел бы знать, есть ли какой-нибудь способ избежать копирования буфера изображения для манипулирования им. Я знаю, что могу использовать небезопасный код для приведения bitmapData.Scan0 к любому указателю. Мой вопрос: можно ли использовать Span или Memory, избегая при этом Marshall.Copy и небезопасного кода?
У меня есть цикл, который получает данные изображения в виде объекта Bitmap, которым мне нужно быстро манипулировать, чтобы обработать следующий кадр. Рассмотрим следующий код: [code]private static void Main () { var opacity = (byte) 127; var channelAlpha = (int) 3; var pixelFormat = PixelFormat.Format32bppArgb; var boundsSource = Screen.PrimaryScreen!.Bounds; var copyPixelOperation = CopyPixelOperation.SourceCopy; var boundsTarget = new Rectangle (x: 0, y: 0, width: boundsSource.Width, height: boundsSource.Height);
using (var bitmap = new Bitmap (boundsSource.Width, boundsSource.Height, pixelFormat)) { using (var graphics = Graphics.FromImage (bitmap)) graphics.CopyFromScreen (boundsSource.X, boundsSource.Y, boundsTarget.X, boundsTarget.Y, boundsSource.Size, copyPixelOperation);
var bitmapData = bitmap.LockBits (rect: boundsTarget, flags: ImageLockMode.ReadWrite, format: pixelFormat); var buffer = new byte [Math.Abs (bitmapData.Stride) * bitmapData.Height];
// Reduce opacity. Marshal.Copy (source: bitmapData.Scan0, destination: buffer, startIndex: 0, length: buffer.Length); for (int i = 0; i < buffer.Length; i += 4) { buffer [i + channelAlpha] = opacity; } Marshal.Copy (source: buffer, destination: bitmapData.Scan0, startIndex: 0, length: buffer.Length);
bitmap.UnlockBits (bitmapData); } } [/code] Я хотел бы знать, есть ли какой-нибудь способ избежать копирования буфера изображения для манипулирования им. Я знаю, что могу использовать небезопасный код для приведения bitmapData.Scan0 к любому указателю. Мой вопрос: можно ли использовать Span или Memory, избегая при этом Marshall.Copy и небезопасного кода?