修复Image<Gray, byte> 内部存储行对齐导致赋值时的长度不匹配引起的异常
This commit is contained in:
@@ -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<Gray, byte>(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<Gray, byte> ToEmguCVFromPixels(byte[] pixels, int width, int height)
|
||||
{
|
||||
var image = new Image<Gray, byte>(width, height);
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user