Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1.  
  2. public static void SaveUIElementToJpegImage(UIElement element)
  3. {
  4.     // todo: pass myChart as a command parameter declaratively to be handled in PlotViewModel
  5.     var b = new WriteableBitmap(element, null);
  6.     using (var s = GetLocalFilestreamFromFileDialog(".jpg"))
  7.     {
  8.         if (s != null)
  9.         {
  10.             SaveBitmapToJpeg(b, s);
  11.         }
  12.     }
  13. }
  14.  
  15. // as found here: http://blog.blueboxes.co.uk/2009/07/21/rendering-xaml-to-a-jpeg-using-silverlight-3/
  16. // also see attribution to SO post here: http://stackoverflow.com/questions/1139200/using-fjcore-to-encode-silverlight-writeablebitmap
  17. private static void SaveBitmapToJpeg(WriteableBitmap bitmap, Stream fs)
  18. {
  19.     //Convert the Image to pass into FJCore
  20.     int width = bitmap.PixelWidth;
  21.     int height = bitmap.PixelHeight;
  22.     int bands = 3;
  23.     byte[][,] raster = new byte[bands][,];
  24.     for (int i = 0; i < bands; i++)
  25.     {
  26.         raster[i] = new byte[width, height];
  27.     }
  28.     for (int row = 0; row < height; row++)
  29.     {
  30.         for (int column = 0; column < width; column++)
  31.         {
  32.             int pixel = bitmap.Pixels[width * row + column];
  33.             raster[0][column, row] = (byte)(pixel >> 16);
  34.             raster[1][column, row] = (byte)(pixel >> 8);
  35.             raster[2][column, row] = (byte)pixel;
  36.         }
  37.     }
  38.     ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
  39.     FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
  40.     //Encode the Image as a JPEG
  41.     MemoryStream stream = new MemoryStream();
  42.     FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
  43.     encoder.Encode();
  44.     //Move back to the start of the stream
  45.     stream.Seek(0, SeekOrigin.Begin);
  46.     //Get the Bytes and write them to the stream
  47.     byte[] binaryData = new Byte[stream.Length];
  48.     long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
  49.     fs.Write(binaryData, 0, binaryData.Length);
  50. }
  51.  
  52. private static Stream GetLocalFilestreamFromFileDialog(string defaultExtension)
  53. {
  54.     // save to user-specified location (requires user interaction)
  55.     var dialog = new SaveFileDialog();
  56.     dialog.DefaultExt = defaultExtension;
  57.     dialog.Filter = dialog.DefaultExt + " File|*" + dialog.DefaultExt + "|All Files|*.*";
  58.     if (dialog.ShowDialog() == true)
  59.     {
  60.         return dialog.OpenFile();
  61.     }
  62.     return null;
  63. }
  64. private static Stream GetFileStreamFromIsolatedStorage(string filename, FileMode fileMode)
  65. {
  66.     // save to IsolatedStorage with no user interaction
  67.     var userstore = IsolatedStorageFile.GetUserStoreForApplication();
  68.     var locations = userstore.GetDirectoryNames();
  69.     return new IsolatedStorageFileStream(filename, fileMode,
  70.         IsolatedStorageFile.GetUserStoreForApplication());
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement