如果直接将内存中的Bitmap 传给C++,原图会失去颜色,如下:
代码如下:
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
Loger loger = new Loger();
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, xianWeiJingCanShu.picLevel);
myEncoderParameters.Param[0] = myEncoderParameter;
picID._bitmap.Save($@"D:\检测系统图像\{picID.id}\图像\{pointNo}_{picID.picture_name}.jpg",jpgEncoder,myEncoderParameters);//,ImageFormat.Jpeg {picID.id}_ //imageCount++; {pointNo}_{picID.imageCount.ToString("D2")}
if (isShiBie)
{
picID._bitmap.Dispose();
return;
}
// loger.WriteImg(Encoding.UTF8.GetString(picID._bitmap.ToBytes(ImageFormat.Jpeg).ToArray()), picID.id+"yuan");
//return;
// Bitmap bmp = new Bitmap($@"D:\检测系统图像\{picID.id}\图像\{pointNo}_{picID.picture_name}.jpg");
// picID._bitmap = bmp;
// loger.WriteImg(Encoding.UTF8.GetString(picID._bitmap.ToBytes(ImageFormat.Jpeg).ToArray()), picID.id + "disk");
Bitmap bmpInitial = new Bitmap(picID._bitmap);
//将图像转换为24位rgb图像
if (bmpInitial.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
Console.WriteLine("转换8to24:{0}", path);
picID._bitmap = new Bitmap(bmpInitial.Width, bmpInitial.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(picID._bitmap))
{
g.DrawImage(bmpInitial, new Rectangle(0, 0, bmpInitial.Width, bmpInitial.Height));
}
}
else
{
picID._bitmap = bmpInitial;
}
// picID._bitmap = bmp;
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, picID._bitmap.Width, picID._bitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = picID._bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, picID._bitmap.PixelFormat);
//System.Drawing.Imaging.PixelFormat pf = picID._bitmap.PixelFormat;
//if (pf == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
//{
// stride = picID._bitmap.Width * 4;
//}
//else
//{
// stride = picID._bitmap.Width * 3;
//}
int channel = 4;
if (picID._bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
{
channel = 1;
}
else if (picID._bitmap.PixelFormat == PixelFormat.Format24bppRgb)
{
channel = 3;
}
else if (picID._bitmap.PixelFormat == PixelFormat.Format32bppArgb)
{
channel = 4;
}
stride = channel;
loger.WriteLog(DateTime.Now.ToString("G"));
loger.WriteLog($"开始识别------{picID.imageCount}--------"+picID.id);
byte[] error = new byte[1024 * 10];
var s = "";
int result = -1;
// IntPtr ptrRet =(IntPtr) 0;
IntPtr ptrIn = Marshal.StringToHGlobalAnsi($@"{pointNo}_{picID.picture_name}");//($@"{picID.id}_{pointNo}_{ picID.imageCount.ToString("D2")}"); {picID.id}_
result = recogCell_mat_nopath(ptrIn, (IntPtr)bmpData.Scan0, picID._bitmap.Width, picID._bitmap.Height, stride, ref error[0]);
loger.WriteLog($"识别结束--result:{result}----{picID.picture_name}--------" + picID.id);
代码中保存硬盘的图片是彩色的,如果没有8转24位rgb这一段,C++收到的就可能是灰色的
Bitmap bmpInitial = new Bitmap(picID._bitmap);
//将图像转换为24位rgb图像
if (bmpInitial.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
Console.WriteLine("转换8to24:{0}", path);
picID._bitmap = new Bitmap(bmpInitial.Width, bmpInitial.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(picID._bitmap))
{
g.DrawImage(bmpInitial, new Rectangle(0, 0, bmpInitial.Width, bmpInitial.Height));
}
}
else
{
picID._bitmap = bmpInitial;
}
最后效果: