2

I have tried as follows

BitmapSource bitmap;
            bitmap = graphData.CreateScreenshot();
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            BitmapFrame outputFrame = BitmapFrame.Create(bitmap);
            encoder.Frames.Add(outputFrame);
            encoder.QualityLevel = 100;
            FileStream stream = new FileStream("test.jpg", FileMode.Create);
            encoder.Save(stream);

but I am not able to view the image. I can see following message:

Windows photo viewer can't open this picture because the picture is being edited in another program

What went wrong here?

1
  • 3
    tried stream.close() ? Commented Mar 18, 2015 at 10:17

1 Answer 1

2

You should try and Close the stream after you've created it using

stream.Close();

That said, it would be better to create the Stream in a using statement. The Stream would be disposed of when execution leaves the using block and would hence be closed automatically.

using (var stream = new FileStream("test.jpg", FileMode.Create))
{
    encoder.Save(stream);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Looks like people beat me too it in the comments
Have a look at the Using statement msdn.microsoft.com/en-us/library/yh598w02.aspx also if it's working now mark as answered. Glad to help.
its ok I will let you take this one :)
Muds, Sorry I genuinely was writing this as you commented I had to build a project to double check. :<
Small advice: put it in a using pattern to make it more readable and to prevent issues like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.