title: Why won't my Gdiplus::Bitmap save a file
body: <p>I am new to using GDI+ and I'm doing a proof of concept for a personal project of mine.</p>
  
  <p>For this code segment, I essentially want to convert a <code>cv::Mat</code> into a bitmap and save the file to an arbitrary path.</p>
  
  <p>My conversion process is adapted from this StackOverflow answer:</p>
  
  <p><a href="https://stackoverflow.com/questions/19568158/">How to convert an opencv mat image to gdi bitmap</a></p>
  
  <p>My problem is that when I call <code>bitmap.Save()</code>, it returns an error code of 2 (<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms534175.aspx" rel="nofollow noreferrer">InvalidParameter</a>).</p>
  
  <p>I don't understand why one or more of my input parameters is wrong. </p>
  
  <p><strong>cv::Mat initialization</strong></p>
  
  <pre><code>cv::Mat mat = cv::Mat::ones(768, 1366, CV_8UC3);
  show_image_from_mat(mat);
  </code></pre>
  
  <p><strong>The show_image_from_mat function</strong></p>
  
  <pre><code>int show_image_from_mat(cv::Mat image) 
  {
      cv::Size size = image.size();
      GdiplusStartupInput gdiplusStartupInput;
      ULONG_PTR           gdiplusToken;
      // Initialize GDI+.
      printf("================================================================");
      printf("\n%i StarupStatus ",GdiplusStartup(&amp;gdiplusToken, &amp;gdiplusStartupInput, NULL));
      // gdiplus
      Gdiplus::Bitmap bitmap(size.width, size.height, image.step1(), PixelFormat24bppRGB, image.data);
      CLSID bmpClsid;
      printf("\n%p result", CLSIDFromString(L"{557cf400-1a04-11d3-9a73-0000f81ef32e}",&amp;bmpClsid));
      printf("\n%p clsid", bmpClsid);
      const WCHAR* filename = L"C:\\output.bmp";
      char shortfilename[15];//You will need to malloc if you need to handle arbitrary filenames.
  
      printf("\n%i ErrorCode",bitmap.Save(filename, &amp;bmpClsid,NULL));
      wcstombs(shortfilename, filename, 15);
      show_image_from_file(shortfilename);
      GdiplusShutdown(gdiplusToken);
      return 9001;
  }
  </code></pre>
  
  <p>I got the CLSID abomination from <a href="https://stackoverflow.com/questions/5345803/">Does GDI+ have standard image encoder CLSIDs?</a>.</p>
  
  <p>And I am writing this in Visual Studio (I'm also new to this).</p>
  
  <p>If the image doesn't load, my output is this:</p>
  
  <p><img src="https://i.stack.imgur.com/5rWKc.png" alt="image"></p>