从字符串^转换为char * winforms c ++

时间:2013-10-17 03:46:31

标签: winforms c++-cli

我正在从文本框中读取路径,然后尝试打开图像

String^ P = path->Text;

IplImage* img = cvLoadImage(P);

它给我以下错误

Error 1 error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *'

任何人都可以告诉我如何将其转换为char *。

3 个答案:

答案 0 :(得分:0)

System::String ^ str = path->Text ;
char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
printf(str2);

包含以下头文件

#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;

答案 1 :(得分:0)

永远不会太迟。

这是我正在使用的代码

并且不要忘记将头文件包含在基本文件#include“saveImg.h”中 通过

来打电话

saveImg();

saveImg.h

int saveImg (){ // int argc , char* argv[] ) { //  (int argc, char** argv) {
    IplImage* img;
    // if( argc == 2 && (img=cvLoadImage(argv[1], 1))!= 0)     {
    img=cvLoadImage("hello.jpg") ; 
        IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
        IplImage* net1 = cvCreateImage( cvGetSize(img), 8, 3 );
        CvMemStorage* storage = cvCreateMemStorage(0);
        cvCvtColor( img, gray, CV_BGR2GRAY );
        cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
        CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 );
        int i;
        for( i = 0; i < circles->total; i++ )         {
             float* p = (float*)cvGetSeqElem( circles, i );
             cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 );
             cvCircle( img, cvPoint(cvRound(p[1]),cvRound(p[2])), cvRound(p[3]), CV_RGB(50,0,100), 3, 8, 0 );
//                     cvWaitKey(0);
        }
        cvNamedWindow( "Original", 1 );
        cvShowImage( "Original", img );
        cvWaitKey(0);

        cvNamedWindow( "circles", 1 );
        cvShowImage( "circles", gray );
        cvWaitKey(0);
    return 0;
}

答案 2 :(得分:0)

Visual Studio C ++ 10及以上版本中的Char * to String ^

我在制作一个小型计算器时测试了这个并且没问题。

// Variables  : 

char * number1 ; 
double num1 ; 
String ^ no1 ; 


     number1 =    (char*)Marshal::StringToHGlobalAnsi(no1).ToPointer();  num1 = atof (number1)  ;