C# 的随手笔记 1 - PInvoke C++转C# 读取 image的方法

背景知识

C# 的随手笔记 1 - DLL的 C Array to C# Array (PInvoke)

OpenCV 笔记 5.1 OPENCV的Array 使用Bitmap在C#的 pictureBox (指标方式 BitmapData )

假设有个 C++ 读img的function

tmp_char_Path 图片位置

tmp_Image输出图片buf

 void Image_read(const char* tmp_char_Path, unsigned char* tmp_Image,int &tmp_rows, int &tmp_cols)

用C#去接就是

PInvoke  C++

[DllImport("Undistort_Project_DLL1.dll", EntryPoint = "Image_read", CallingConvention = CallingConvention.Cdecl)]
private static extern void Image_read(IntPtr tmp_char_Path, IntPtr tmp_Image, ref int tmp_rows, ref int tmp_cols);

 

接收的档案 C#

namespace Read_IMG
{
    public partial class Read_IMG_C
    {
    	
        public void Image_RGB_Get(string Path_string, ref byte[] tmp_Camera_Buffer, ref int tmp_rows, ref int tmp_cols)
        {
            int len = tmp_rows * tmp_cols * 3;
            IntPtr Path_IntPtr = Marshal.StringToHGlobalAnsi(Path_string);
            IntPtr Image_IntPtr = Marshal.AllocHGlobal(len);

            Image_read(Path_IntPtr, Image_IntPtr, ref tmp_rows, ref tmp_cols);

            Marshal.Copy(Image_IntPtr, tmp_Camera_Buffer, 0, len);

            Marshal.FreeHGlobal(Image_IntPtr);
            Marshal.FreeHGlobal(Path_IntPtr); 
        }
        
        [DllImport("Read_IMG.dll", EntryPoint = "Image_read", CallingConvention = CallingConvention.Cdecl)]
        private static extern void Image_read(IntPtr tmp_char_Path, IntPtr tmp_Image, ref int tmp_rows, ref int tmp_cols);
        
    }
}

 

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章

5 点赞(415) 阅读(67)