C# 调用C++的dll进行图像加载存储

C++图像处理函数

// TestBitMap.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
extern "C" int _declspec (dllexport) readBitmap(void ** pBmpBuf,BITMAPINFO &bm);
int _declspec (dllexport) readBitmap(void ** pBmpBuf,BITMAPINFO &bm)
{

 FILE *fp=fopen("工区0110.bmp","rb");//二进制读方式打开指定的图像文件
 
    if(fp==0) return 0;
 
    //跳过位图文件头结构BITMAPFILEHEADER
 
 //  fseek(fp, sizeof(BITMAPFILEHEADER),0);
 BITMAPFILEHEADER bmpFileHeader;
 fread(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp); 
    //定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
 
 
 BITMAPINFOHEADER bmpInfoHeader;
    fread(&bmpInfoHeader, sizeof(BITMAPINFOHEADER), 1,fp); //获取图像宽、高、每像素所占位数等信息
 
    int bmpWidth = bmpInfoHeader.biWidth;
 
    int bmpHeight = bmpInfoHeader.biHeight;
 
    int biBitCount = bmpInfoHeader.biBitCount;//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
 
    int lineByte=(bmpWidth * biBitCount/8+3)/4*4;//灰度图像有颜色表,且颜色表表项为256

 bm.bmiHeader = bmpInfoHeader;
 
    if(biBitCount==8)
 {
  
        //申请颜色表所需要的空间,读颜色表进内存
  
        RGBQUAD *pColorTable=new RGBQUAD[256];
  
        fread(pColorTable,sizeof(RGBQUAD),256,fp);
  delete []pColorTable;
  
 }
 
    //申请位图数据所需要的空间,读位图数据进内存
 
   // pBmpBuf=new unsigned char[lineByte * bmpHeight];
// *pBmpBuf = malloc(lineByte*bmpHeight);
//  if (*pBmpBuf == NULL)
//  {
//   return -1;
//  }
     fread(*pBmpBuf,1,lineByte * bmpHeight,fp);

    fclose(fp);//关闭文件
 
    return 1;//读取文件成功
}

C# 调用

        [DllImport("TestBitMap", EntryPoint = "readBitmap",CallingConvention = CallingConvention.Cdecl)]
        public static extern int readBitmap(ref IntPtr a,ref BITMAPINFO bm);

 public int getBitmap()
        {          

           BITMAPINFO bm = new BITMAPINFO();
           // int d = 0;
           IntPtr e = Marshal.AllocHGlobal(3000*89);//3000*89为实际位图数据的大小
           int x = readBitmap(ref e,ref bm);

         int bmpWidth = bm.bmiHeader.biWidth;
            int bmpHeight = bm.bmiHeader.biHeight;
            int bmpBitCount = bm.bmiHeader.biBitCount;
            int stride = (bmpWidth * bmpBitCount / 8 + 3) / 4 * 4;
           
            Bitmap bt = new Bitmap(bmpWidth, bmpHeight, stride, PixelFormat.Format24bppRgb, e);//存储为24位图像
            bt.Save("testBt.bmp");

 

//其他操作

       int x = readBitmap(ref e,ref bm);
        //    Bitmap bt1 = new Bitmap("工区0110.bmp");
        //    Bitmap bt2 = new Bitmap("工区0120.bmp");
        //    BitmapData bd = new BitmapData();
        //    Rectangle rect = new Rectangle(0, 0, bt1.Width, bt1.Height);

        //    System.Drawing.Imaging.BitmapData bmpData1 =
        //bt1.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
        //bt1.PixelFormat);
        //    System.Drawing.Imaging.BitmapData bmpData2 =
        //bt2.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
        //bt2.PixelFormat);

           // bmpData1 = bmpData2;
           // int bytes  = bmpData1.Stride * bmpData1.Height;
           // byte[] rgbValues = new byte[bytes];
           // IntPtr testptr = bmpData1.Scan0;
           // Marshal.Copy(bmpData1.Scan0, rgbValues, 0, bytes);
           // Marshal.Copy(rgbValues, 0, bmpData2.Scan0, bytes);
           Marshal.Copy(bmpData1.Scan0, 0, bmpData2.Scan0, bmpData1.Stride * bmpData1.Height);
           // // bmpData2.Scan0 = bmpData1.Scan0;

           // bt1.UnlockBits(bmpData2);
           // bt2.UnlockBits(bmpData1);

           // bt1.Save("testBt1.bmp");
           // bt2.Save("testBt2.bmp");

   // // Create a new bitmap.
   // Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");

   // // Lock the bitmap's bits. 
   // Rectangle rect = new 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 bytes  = bmpData.Stride * bmp.Height;
   // byte[] rgbValues = new byte[bytes];

   // // Copy the RGB values into the array.
   // System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

   // // Set every red value to 255. 
   // for (int counter = 0; counter < rgbValues.Length; counter+=3)
   //     rgbValues[counter] = 255;
   // // Copy the RGB values back to the bitmap
   // System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

   // // Unlock the bits.
   // bmp.UnlockBits(bmpData);

   // // Draw the modified image.
   // e.Graphics.DrawImage(bmp, 0, 150);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值