

无法正常出图
public bool Process()
{
try
{
ImageData image = new ImageData();
ImageData image2 = new ImageData();
// 捕获 GetImageValue 可能的异常
try
{
GetImageValue("in0", ref image);
}
catch (Exception ex)
{
ShowMessageBox("获取输入图片in0失败: " + ex.Message);
throw;
}
try
{
GetImageValue("in1", ref image2);
}
catch (Exception ex)
{
ShowMessageBox("获取输入图片in1失败: " + ex.Message);
throw;
}
// 捕获 ImageDataToBitMap 转换异常
Bitmap bmp = null;
Bitmap bmp2 = null;
try
{
bmp = ImageDataToBitMap(image);
bmp2 = ImageDataToBitMap(image2);
}
catch (Exception ex)
{
ShowMessageBox("图片转Bitmap失败: " + ex.Message);
throw;
}
using (bmp)
using (bmp2)
{
// 捕获垂直拼接异常
Bitmap mergedBmp = null;
try
{
mergedBmp = MergedBitmapsVertically(bmp, bmp2);
}
catch (Exception ex)
{
ShowMessageBox("垂直拼接图片失败: " + ex.Message);
throw;
}
using (mergedBmp)
{
// 捕获 Bitmap转ImageData 异常
ImageData data1 = null;
try
{
data1 = BitmapToImageData(mergedBmp, image.PixelFormat);
}
catch (Exception ex)
{
ShowMessageBox("Bitmap转ImageData失败: " + ex.Message);
throw;
}
out0 = data1;
}
}
}
catch (Exception ex)
{
ShowMessageBox("主流程出错: " + ex.Message);
}
return true;
}
private ImageData BitmapToImageData(Bitmap mergedBmp, ImagePixelFormate pixelFormat)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
ImageFormat format = ImageFormat.Bmp;
mergedBmp.Save(ms, format);
return new ImageData
{
Buffer = ms.ToArray(),
Width = mergedBmp.Width,
Height = mergedBmp.Height,
PixelFormat = pixelFormat
};
}
}
catch (Exception ex)
{
ShowMessageBox("BitmapToImageData 内部错误: " + ex.Message);
throw;
}
}
private Bitmap MergedBitmapsVertically(Bitmap bmp, Bitmap bmp2)
{
try
{
// 校验宽度一致性
if (bmp.Width != bmp2.Width)
{
throw new ArgumentException("两张图片宽度不一致!bmp宽:" + bmp.Width + ",bmp2宽:" + bmp2.Width);
}
int mergedWidth = bmp.Width;
int mergedHeight = bmp.Height + bmp2.Height;
Bitmap mergedBmp = new Bitmap(mergedWidth, mergedHeight, bmp.PixelFormat);
using (Graphics g = Graphics.FromImage(mergedBmp))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.Low;
g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
g.DrawImage(bmp2, new Rectangle(0, bmp.Height, bmp2.Width, bmp2.Height));
}
return mergedBmp;
}
catch (Exception ex)
{
ShowMessageBox("MergedBitmapsVertically 内部错误: " + ex.Message);
throw;
}
}
private Bitmap ImageDataToBitMap(ImageData image)
{
try
{
// 校验像素格式是否为RGB24(对应枚举值35127316)
if (image.PixelFormat != ImagePixelFormate.RGB24)
{
throw new NotSupportedException("仅支持RGB24格式,当前格式:" + image.PixelFormat.ToString());
}
// 校验原始数据长度是否匹配:宽×高×3字节(RGB24每个像素3字节)
int expectedBufferLength = image.Width * image.Height * 3;
if (image.Buffer == null || image.Buffer.Length != expectedBufferLength)
{
throw new InvalidDataException("原始像素数据长度不匹配!预期:" + expectedBufferLength + ",实际:" + (image.Buffer == null ? 0 : image.Buffer.Length));
}
// 创建Bitmap(RGB24格式对应Format24bppRgb)
Bitmap bmp = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// 锁定Bitmap的像素缓冲区,写入原始数据
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
try
{
// 将原始RGB24数据复制到Bitmap缓冲区
System.Runtime.InteropServices.Marshal.Copy(image.Buffer, 0, bmpData.Scan0, image.Buffer.Length);
}
finally
{
// 解锁像素缓冲区
bmp.UnlockBits(bmpData);
}
return bmp;
}
catch (Exception ex)
{
ShowMessageBox("ImageDataToBitMap 内部错误: " + ex.Message);
throw;
}
}
}