C# 读取图片传递给 C++ 编写的动态库

  1. C#读取图片转成RGB格式。先贴上代码:
Bitmap bmpInitial = (Bitmap)System.Drawing.Image.FromFile("1.jpg");
Bitmap bmp = null;
 //将图像转换为24位rgb图像
if (bmpInitial.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
	bmp = new Bitmap(bmpInitial.Width, bmpInitial.Height,System.Drawing.Imaging.PixelFormat.Format24bppRgb);
	using (Graphics g = Graphics.FromImage(bmp))
	{
		g.DrawImage(bmpInitial, new Rectangle(0, 0, bmpInitial.Width, bmpInitial.Height));
	}  
}
else
{
	 bmp = bmpInitial;
}
//设置dpi=96
bmp.SetResolution(96, 96);

System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
//Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap. 
int bytesLength = Math.Abs(bmpData.Stride) * bmp.Height;
int imageWeidth = bmp.Width;
int imageHeight = bmp.Height;
//图像的Stride
int imageStride = bmpData.Stride;
byte[] buffer = new byte[bytesLength];
// Copy the RGB values into the array.
Marshal.Copy(ptr, buffer, 0, bytesLength);
bmp.UnlockBits(bmpData);
 

C++ 代码

Mat ByteToMat(BYTE* pImg, int nH, int nW,int stride)
{
    Mat image = Mat(nH, nW, CV_8UC3, pImg,stride).clone();
    return image;
}

 

[DllImport("VesselExportCp.dll", EntryPoint = "GetResult", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] public static extern void GetResult(byte[] bimage, int nH, int nW, int stride, [In, Out] ref StructResult result);

extern "C" VES_API void __stdcall GetResult(BYTE* bimage, int nH, int nW, int stride, StructResult* result);
 

https://blog.csdn.net/zhaojia_carol/article/details/86080471

https://blog.csdn.net/weixin_42112458/article/details/83117305

https://blog.csdn.net/rome12/article/details/90025439

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值