2 Replies - 10332 Views - Last Post: 19 April 2012 - 05:24 AM

#1 amit2011  Icon User is offline

  • D.I.C Head

Reputation: -13
  • View blog
  • Posts: 64
  • Joined: 17-March 11

Silverlight capturing screenshot and saving to disk

Posted 18 April 2012 - 09:57 PM


private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
             //WriteableBitmap bitmap = new WriteableBitmap(target , null );
               WriteableBitmap bitmap = new WriteableBitmap(500, 500);
            //     
            if (bitmap != null)
            {
                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.Filter = "JPEG Files (*.jpeg)|*.jpeg";
                saveDlg.DefaultExt = ".jpeg";
                
                if ((bool)saveDlg.ShowDialog())
                {
                    using (Stream fs = saveDlg.OpenFile())
                    {

                       SaveToFile(bitmap, fs);  
                       // SaveToFile(bitmap);
                       MessageBox.Show("Screenshot Saved");
                   
                    }
                }
            }
        }
        
        private static void SaveToFile(WriteableBitmap bitmap,Stream fs)

       // private static String SaveToFile(WriteableBitmap bitmap)
        {
            int width = bitmap.PixelWidth;
            int height = bitmap.PixelHeight;
            int bands = 3;
            byte[][,] raster = new byte[bands][,];

            //Convert the Image to pass into FJCore
     
            for (int i = 0; i < bands; i++)
            {
                raster[i] = new byte[width, height];
            }

            for (int row = 0; row < height; row++)
            {
                for (int column = 0; column < width; column++)
                {
                    int pixel = bitmap.Pixels[width * row + column];
                    raster[0][column, row] = (byte)(pixel >> 16);
                    raster[1][column, row] = (byte)(pixel >> 8);
                    raster[2][column, row] = (byte)pixel;
                }
            }

            ColorModel model = new ColorModel {colorspace = ColorSpace.RGB };
            FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);

            //Encode the Image as a JPEG
            MemoryStream stream = new MemoryStream();
            FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
            encoder.Encode();

            //Back to the start
            stream.Seek(0, SeekOrigin.Begin);
           
            //Get the Bytes and write them to the stream
            byte[] binaryData = new Byte[stream.Length];
            long bytesRead;
             bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
            fs.Write(binaryData, 0, binaryData.Length );  
            // string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
            // return base64String; 
        }





// took this code from a blog(as i was having no proper idea for getting screenshot)
after writing WriteableBitmap bitmap = new WriteableBitmap(target , null); it is showing an error i.e

System.ArgumentNullException was unhandled by user code Message=Value cannot be null. Parameter name: element StackTrace: at System.Windows.Media.Imaging.WriteableBitmap..ctor(UIElement element, Transform transform) at TutorialViewChangeEvents.MainPage.btnCapture_Click(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.onclick() at System.Windows.Controls.Button.onclick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags) InnerException:

But after commenting that and using WriteableBitmap bitmap = new WriteableBitmap(500, 500); it is not showing an error and saves the image, but pixel width and height is accoding to what i enter(i.e 500,500 here).
But it is showing black image i.e not the actual.

What should i do now?

Is This A Good Question/Topic? 0
  • +

Replies To: Silverlight capturing screenshot and saving to disk

#2 pryogene  Icon User is offline

  • The Leafiest of the Leif's
  • member icon

Reputation: 42
  • View blog
  • Posts: 670
  • Joined: 30-June 09

Re: Silverlight capturing screenshot and saving to disk

Posted 19 April 2012 - 05:01 AM

What you haven't told us is what exactly it is you're trying to Screenshot. If it's the entire desktop, you're going to want to find the resolution of the desktop and use those as your parameters. If it's a particular window, then you're going to want to do some Windows Interop to get a pointer to the window. If it's a particular area, you need to take input of the points and do some math to get the sizes.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Silverlight capturing screenshot and saving to disk

Posted 19 April 2012 - 05:24 AM

Well, for starters, the reason you are getting the error is because you are passing null into the constructor(just as the error message told you).

If you are trying simply take a screenshot of the Silverlight application, try this link...

http://www.silverlig...ght-Screen.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1