C# 调用C++的dll进行图像加载存储
2014-01-10 09:48
555 查看
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);
}
// 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);
}
相关文章推荐
- 笔记备忘: 在 C# 中进行 win32 dll 的动态加载, 调用, 和释放
- C#调用c++封装的dll文件提示未能加载正确的程序集XXX或其某一依赖项。。。
- C#调用C++DLL出现的堆被损坏,说明加载的任何DLL中有Bug
- C++调用C#接口dll,对dll进行注册
- C#调用C++DLL出现的堆被损坏,说明加载的任何DLL中有Bug
- 在c#中调用c++dll如何进行同步调试dll
- c++ clr编译dll在c#调用时出现“试图加载不正确的格式”“找不到dll”错误的解决
- C# 调用 C++ DLL 中的委托,引发“对XXX::Invoke类型的已垃圾回收委托进行了回调”错误的解决办法
- C#调用C++ Build 写的DLL时,有时会出错,错误为无法加载 DLL "XXX.dll": 找不到指定的模块。(异常来自 HRESULT:0xXXXXXX)。。
- 如何实现C++调用C#的dll方法教程
- C#调用C++的DLL类库
- C# 发中调用一个采用C++封装好的dll, 其结构体中的二维数转换
- C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)
- C#调用C++ DLL的方式
- c#调用c++ dll的几种类型转换
- C/C++/C# 调用C/C++ Dll
- C#调用C++动态链接库之Win32dll(函数)、MFCdll(对话框)
- C#调用c++Dll 结构体数组指针的问题
- 用C#调用C++dll
- 在c#里调用C++的dll时,需要注意的一些问题