C# SampleGrabber参数不正确
我在directshow应用程序中使用samplegrabber保存一个jpg文件,这让我头疼不已。但是,当我启动CaptureImage()函数时,该图运行良好,它到达GetCurrentBuffer并返回“参数不正确”。。hr=-2147024809C# SampleGrabber参数不正确,c#,directshow,C#,Directshow,我在directshow应用程序中使用samplegrabber保存一个jpg文件,这让我头疼不已。但是,当我启动CaptureImage()函数时,该图运行良好,它到达GetCurrentBuffer并返回“参数不正确”。。hr=-2147024809 AMMediaType mediaType = new AMMediaType(); VideoInfoHeader videoInfo = new VideoInfoHeader(); (pS
AMMediaType mediaType = new AMMediaType();
VideoInfoHeader videoInfo = new VideoInfoHeader();
(pSampleGrabber as ISampleGrabber).GetConnectedMediaType(mediaType);
videoInfo = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader));
int width = videoInfo.BmiHeader.Width;
int height = videoInfo.BmiHeader.Height;
int size = videoInfo.BmiHeader.ImageSize;
DsUtils.FreeAMMediaType(mediaType);
int hr = 0;
int bufferSize = 0;
hr = (pSampleGrabber as ISampleGrabber).GetCurrentBuffer(ref bufferSize, IntPtr.Zero);
CheckHR(hr, "Could not get buffer size for image capture.");
IntPtr frameBufferPointer = Marshal.AllocCoTaskMem(bufferSize);
hr = (pSampleGrabber as ISampleGrabber).GetCurrentBuffer(ref bufferSize, frameBufferPointer);
CheckHR(hr, "Could not get buffer size for image capture.");
byte[] frameBuffer = new byte[bufferSize];
Marshal.Copy(frameBufferPointer, frameBuffer, 0, bufferSize);
Marshal.FreeCoTaskMem(frameBufferPointer);
Bitmap frame = new Bitmap(width, height, PixelFormat.Format32bppArgb);
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bmpData = frame.LockBits(rect, ImageLockMode.ReadWrite, frame.PixelFormat);
Marshal.Copy(frameBuffer, 0, bmpData.Scan0, bufferSize);
frame.UnlockBits(bmpData);
frame.RotateFlip(RotateFlipType.RotateNoneFlipY);
if (format == "PAL" || format == "NTSC" || format == "NTSC32")
{
Bitmap sdBitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage((System.Drawing.Image)sdBitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(frame, 0, 0, width, height);
g.Dispose();
frame = sdBitmap;
sdBitmap.Dispose();
}
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (long)100);
ImageCodecInfo jpegInfo = null;
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
{
if (codec.MimeType.ToLower() == "image/jpeg")
{
jpegInfo = codec;
break;
}
}
EncoderParameters encoderParam = new EncoderParameters(1);
encoderParam.Param[0] = qualityParam;
double timeStamp = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
frame.Save(@"C:\Records\" + timeStamp.ToString() + ".jpg", jpegInfo, encoderParam);
frame.Dispose();
文件最终保存了下来,如果我取出支票HR检查,但是图像是黑色的,具有正确的宽度和高度,这就是为什么我去检查HR,看它是否健康。显然不是。我在谷歌上搜索,试图找出错误和任何常见问题,但还是撞到了我的头(
下面是我的图表:
int hr = 0;
// Graph builder.
ICaptureGraphBuilder2 pBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
hr = pBuilder.SetFiltergraph(pGraph);
CheckHR(hr, "Can't SetFiltergraph.");
// Add Decklink Video Capture.
IBaseFilter pDecklinkVideoCapture = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_DecklinkVideoCapture));
hr = pGraph.AddFilter(pDecklinkVideoCapture, "Decklink Video Capture");
CheckHR(hr, "Can't add Decklink Video Capture to graph.");
// Get Decklink display format.
Dictionary<string, AMMediaType> formats = GetDisplayFormat(pDecklinkVideoCapture);
hr = (DsFindPin.ByName(pDecklinkVideoCapture, "Capture") as IAMStreamConfig).SetFormat(formats[format]);
CheckHR(hr, "Can't set video format on Decklink Video Capture.");
// Add Intel® Media SDK H.264 Encoder.
IBaseFilter pIntelMediaSDKH264Encoder = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_IntelMediaSDKH264Encoder));
hr = pGraph.AddFilter(pIntelMediaSDKH264Encoder, "Intel® Media SDK H.264 Encoder");
CheckHR(hr, "Can't add Intel® Media SDK H.264 Encoder to graph.");
// Add Intel® Media SDK MP4 Muxer.
IBaseFilter pIntelMediaSDKMP4Muxer = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_IntelMediaSDKMP4Muxer));
hr = pGraph.AddFilter(pIntelMediaSDKMP4Muxer, "Intel® Media SDK MP4 Muxer");
CheckHR(hr, "Can't add Intel® Media SDK MP4 Muxer to graph.");
// Add File writer.
IBaseFilter pFilewriter = (IBaseFilter)new FileWriter();
hr = pGraph.AddFilter(pFilewriter, "File writer");
CheckHR(hr, "Can't add File writer to graph");
// Set destination filename.
IFileSinkFilter pFilewriter_sink = pFilewriter as IFileSinkFilter;
if (pFilewriter_sink == null)
{
CheckHR(unchecked((int)0x80004002), "Can't get IFileSinkFilter");
}
hr = pFilewriter_sink.SetFileName(destination, null);
CheckHR(hr, "Can't set filename.");
// Add Smart Tee.
IBaseFilter pSmartTee = (IBaseFilter)new SmartTee();
hr = pGraph.AddFilter(pSmartTee, "Smart Tee");
CheckHR(hr, "Can't add Smart Tee to graph.");
// Add AVI Decompressor.
IBaseFilter pAVIDecompressor2 = (IBaseFilter)new AVIDec();
hr = pGraph.AddFilter(pAVIDecompressor2, "AVI Decompressor");
CheckHR(hr, "Can't add AVI Decompressor to graph.");
// Add SampleGrabber.
pSampleGrabber = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_SampleGrabber));
hr = pGraph.AddFilter(pSampleGrabber, "SampleGrabber");
CheckHR(hr, "Can't add SampleGrabber to graph.");
// Set SampleGrabber Media Type.
AMMediaType pSampleGrabber_pmt = new AMMediaType();
pSampleGrabber_pmt.majorType = MediaType.Video;
pSampleGrabber_pmt.subType = new Guid("{43594448-0000-0010-8000-00AA00389B71}");
pSampleGrabber_pmt.formatType = FormatType.VideoInfo;
VideoInfoHeader pSampleGrabber_format = new VideoInfoHeader();
pSampleGrabber_pmt.formatPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(pSampleGrabber_format));
Marshal.StructureToPtr(pSampleGrabber_format, pSampleGrabber_pmt.formatPtr, false);
hr = ((ISampleGrabber)pSampleGrabber).SetMediaType(pSampleGrabber_pmt);
DsUtils.FreeAMMediaType(pSampleGrabber_pmt);
CheckHR(hr, "Can't set media type to sample grabber.");
// Add Null Renderer.
IBaseFilter pNullRenderer = (IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_NullRenderer));
hr = pGraph.AddFilter(pNullRenderer, "Null Renderer");
CheckHR(hr, "Can't add Null Renderer to graph.");
// Connect Decklink Video Capture and Smart Tee.
hr = pGraph.ConnectDirect(GetPin(pDecklinkVideoCapture, "Capture"), GetPin(pSmartTee, "Input"), null);
CheckHR(hr, "Can't connect Decklink Video Capture and Smart Tee.");
// Connect Smart Tee and AVI Decompressor.
hr = pGraph.ConnectDirect(GetPin(pSmartTee, "Capture"), GetPin(pAVIDecompressor2, "XForm In"), null);
CheckHR(hr, "Can't connect Smart Tee and AVI Decompressor.");
// Connect AVI Decompressor and Intel® Media SDK H.264 Encoder.
hr = pGraph.ConnectDirect(GetPin(pAVIDecompressor2, "XForm Out"), GetPin(pIntelMediaSDKH264Encoder, "In"), null);
CheckHR(hr, "Can't connect AVI Decompressor and Intel® Media SDK H.264 Encoder.");
// Connect Intel® Media SDK H.264 Encoder and Intel® Media SDK MP4 Muxer.
hr = pGraph.ConnectDirect(GetPin(pIntelMediaSDKH264Encoder, "Out"), GetPin(pIntelMediaSDKMP4Muxer, "Input 0"), null);
CheckHR(hr, "Can't connect Intel® Media SDK H.264 Encoder and Intel® Media SDK MP4 Muxer.");
// Connect Intel® Media SDK MP4 Muxer and File writer.
hr = pGraph.ConnectDirect(GetPin(pIntelMediaSDKMP4Muxer, "Output"), GetPin(pFilewriter, "in"), null);
CheckHR(hr, "Can't connect Intel® Media SDK MP4 Muxer and File writer.");
// Connect Smart Tee and SampleGrabber.
hr = pGraph.ConnectDirect(GetPin(pSmartTee, "Preview"), GetPin(pSampleGrabber, "Input"), null);
CheckHR(hr, "Can't connect Smart Tee and SampleGrabber.");
// Connect SampleGrabber and Null Renderer.
hr = pGraph.ConnectDirect(GetPin(pSampleGrabber, "Output"), GetPin(pNullRenderer, "In"), null);
CheckHR(hr, "Can't connect SampleGrabber and Null Renderer.");
int hr=0;
//图形生成器。
ICaptureGraphBuilder2 pBuilder=(ICaptureGraphBuilder2)新CaptureGraphBuilder2();
hr=pBuilder.SetFiltergraph(pGraph);
检查hr(hr,“无法设置FilterGraph.”);
//添加Decklink视频捕获。
IBaseFilter pDecklinkVideoCapture=(IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_DecklinkVideoCapture));
hr=pGraph.AddFilter(pDecklinkVideoCapture,“Decklink视频捕获”);
选中hr(hr,“无法将Decklink视频捕获添加到图表中”);
//获取链接显示格式。
字典格式=GetDisplayFormat(PDECKlingVideoCapture);
hr=(DsFindPin.ByName(pDecklinkVideoCapture,“Capture”)作为IAMStreamConfig.SetFormat(formats[format]);
选中hr(hr,“无法在Decklink视频捕获上设置视频格式”);
//添加Intel®Media SDK H.264编码器。
IBaseFilter PintelMediaSDKH264编码器=(IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_IntelMediaSDKH264Encoder));
hr=pGraph.AddFilter(PintelMediaSDKH264编码器,“英特尔®媒体SDK H.264编码器”);
检查hr(hr,“无法将Intel®Media SDK H.264编码器添加到图形中”);
//添加“英特尔®媒体SDK MP4复用器”。
IBaseFilter pIntelMediaSDKMP4Muxer=(IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_IntelMediaSDKMP4Muxer));
hr=pGraph.AddFilter(pIntelMediaSDKMP4Muxer,“英特尔®媒体SDK MP4 Muxer”);
选中hr(hr,“无法将Intel®Media SDK MP4 Muxer添加到图形中”);
//添加文件编写器。
IBaseFilter pFilewriter=(IBaseFilter)new FileWriter();
hr=pGraph.AddFilter(pFilewriter,“文件编写器”);
CheckHR(hr,“无法将文件写入器添加到图形”);
//设置目标文件名。
IFileSinkFilter pFilewriter_sink=pFilewriter作为IFileSinkFilter;
if(pFilewriter\u sink==null)
{
CheckHR(未选中((int)0x80004002),“无法获取IFileSinkFilter”);
}
hr=pFilewriter\u sink.SetFileName(目的地,null);
选中hr(hr,“无法设置文件名”);
//添加智能T恤。
IBaseFilter pSmartTee=(IBaseFilter)新智能TEE();
hr=pGraph.AddFilter(pSmartTee,“智能Tee”);
选中hr(hr,“无法将智能t形三通添加到图形中”);
//添加AVI解压器。
IBaseFilter pAVIDecompressor2=(IBaseFilter)新AVIDec();
hr=pGraph.AddFilter(pAVIDecompressor2,“AVI解压器”);
CheckHR(hr,“无法将AVI解压缩程序添加到图形中”);
//添加采样捕获器。
pSampleGrabber=(IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_SampleGrabber));
hr=pGraph.AddFilter(pSampleGrabber,“SampleGrabber”);
检查hr(hr,“无法将SampleGrabber添加到图形中”);
//设置SampleGrabber介质类型。
AMMediaType pSampleGrabber_pmt=新AMMediaType();
pSampleGrabber_pmt.majorType=MediaType.Video;
pSampleGrabber_pmt.subType=新Guid({4359448-0000-0010-8000-00AA00389B71});
pSampleGrabber\u pmt.formatType=formatType.VideoInfo;
VideoInfoHeader pSampleGrabber_format=新的VideoInfoHeader();
pSampleGrabber_pmt.formatPtr=Marshal.alloctaskmem(Marshal.SizeOf(pSampleGrabber_格式));
Marshal.StructureToPtr(pSampleGrabber\u格式,pSampleGrabber\u pmt.formatPtr,false);
hr=((ISampleGrabber)pSampleGrabber).SetMediaType(pSampleGrabber\u pmt);
DsUtils.FreeAMMediaType(pSampleGrabber\u pmt);
检查hr(hr,“无法将介质类型设置为样本抓取器”);
//添加空渲染器。
IBaseFilter pNullRenderer=(IBaseFilter)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_NullRenderer));
hr=pGraph.AddFilter(pNullRenderer,“空渲染器”);
CheckHR(hr,“无法将空渲染器添加到图形中”);
//连接Decklink视频捕获和智能T恤。
hr=pGraph.ConnectDirect(GetPin(pDecklinkVideoCapture,“Capture”)、GetPin(pSmartTee,“Input”)、null;
检查hr(hr,“无法连接Decklink视频捕获和智能t恤”);
//连接智能三通和AVI减压器。
hr=pGraph.ConnectDirect(GetPin(pSmartTee,“捕获”),GetPin(pAVIDecompressor2,“变换输入”),null;
检查hr(hr,“无法连接智能t形三通和AVI减压器”);
//连接AVI解压器和Intel®Media SDK H.264编码器。
hr=pGraph.ConnectDirect(GetPin(pAVIDecompressor2,“变换输出”)、GetPin(pintelmediasdkh264编码器,“输入”)、null;
检查hr(hr,“无法连接AVI解压缩程序和英特尔®媒体SDK H.264编码器”);
//连接英特尔®媒体SDK H.264编码器和英特尔®媒体SDK MP4复用器。
hr=pGraph.ConnectDirect(GetPin(pintelmediasdkh264编码器,“输出”)、GetPin(pIntelMediaSDKMP4Muxer,“输入0”)、null;
选中hr(hr,“无法连接英特尔®媒体SDK H.264编码器和英特尔®媒体SDK MP4复用器”);
//连接Intel®Media SDK MP4多路复用器和文件写入器。
hr=pGraph.ConnectDirect(GetPin(pIntelMediaSDKMP4Muxer,“输出”)、GetPin(pFilewriter,“输入”)、null;
选中hr(hr,“无法连接英特尔®媒体SDK MP4复用器和文件写入器”);
//连接智能T形三通和取样装置。
hr=pGraph.ConnectDirect(GetPin(pSmartTee,“预览”),GetPin(pSampleGrabber,“输入”),null;