-4

I get an error saying the encoder parameter is required on the Save() line at runtime. I have no idea what I should be putting for this parameter though. Any ideas?

using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
{
                using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                {
                    g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                     Screen.PrimaryScreen.Bounds.Y,
                                     0, 0,
                                     bmpScreenCapture.Size,
                                     CopyPixelOperation.SourceCopy);

                    MemoryStream ms = new MemoryStream();
                    bmpScreenCapture.Save(ms, bmpScreenCapture.RawFormat);   // <---- ERROR
                    byte[] bytes = ms.GetBuffer();
                    ms.Close();
                }
}
user441521
  • 6,942
  • 23
  • 88
  • 160
  • 1
    When you say "error," do you mean compiler or runtime? – Matthew Haugen Jul 30 '14 at 17:51
  • Take a look in other posts: http://stackoverflow.com/questions/5336553/c-sharp-print-screen-active-window and http://stackoverflow.com/questions/18046083/save-print-screen-in-c-sharp – briba Jul 30 '14 at 17:53
  • Neither of those are saving to a "MemoryStream" object, so I thought it was handled differently. Not sure why the crazy down votes. Someone teaches someone something and we down vote the question? Those links are not exactly the same question as mine as they are saving them to disk. – user441521 Jul 30 '14 at 21:56

1 Answers1

3

Change that line as

bmpScreenCapture.Save(ms, ImageFormat.Png); 

BTW: You can use any format that ImageFormat supports

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat(v=vs.110).aspx

EZI
  • 15,209
  • 2
  • 27
  • 33