title: Creating GDI+ bitmaps in memory and then saving as png
body: <p>I am new to C++ and been having trouble with writing a function using the GDI+ library to create a new bitmap in memory ( so not opening/reading an existing bitmap); then drawing on the bitmap; before saving it to png. In particular, I am having problems with the bitmap creation and saving code. I am constrained to using codeblocks and I can't use visual studios, even if I wanted to.  The code is as follows:</p>
  
  <pre><code>#include "drawImage.h"
  #include &lt;windows.h&gt;
  #include &lt;objidl.h&gt;
  #include &lt;gdiplus.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  using namespace Gdiplus;
  
  drawImage::drawImage(){}
  
  void drawImage::DrawBitmap(int width, int height){
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    GdiplusStartup(&amp;gdiplusToken, &amp;gdiplusStartupInput, NULL);
  
    {
      //Create a bitmap
      Bitmap myBitmap(width, height, PixelFormatCanonical);
      Graphics g(&amp;myBitmap);
      Pen blackpen(Color(255,0,0,0), 3);
  
      //draw on bitmap
      int x1 = 1;
      int x2 = 200;
      int y1 = 1;
      int y2 = 200;
      g.DrawLine(&amp;blackpen, x1,y1,x2,y2);
  
      // Save bitmap (as a png)
      CLSID pngClsid;
      GetEncoderClsid(L"image/png", &amp;pngClsid);
      myBitmap.Save(L"C:\\test\\test.png", &amp;pngClsid, NULL);
    }
  
    GdiplusShutdown(gdiplusToken);
  }
  </code></pre>
  
  <p>The issues I am having are as follows:</p>
  
  <ol>
  <li><p>The 'saving' code does not compile and gives the error message "'GetEncoderClsid' was not declared in this scope". However, I got this direct from the Microsoft website <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms533837(v=vs.85).aspx" rel="nofollow noreferrer">here</a>. I don't think this is the proper way of converting to png but I dont know an alternative way?</p></li>
  <li><p>When the code is compiled and run (by commenting out the saving code), it then crashes on the line "Bitmap *myBitmap = new Bitmap(width, height, PixelFormatCanonical);" and gives an error message saying my executable has stopped working.</p></li>
  </ol>
  
  <p>I have added the 'gdi32' linker library and also '-lgdiplus' as a linker option. Also, I have used <a href="http://www.functionx.com/win32/" rel="nofollow noreferrer">this website</a> to help with the gdi stuff although the section on bitmaps only deals with loading existing bitmaps (not creating new ones in memory)</p>
  
  <p>I am totally lost on what to do, so any help or advice on this matter is much appreciated.</p>