- 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