private void PrintGraph() { Encoders.AddEncoder <JpegEncoder>(); Encoders.AddEncoder <PngEncoder>(); GraphViewModel graphVM = ViewModelLocator.GraphDataStatic; ExtendedImage extendedImage = graphVM.GraphToImage(); Grid printGrid = new Grid(); RowDefinition printRowDefinition = new RowDefinition(); printGrid.RowDefinitions.Add(printRowDefinition); Image image = new Image() { Source = extendedImage.ToBitmap() }; printGrid.Children.Add(image); PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += (s, args) => { args.PageVisual = printGrid; args.HasMorePages = false; }; printDocument.Print("SnagL Graph"); }
public static WriteableBitmap GetBitmapSource(ExtendedImage image) { // Waits for the bitmap to be loaded if it needs so. if (!image.IsFilled) { if (!image.IsLoading) { return(null); } ManualResetEvent resetEvent = new ManualResetEvent(false); EventHandler onLoaded = new EventHandler((o, e) => { resetEvent.Set(); }); image.LoadingCompleted += onLoaded; resetEvent.WaitOne(1000); image.LoadingCompleted -= onLoaded; } // Converts the bitmap. return(image.ToBitmap()); }
private void AssignImage(ExtendedImage image) { Contract.Requires(image != null); _isLoadingImage = false; if (image.IsFilled) { _frames.Clear(); WriteableBitmap imageBitmap = image.ToBitmap(); if (image.IsAnimated && AnimationMode != AnimationMode.None) { Stop(); List <ImageBase> frames = new List <ImageBase>(); frames.Add(image); frames.AddRange(image.Frames.OfType <ImageBase>()); foreach (ImageBase frame in frames) { if (frame != null && frame.IsFilled) { _framerefs.Add(frame.ToBitmap()); _frames.Add(new KeyValuePair <ImageBase, WeakReference>(frame, new WeakReference(_framerefs[_framerefs.Count - 1]))); } } AnimateImage(); Start(); } else { if (_image != null) { _image.Source = imageBitmap; } } } }
public void Execute() { try { var m = new ServiceManager <GetImageCompletedEventArgs>(delegate(BodyArchitectAccessServiceClient client1, EventHandler <GetImageCompletedEventArgs> operationCompleted) { using (OperationContextScope ocs = new OperationContextScope(client1.InnerChannel)) { OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("SessionId", "http://MYBASERVICE.TK/", ApplicationState.Current.SessionData.Token.SessionId)); OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("PictureId", "http://MYBASERVICE.TK/", picture.PictureIdk__BackingField)); OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Hash", "http://MYBASERVICE.TK/", picture.Hashk__BackingField)); ApplicationState.AddCustomHeaders(); client1.GetImageCompleted -= operationCompleted; client1.GetImageCompleted += operationCompleted; client1.GetImageAsync(); } }); m.OperationCompleted += (s, a) => { if (a.Error != null) { PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField); PicturesCache.Instance.AddToCache(item); PicturesCache.Instance.Notify(new PictureInfoDTO() { PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField }); } else if (a.Result != null) { MemoryStream stream = new MemoryStream(a.Result.Result); ExtendedImage desert = new ExtendedImage(); desert.SetSource(stream); desert.LoadingFailed += delegate(object sender, UnhandledExceptionEventArgs e) { PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField); PicturesCache.Instance.AddToCache(item); PicturesCache.Instance.Notify(new PictureInfoDTO() { PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField }); }; desert.LoadingCompleted += delegate { ApplicationState.SynchronizationContext.Post(delegate { var image = desert.ToBitmap(); PictureCacheItem item = new PictureCacheItem(image, picture.PictureIdk__BackingField, picture.Hashk__BackingField); PicturesCache.Instance.AddToCache(item); PicturesCache.Instance.Notify(picture); //if (OperationCompleted != null) //{ // OperationCompleted(this, EventArgs.Empty); //} }, null); }; } }; m.Run(); } catch (Exception) { PictureCacheItem item = new PictureCacheItem(null, picture.PictureIdk__BackingField, picture.Hashk__BackingField); PicturesCache.Instance.AddToCache(item); PicturesCache.Instance.Notify(new PictureInfoDTO() { PictureIdk__BackingField = picture.PictureIdk__BackingField, Hashk__BackingField = picture.Hashk__BackingField }); } }
public static ImageSource SaveThumbnail(ThumbnailOptions options) { // Make sure this method runs in the UI thread. if (!Deployment.Current.Dispatcher.CheckAccess()) { return(Deployment.Current.Dispatcher.Invoke <ImageSource>(() => { return SaveThumbnail(options); })); } // Gets the images. WriteableBitmap preferedImage = GetBitmapSource(options.PreferedMedia); WriteableBitmap fallbackImage = GetBitmapSource(options.FallbackMedia); // Determines which image needs to be saved. WriteableBitmap targetImage = preferedImage; if (preferedImage == null) { // Use the fallback image if the prefered image does not exist. targetImage = fallbackImage; } else if (fallbackImage != null && options.MinWidth > -1 && preferedImage.PixelWidth < options.MinWidth && preferedImage.PixelWidth < fallbackImage.PixelWidth) { // Use the fallback image if its width is bigger than the prefered image's width, the latter being // smaller than the min width. targetImage = fallbackImage; } // No image? Return. if (targetImage == null) { return(null); } // Gets the dimensions of the target image. int targetWidth = (int)Math.Max(options.MinWidth, targetImage.PixelWidth); double sourcePixelRatio = targetImage.PixelWidth / (double)targetImage.PixelHeight; int targetHeight = (int)Math.Floor(targetWidth / sourcePixelRatio); // Blurs the image if needed. ExtendedImage targetExtendedImage = null; if (options.Blur) { // Converts the image to an ImageTools image. targetExtendedImage = targetImage.ToImage(); // Resizes the image if it can help decreasing the time needed to blur it. // The image is only resized if the target size is less than the original size. // Otherwise it means that the original image is smaller than the target size, in which case we blur it // as it is and let WP scale up the blurred image later. if (targetExtendedImage.PixelHeight * targetExtendedImage.PixelWidth > targetWidth * targetHeight) { targetExtendedImage = ExtendedImage.Resize(targetExtendedImage, targetWidth, targetHeight, new ImageTools.Filtering.NearestNeighborResizer()); } // Inits the blur filter if needed and runs it. if (_gaussianBlurFilter == null) { _gaussianBlurFilter = new ImageTools.Filtering.GaussianBlur() { Variance = 2d }; } targetExtendedImage = ExtendedImage.ApplyFilters(targetExtendedImage, _gaussianBlurFilter); } // Crops the image if needed. if (options.CropRectangle.HasValue) { if (targetExtendedImage == null) { // Converts the image to an ImageTools image. targetExtendedImage = targetImage.ToImage(); } // Computes the downscaled crop rectangle. // We're downscaling the crop rectangle instead of upscaling the image and then cropping it, // in order to save some time. // WP will upscale the cropped image later on. int conformedCropWidth = Math.Min(options.CropRectangle.Value.Width, targetWidth); int conformedCropHeight = Math.Min(options.CropRectangle.Value.Height, targetHeight); double scaleFactor = (double)targetExtendedImage.PixelWidth / (double)targetWidth; Rectangle crop = options.CropRectangle.Value; crop.Width = (int)(conformedCropWidth * scaleFactor); crop.Height = (int)(conformedCropHeight * scaleFactor); crop.X = (int)((double)crop.X * scaleFactor); crop.Y = (int)((double)crop.Y * scaleFactor); // Crops the image. if (crop.Width > 0 && crop.Height > 0) { targetExtendedImage = ExtendedImage.Crop(targetExtendedImage, crop); } // Stores the final dimensions of the image for later scaling. targetWidth = conformedCropWidth; targetHeight = conformedCropHeight; } if (targetExtendedImage != null) { targetImage = targetExtendedImage.ToBitmap(); } // Saves the image. try { if (targetWidth > 0 && targetHeight > 0) { using (IsolatedStorageFileStream stream = options.IsoStoreFile.OpenFile(options.Filename, FileMode.Create, FileAccess.ReadWrite)) { targetImage.SaveJpeg(stream, targetWidth, targetHeight, 0, 100); } } } catch (IsolatedStorageException ex) { // Nothing to do, let's just dump this for further analysis. DebugUtils.DumpException(ex, string.Format("OpenFile(f:{0})", options.Filename), true); } catch (ArgumentException ex) { // Nothing to do, let's just dump this for further analysis. DebugUtils.DumpException(ex, string.Format("SaveJpeg(w:{0},h:{1})", targetWidth, targetHeight), true); } // Returns the image. return(targetImage); }