//[DebuggerStepThrough] void OnSelectPicture() { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = ImageExtensions.GraphicFilter; if (openFileDialog.ShowDialog().Value) using (new WaitWrapper()) { _newImage = true; _sourceName = openFileDialog.FileName; if (ImageExtensions.IsSVGGraphics(_sourceName)) { _drawing = SVGConverters.ReadDrawing(_sourceName); _wmf = null; ImageBrush = new DrawingBrush(_drawing); _imageType = ResourceType.Drawing; _svg = File.ReadAllBytes(_sourceName); } else if (ImageExtensions.IsWMFGraphics(_sourceName)) { _wmf = WMFConverter.ReadWMF(_sourceName); _drawing = _wmf == null ? null : _wmf.ToDrawing(); if (_drawing == null) { ImageBrush = new VisualBrush(_wmf.Canvas); _imageType = ResourceType.Visual; } else { _wmf = null; ImageBrush = new DrawingBrush(_drawing); _imageType = ResourceType.Drawing; } } else { _drawing = null; _wmf = null; if (new FileInfo(_sourceName).Length > 0) { ImageBrush = new ImageBrush(new BitmapImage(new Uri(_sourceName))); _imageType = ResourceType.Image; } else { MessageBoxService.Show("Невозможно загрузить пустое изображение"); return; } } OnPropertyChanged(() => ImageBrush); if (UpdateProperty != null) UpdateProperty(false); } }
public static Guid SaveImage(WMFImage wmf) { if (wmf == null || wmf.Canvas == null) return Guid.Empty; foreach (var glyph in wmf.Canvas.FindVisualChildren<Glyphs>()) { var glyphGuid = new Guid(Path.GetFileNameWithoutExtension(glyph.FontUri.ToString())); var data = wmf.Resources[glyphGuid]; var resourceGuid = ServiceFactoryBase.ContentService.AddContent(data); glyph.FontUri = new Uri(resourceGuid.ToString(), UriKind.Relative); } return ServiceFactoryBase.ContentService.AddContent(wmf.Canvas); }
public ImagePropertiesViewModel(IElementBackground element) { _drawing = null; _wmf = null; _newImage = false; _element = element; _sourceName = _element.BackgroundSourceName; _imageSource = _element.BackgroundImageSource; _svgImageSource = _element.BackgroundSVGImageSource; _imageType = _element.ImageType; SelectPictureCommand = new RelayCommand(OnSelectPicture); RemovePictureCommand = new RelayCommand(OnRemovePicture, CanRemovePicture); UpdateImage(); }
void OnSelectPicture() { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = ImageExtensions.GraphicFilter; if (openFileDialog.ShowDialog().Value) using (new WaitWrapper()) { _sourceName = openFileDialog.FileName; if (ImageExtensions.IsSVGGraphics(_sourceName)) { _drawing = SVGConverters.ReadDrawing(_sourceName); _wmf = null; ImageBrush = new DrawingBrush(_drawing); _svg = File.ReadAllBytes(_sourceName); } else if (ImageExtensions.IsWMFGraphics(_sourceName)) { _wmf = WMFConverter.ReadWMF(_sourceName); _drawing = _wmf == null ? null : _wmf.ToDrawing(); if (_drawing == null) ImageBrush = new VisualBrush(_wmf.Canvas); else { _wmf = null; ImageBrush = new DrawingBrush(_drawing); } } else { _drawing = null; _wmf = null; ImageBrush = new ImageBrush(new BitmapImage(new Uri(_sourceName))); } _imageChanged = true; } }
void OnRemovePicture() { _newImage = false; _drawing = null; _wmf = null; _svg = null; isWasDelete = true; ImageBrush = null; OnPropertyChanged(() => ImageBrush); if (UpdateProperty != null) UpdateProperty(true); }
public static WMFImage ReadWMF(string fileName) { #region createTempFile var temp = Path.Combine(Path.GetTempPath() + Path.GetRandomFileName()); temp = temp.Remove(temp.Length - 3) + "xps"; #endregion using (PrintDocument pdx = new PrintDocument()) { pdx.PrintPage += (object printSender, PrintPageEventArgs printE) => { var img = System.Drawing.Image.FromFile(fileName); printE.Graphics.DrawImageUnscaled(img, printE.PageSettings.Bounds); printE.HasMorePages = false; }; pdx.DefaultPageSettings.PaperSize = new PaperSize("", 3500, 8000); pdx.DefaultPageSettings.Landscape = true; pdx.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"; pdx.PrinterSettings.PrintToFile = true; pdx.PrinterSettings.PrintFileName = temp; pdx.PrintController = new StandardPrintController(); pdx.Print(); } #region WaitForPrinter for (int i = 0; i < 10; i++) { if (File.Exists(temp)) { FileInfo fi = new FileInfo(temp); for (int j = 0; i < 10; i++) { if (fi.Length == 0) { System.Threading.Thread.Sleep(1000); fi = new FileInfo(temp); } else break; if ((fi.Length == 0) && (j == 9)) throw new Exception("Печать в файл занимает слишком много времени. Либо размер файла слишком большой, либо проблема связана с XPS принтером, либо файл повреждён."); break; } break; } else { System.Threading.Thread.Sleep(1000); } } #endregion WMFImage wmf = new WMFImage(); using (XpsDocument xpsDocument = new XpsDocument(temp, FileAccess.Read)) { DocumentReference docReference = xpsDocument.GetFixedDocumentSequence().References.First(); FixedDocument doc = docReference.GetDocument(false); PageContent content = doc.Pages[0]; var fixedPage = content.GetPageRoot(false); wmf.Canvas = new Canvas() { Width = fixedPage.Width, Height = fixedPage.Height, }; for (int i = fixedPage.Children.Count - 1; i >= 0; i--) { var child = fixedPage.Children[i]; fixedPage.Children.Remove(child); if (child is Glyphs) { var glyph = (Glyphs)child; var glyphRun = glyph.ToGlyphRun(); var path = new System.Windows.Shapes.Path() { Fill = glyph.Fill, Data = glyphRun.BuildGeometry(), RenderTransform = glyph.RenderTransform, RenderTransformOrigin = glyph.RenderTransformOrigin, RenderSize = glyph.RenderSize, }; wmf.Canvas.Children.Insert(0, path); } else wmf.Canvas.Children.Insert(0, child); } ReadResources(xpsDocument.FixedDocumentSequenceReader.FixedDocuments[0].FixedPages[0], wmf); xpsDocument.Close(); } GC.Collect(); try { File.Delete(temp); } catch { } return wmf; }
static void ReadResources(IXpsFixedPageReader reader, WMFImage wmf) { if (wmf.Canvas == null) return; foreach (var glyph in wmf.Canvas.FindVisualChildren<Glyphs>()) { var guid = new Guid(Path.GetFileNameWithoutExtension(glyph.FontUri.ToString())); if (!wmf.Resources.ContainsKey(guid)) { var data = DeobfuscateFont(reader.GetFont(glyph.FontUri)); wmf.Resources.Add(guid, data); } } }
public override void CopyProperties() { _sourceName = null; _drawing = null; _wmf = null; var properties = (LayoutPartImageProperties)_layoutPartImageViewModel.Properties; ImageBrush = ImageHelper.GetResourceBrush(properties.ReferenceUID, properties.ImageType); Stretch = properties.Stretch.ToWindowsStretch(); _imageChanged = false; }
void OnRemovePicture() { _drawing = null; _wmf = null; ImageBrush = null; _imageChanged = true; _svg = null; }