From 95b9a6a2aea1f03cfc55954e073023312dff050f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F?= Date: Tue, 21 Apr 2026 09:14:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DImage=20?= =?UTF-8?q?=E5=86=85=E9=83=A8=E5=AD=98=E5=82=A8=E8=A1=8C=E5=AF=B9=E9=BD=90?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=B5=8B=E5=80=BC=E6=97=B6=E7=9A=84=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E4=B8=8D=E5=8C=B9=E9=85=8D=E5=BC=95=E8=B5=B7=E7=9A=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImageProcessing/ImageConverter.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/XplorePlane/Services/ImageProcessing/ImageConverter.cs b/XplorePlane/Services/ImageProcessing/ImageConverter.cs index ed28a2b..539e7d1 100644 --- a/XplorePlane/Services/ImageProcessing/ImageConverter.cs +++ b/XplorePlane/Services/ImageProcessing/ImageConverter.cs @@ -28,11 +28,11 @@ namespace XplorePlane.Services var formatted = new FormatConvertedBitmap(bitmapSource, PixelFormats.Gray8, null, 0); int width = formatted.PixelWidth; int height = formatted.PixelHeight; - int stride = width; - byte[] pixels = new byte[height * stride]; - formatted.CopyPixels(pixels, stride, 0); var image = new Image(width, height); + int stride = image.Bytes.Length / height; + var pixels = new byte[height * stride]; + formatted.CopyPixels(pixels, stride, 0); image.Bytes = pixels; return image; } @@ -40,7 +40,19 @@ namespace XplorePlane.Services public static Image ToEmguCVFromPixels(byte[] pixels, int width, int height) { var image = new Image(width, height); - image.Bytes = pixels; + int required = image.Bytes.Length; + if (pixels.Length == required) + { + image.Bytes = pixels; + } + else + { + int stride = required / height; + var padded = new byte[required]; + for (int row = 0; row < height; row++) + Buffer.BlockCopy(pixels, row * width, padded, row * stride, width); + image.Bytes = padded; + } return image; } @@ -50,8 +62,8 @@ namespace XplorePlane.Services int width = emguImage.Width; int height = emguImage.Height; - int stride = width; byte[] pixels = emguImage.Bytes; + int stride = pixels.Length / height; return BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null, pixels, stride); }