欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 directshow代碼,哪位大俠能幫小弟我轉成vb.net的,非常感謝

更新时间: 2014-05-13 14:33:33 责任编辑: Author_N2

 

收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 directshow代碼,哪位大俠能幫我轉成VB.net的,非常感謝
小弟用VB.net開發多個攝像頭抓圖的軟件,在網上找了代碼,但是是C語言,小弟才疏學淺,目前還無法轉化成VB.net的.
哪位大俠能幫幫我,非常感謝!!!

C/C++ code

#include <stdio.h>
#include <dshow.h>

// change here
#define    FILENAME L"C:\\DXSDK\\Samples\\Media\\butterfly.mpg"

// note that this sample fails on some environment
int
main()
{
 IGraphBuilder *pGraphBuilder;
 IMediaControl *pMediaControl;
 IBasicVideo *pBasicVideo;

 CoInitialize(NULL);

 CoCreateInstance(CLSID_FilterGraph,
    NULL,
    CLSCTX_INPROC,
    IID_IGraphBuilder,
    (LPVOID *)&pGraphBuilder);

 pGraphBuilder->QueryInterface(IID_IMediaControl,
    (LPVOID *)&pMediaControl);

 pMediaControl->RenderFile(FILENAME);

 pGraphBuilder->QueryInterface(IID_IBasicVideo,
     (LPVOID *)&pBasicVideo);

 pMediaControl->Run();

 // The image will be saved when OK is clicked
 MessageBox(NULL,
    "Grab Image",
    "Grab",
    MB_OK);

 // Must Pause before using GetCurrentImage
 pMediaControl->Pause();

 // get width and height
 long height, width;

 pBasicVideo->get_VideoHeight(&height);
 pBasicVideo->get_VideoWidth(&width);

 long bufSize;
 long *imgData;
 HRESULT hr;

 /*
 The second value is NULL to resolve required buffer size.
 The required buffer size will be returned in variable "bufSize".
 */
 hr = pBasicVideo->GetCurrentImage(&bufSize, NULL);
 if (FAILED(hr)) {
     printf("GetCurrentImage failed\n");
     return 1;
 }

 if (bufSize < 1) {
     printf("failed to get data size\n");
     return 1;
 }
 imgData = (long *)malloc(bufSize);

 // The data will be in DIB format
 pBasicVideo->GetCurrentImage(&bufSize, imgData);

 // save DIB file as Bitmap.
 // This sample saves image as bitmap to help
 // understanding the sample.
 HANDLE fh;
 BITMAPFILEHEADER bmphdr;
 BITMAPINFOHEADER bmpinfo;
 DWORD nWritten;

 memset(&bmphdr, 0, sizeof(bmphdr));
 memset(&bmpinfo, 0, sizeof(bmpinfo));

 bmphdr.bfType = ('M' << 8) | 'B';
 bmphdr.bfSize = sizeof(bmphdr) + sizeof(bmpinfo) + bufSize;
 bmphdr.bfOffBits = sizeof(bmphdr) + sizeof(bmpinfo);

 bmpinfo.biSize = sizeof(bmpinfo);
 bmpinfo.biWidth = width;
 bmpinfo.biHeight = height;
 bmpinfo.biPlanes = 1;
 bmpinfo.biBitCount = 32;

 fh = CreateFile("result.bmp",
     GENERIC_WRITE, 0, NULL,
     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 WriteFile(fh, &bmphdr, sizeof(bmphdr), &nWritten, NULL);
 WriteFile(fh, &bmpinfo, sizeof(bmpinfo), &nWritten, NULL);
 WriteFile(fh, imgData, bufSize, &nWritten, NULL);
 CloseHandle(fh);

 free(imgData);

 // Release resource
 pBasicVideo->Release();

 pMediaControl->Release();
 pGraphBuilder->Release();

 CoUninitialize();

 return 0;
}





主要是這段代碼轉換,把DIB保存為BMP文件


C/C++ code

// get width and height
 long height, width;

 pBasicVideo->get_VideoHeight(&height);
 pBasicVideo->get_VideoWidth(&width);

 long bufSize;
 long *imgData;
 HRESULT hr;

 /*
 The second value is NULL to resolve required buffer size.
 The required buffer size will be returned in variable "bufSize".
 */
 hr = pBasicVideo->GetCurrentImage(&bufSize, NULL);
 if (FAILED(hr)) {
     printf("GetCurrentImage failed\n");
     return 1;
 }

 if (bufSize < 1) {
     printf("failed to get data size\n");
     return 1;
 }
[color=#FF0000] imgData = (long *)malloc(bufSize); '這裡開始傻眼了,不知道怎麼轉換了[/color]

 // The data will be in DIB format
 pBasicVideo->GetCurrentImage(&bufSize, imgData);

 // save DIB file as Bitmap.
 // This sample saves image as bitmap to help
 // understanding the sample.
 HANDLE fh;
 BITMAPFILEHEADER bmphdr;
 BITMAPINFOHEADER bmpinfo;
 DWORD nWritten;

 memset(&bmphdr, 0, sizeof(bmphdr));
 memset(&bmpinfo, 0, sizeof(bmpinfo));

 bmphdr.bfType = ('M' << 8) | 'B';
 bmphdr.bfSize = sizeof(bmphdr) + sizeof(bmpinfo) + bufSize;
 bmphdr.bfOffBits = sizeof(bmphdr) + sizeof(bmpinfo);

 bmpinfo.biSize = sizeof(bmpinfo);
 bmpinfo.biWidth = width;
 bmpinfo.biHeight = height;
 bmpinfo.biPlanes = 1;
 bmpinfo.biBitCount = 32;

 fh = CreateFile("result.bmp",
     GENERIC_WRITE, 0, NULL,
     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 WriteFile(fh, &bmphdr, sizeof(bmphdr), &nWritten, NULL);
 WriteFile(fh, &bmpinfo, sizeof(bmpinfo), &nWritten, NULL);
 WriteFile(fh, imgData, bufSize, &nWritten, NULL);
 CloseHandle(fh);

 free(imgData);





 		
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-05-13 14:33:33
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/168874/
本文WWW.DOC100.NET DOC100.NET版权所有。