3

So I've been trying to create image files from the current game window (a screenshot essentially) but what I've tried so far just isn't working. The methods RenderTarget2D.SaveAsPng() and RenderTarget2D.SaveAsJpeg() aren't implemented yet by the MonoGame developers to use for Windows...

So I'm wondering, is there an alternative?

Here's a sample code that, when run, throws the NotImplemented error, for anyone curious:

Stream stream = new FileStream("screenshot.jpg", FileMode.Create, FileAccess.Write, FileShare.None);
screenshot.SaveAsJpeg(stream, 320, 180);
stream.Close();

The variable screenshot is a RenderTarget2D object where the game screen was drawn in.

Thanks for reading, I hope to get some help.

Lance K.
  • 53
  • 7
  • Can you share how you drew the game screen into a `RenderTarget2D` object? I'd like to use this method to save my game screen, but I can't seem to figure out how to do that step. Thanks! :) – Captain Delano Oct 11 '22 at 03:08

1 Answers1

1

SaveAsJpeg is defined as:

public void SaveAsJpeg(Stream stream, int width, int height)
{
#if WINDOWS_STOREAPP
    SaveAsImage(BitmapEncoder.JpegEncoderId, stream, width, height);
#elif WINDOWS_PHONE

    var pixelData = new byte[Width * Height * GraphicsExtensions.Size(Format)];
    GetData(pixelData);

    var waitEvent = new ManualResetEventSlim(false);
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
        var bitmap = new WriteableBitmap(width, height);
        System.Buffer.BlockCopy(pixelData, 0, bitmap.Pixels, 0, pixelData.Length);
        bitmap.SaveJpeg(stream, width, height, 0, 100);
        waitEvent.Set();
    });

    waitEvent.Wait();
#elif MONOMAC
    SaveAsImage(stream, width, height, ImageFormat.Jpeg);
#else
    throw new NotImplementedException();
#endif
}

Can you add

#define WINDOWS_STOREAPP

in your main class.

Abhinav
  • 2,085
  • 1
  • 18
  • 31
  • I tried this, it does recognize the constant being set but it still throws an NotImplemented error. I tried with all the possible constants, but none seem to work. – Lance K. Jun 16 '13 at 01:01