/// <summary> /// Standard method to convert command line parameter string into an enaum <seealso cref="ScaleTO"/> datatype. /// </summary> /// <param name="args"></param> /// <param name="CurrIndex"></param> /// <param name="ParamName"></param> /// <param name="scaleValue"></param> /// <param name="strError"></param> /// <returns></returns> public static int GetScaleValue(string[] args, int CurrIndex, string ParamName, out ScaleTO scaleValue, out string strError) { strError = string.Empty; scaleValue = ScaleTO.OriginalSize; if (args.Length <= (CurrIndex + 1)) { strError = string.Format(ParamValueMissingMsg, ParamName); return(-1); } string stringValue = args[CurrIndex + 1]; if (stringValue.Trim().Length == 0) { strError = string.Format(ParamValueInvalidStringMsg, ParamName); return(-1); } if (stringValue.ToUpper() == OptScaleModeOriginal.ToUpper()) { scaleValue = ScaleTO.OriginalSize; return(0); } if (stringValue.ToUpper() == OptScaleModeTarget.ToUpper()) { scaleValue = ScaleTO.TargetSize; return(0); } strError = string.Format(ParamValueInvalidSizeMsg, ParamName, stringValue, OptScaleModeOriginal, OptScaleModeTarget); return(-1); }
private BitmapSource RenderToBitmap(FrameworkElement target, double width, double height, double dpiX, double dpiY, ScaleTO thisScale) { Rect bounds; switch (thisScale) { case ScaleTO.OriginalSize: bounds = VisualTreeHelper.GetDescendantBounds(target); break; case ScaleTO.TargetSize: bounds = new Rect(0, 0, width, height); break; default: throw new ArgumentException(string.Format("The scaling mode: {0} is not supported.", thisScale.ToString())); } RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)target.ActualWidth, (int)target.ActualHeight, dpiX, dpiY, PixelFormats.Pbgra32); DrawingVisual visual = new DrawingVisual(); using (DrawingContext context = visual.RenderOpen()) { VisualBrush brush = new VisualBrush(target); context.DrawRectangle(brush, null, new Rect(new Point(), bounds.Size)); } renderBitmap.Render(visual); return(renderBitmap); }
public void Convert(Stream xamlInput, double width, double height, double dpiX, double dpiY, ScaleTO thisScale, Stream pngOutput) { try { // Round width and height to simplify width = Math.Round(width); height = Math.Round(height); Thread pngCreationThread = new Thread((ThreadStart) delegate() { FrameworkElement element = null; try { element = XamlReader.Load(xamlInput) as FrameworkElement; } catch (XamlParseException) { } if (element != null) { Size renderingSize = new Size(width, height); element.Measure(renderingSize); Rect renderingRectangle = new Rect(renderingSize); element.Arrange(renderingRectangle); BitmapSource xamlBitmap = RenderToBitmap(element, width, height, dpiX, dpiY, thisScale); try { PngBitmapEncoder enc = new PngBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(xamlBitmap)); enc.Save(pngOutput); } catch (ObjectDisposedException) { // IF the operation lasted too long, the object might be disposed already } } }); pngCreationThread.IsBackground = true; pngCreationThread.SetApartmentState(ApartmentState.STA); pngCreationThread.Start(); pngCreationThread.Join(); } catch (Exception Exp) { throw Exp; } try { if (pngOutput.Length == 0) { throw new TimeoutException(); } } catch (ObjectDisposedException) { // If the object was disposed, it means that the Stream is ready. } }