title: C# Bitmap BitmapImage bitmapsource
body: <p>I have some code I found somewhere on the Net.</p>
  
  <pre><code>unsafe static Bitmap SaveFrame(IntPtr pFrame, int width, int height)
      {
          try
          {
              int x, y;
              int linesize = width * 3;
              byte* scan0 = (byte*)Marshal.ReadIntPtr(pFrame);
  
              IntPtr bufferPtr = Marshal.AllocHGlobal(linesize * height);
              byte* buffer = (byte*)bufferPtr;
  
              for (y = 0; y &lt; height; y++)
              {
                  for (x = 0; x &lt; linesize; x = x + 3)
                  {
                      *buffer++ = (byte)*(scan0 + y * linesize + x + 2);
                      *buffer++ = (byte)*(scan0 + y * linesize + x + 1);
                      *buffer++ = (byte)*(scan0 + y * linesize + x);
                  }
              }
              Bitmap b = new Bitmap(width, height, linesize, System.Drawing.Imaging.PixelFormat.Format24bppRgb, bufferPtr);
  
  
  
              return b;
          }
          catch (Exception ex) { throw new Exception(ex.Message); }
      }
  </code></pre>
  
  <p>This above code gives me a valid Bitmap, However I'm using WPF and want it to be in a BitmapImage</p>
  
  <p>Without going through this process, Im trying the code</p>
  
  <pre><code>byte[] ptr = ....
  Marshal.Copy(pFrame, ptr , 0, ptr .Length);
  BitmapImage aBitmapImage = new BitmapImage(); 
  aBitmapImage.BeginInit();
  aBitmapImage.StreamSource = new MemoryStream(ptr); //FLastImageMemStream;//
  aBitmapImage.EndInit();
  </code></pre>
  
  <p>which does not work...    </p>
  
  <p>I have also tried</p>
  
  <pre><code>System.Windows.Media.Imaging.BitmapSource.Create(width, height, 96, 96,
      System.Windows.Media.PixelFormats.Rgb24, null, bufferPtr,linesize * height,
      width * 3 ));
  </code></pre>
  
  <p>which also does not give me an image it seems (after assigning it to the Source property of Image)</p>
  
  <p>Can anyone give me any hints?
  Thanks
  Allen</p>