Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:52:46+00:00 2026-05-26T06:52:46+00:00

I’m banging my head over trying to save a jpg file using the samplegrabber

  • 0

I’m banging my head over trying to save a jpg file using the samplegrabber in my directshow application. The graph runs fine however when I launch the CaptureImage() function it gets as far as GetCurrentBuffer and returns “parameter is incorrect”.. hr = -2147024809.

        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();

The file does save in the end, if I take out the CheckHR checks however the image is black with the right width and height which is why I went and checked the HR to see if it was healthy. Apparently not. I’ve searched google trying to figure out the error and any common issues in the matter but again, banging my head. šŸ™

Heres a quick look at my graph:

        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.");

Any suggestions or ideas are most welcomed. Let me know if more info is required.

Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:52:46+00:00Added an answer on May 26, 2026 at 6:52 am

    -2147024809 converted to hex is 0x80070057 which is E_INVALIDARG.

    E_INVALIDARG for GetCurrentBuffer means: Samples are not being buffered. Call ISampleGrabber::SetBufferSamples.

    To activate buffering, call ISampleGrabber::SetBufferSamples with a value of TRUE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.