void CheckImage(BitmapImage img) { if (img.IsDownloading) { Dispatcher.InvokeAsync(() => CheckImage(img), DispatcherPriority.Background); } else { var el = new System.Windows.Controls.Image() { Source = img, SnapsToDevicePixels = true, UseLayoutRounding = true, MinWidth = m_messageQueue.Device.Width, MinHeight = m_messageQueue.Device.Height, Width = img.Width * ((BitmapSource)img).DpiX / 96, Stretch = Stretch.UniformToFill, }; RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor); el.Measure(new Size(m_messageQueue.Device.Width, m_messageQueue.Device.Height)); el.Arrange(new Rect(0, 0, m_messageQueue.Device.Width, m_messageQueue.Device.Height)); m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(m_messageQueue.Device, el, dither: Dither))); }; }
void CheckImage(BitmapImage img) { if(img.IsDownloading) { Dispatcher.InvokeAsync(() => CheckImage(img), DispatcherPriority.Background); } else { var el = new System.Windows.Controls.Image() { Source = img, SnapsToDevicePixels = true, UseLayoutRounding = true, MinWidth = LedBadgeLib.BadgeCaps.Width, MinHeight = LedBadgeLib.BadgeCaps.Height, Width = img.Width * ((BitmapSource)img).DpiX / 96, Stretch = Stretch.UniformToFill, }; RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor); el.Measure(new Size(LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height)); el.Arrange(new Rect(0, 0, LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height)); m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(el, dither : Dither))); }; }
private static UIElement PrintPage(BitmapSource bitmap) { var bitmapSize = new System.Windows.Size(bitmap.PixelWidth, bitmap.PixelHeight); var image = new System.Windows.Controls.Image { Source = bitmap }; image.Measure(bitmapSize); image.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0), bitmapSize)); return image; }
private static UIElement PrintPage(BitmapSource bitmap) { var bitmapSize = new System.Windows.Size(bitmap.PixelWidth, bitmap.PixelHeight); var image = new System.Windows.Controls.Image { Source = bitmap }; image.Measure(bitmapSize); image.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0), bitmapSize)); return(image); }
public void Print(System.Windows.Controls.Image image) { System.Printing.LocalPrintServer localPrintServer = new System.Printing.LocalPrintServer(); PrintDialog printDialog = new PrintDialog(); System.Printing.PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket); double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / image.ActualWidth, capabilities.PageImageableArea.ExtentHeight / image.ActualHeight); image.LayoutTransform = new System.Windows.Media.ScaleTransform(scale, scale); System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); image.Measure(sz); image.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); printDialog.PrintVisual(image, "Print Image"); }
void CheckTweetData(string text, List <BitmapImage> images) { bool ready = true; foreach (var img in images) { if (img.IsDownloading) { ready = false; break; } } if (ready) { Func <ImageSource, Image> makeImg = source => { var el = new System.Windows.Controls.Image() { Source = source, SnapsToDevicePixels = true, UseLayoutRounding = true, MinWidth = m_messageQueue.Device.Width, MinHeight = m_messageQueue.Device.Height, Width = source.Width * ((BitmapSource)source).DpiX / 96, Stretch = Stretch.UniformToFill, }; RenderOptions.SetBitmapScalingMode(source, BitmapScalingMode.NearestNeighbor); el.Measure(new Size(m_messageQueue.Device.Width, m_messageQueue.Device.Height)); el.Arrange(new Rect(0, 0, m_messageQueue.Device.Width, m_messageQueue.Device.Height)); return(el); }; if (images.Count > 0) { m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(m_messageQueue.Device, makeImg(images[0]), dither: Dither))); } m_messageQueue.Enqueue(LedBadgeLib.WPF.MakeQueuedItem(m_messageQueue.Device, LedBadgeLib.WPF.MakeSingleLineItem(m_messageQueue.Device, text))); for (int i = 1; i < images.Count; ++i) { m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(m_messageQueue.Device, makeImg(images[i]), dither: Dither))); } } else { Dispatcher.InvokeAsync(() => CheckTweetData(text, images), DispatcherPriority.Background); } }
public static Bitmap ImageControlToBitmap(System.Windows.Controls.Image image) { RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)image.ActualWidth, (int)image.ActualHeight, 96.0, 96.0, PixelFormats.Pbgra32); image.Measure(new System.Windows.Size((int)image.ActualWidth, (int)image.ActualHeight)); image.Arrange(new System.Windows.Rect(new System.Windows.Size((int)image.ActualWidth, (int)image.ActualHeight))); rtBmp.Render(image); PngBitmapEncoder encoder = new PngBitmapEncoder(); MemoryStream stream = new MemoryStream(); encoder.Frames.Add(BitmapFrame.Create(rtBmp)); encoder.Save(stream); return(new Bitmap(stream)); }
/// <summary> /// Save chart image to Bitmap /// </summary> private System.Drawing.Bitmap ImageToBitmap(System.Windows.Controls.Image img) { RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)img.Width, (int)img.Height, 96.0, 96.0, PixelFormats.Pbgra32); img.Measure(new System.Windows.Size((int)img.Width, (int)img.Height)); img.Arrange(new Rect(new System.Windows.Size((int)img.Width, (int)img.Height))); rtBmp.Render(img); PngBitmapEncoder encoder = new PngBitmapEncoder(); MemoryStream stream = new MemoryStream(); encoder.Frames.Add(BitmapFrame.Create(rtBmp)); // Save to memory stream and create Bitmap from stream encoder.Save(stream); return(new System.Drawing.Bitmap(stream)); }
private void SaveImageToFile(System.Windows.Controls.Image imageIn, string filePath) { if (imageIn.Source == null) { return; } RenderTargetBitmap rtBmp = new RenderTargetBitmap((int)imageIn.Source.Width, (int)imageIn.Source.Height, 96.0, 96.0, PixelFormats.Pbgra32); imageIn.Measure(new System.Windows.Size((int)imageIn.Source.Width, (int)imageIn.Source.Height)); imageIn.Arrange(new Rect(new System.Windows.Size((int)imageIn.Source.Width, (int)imageIn.Source.Height))); rtBmp.Render(imageIn); JpegBitmapEncoder jEncoder = new JpegBitmapEncoder(); MemoryStream stream = new MemoryStream(); jEncoder.Frames.Add(BitmapFrame.Create(rtBmp)); /* * PngBitmapEncoder encoder = new PngBitmapEncoder(); * MemoryStream stream = new MemoryStream(); * encoder.Frames.Add(BitmapFrame.Create(rtBmp)); */ // Save to memory stream and create Bitamp from stream jEncoder.Save(stream); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream); // Demonstrate that we can do something with the Bitmap bitmap.Save(GetSaveToFilePath(filePath), ImageFormat.Jpeg); // Optionally, if we didn't need Bitmap object, but // just wanted to render to file, we could: //encoder.Save(new FileStream(GetSaveToFilePath(filePath), FileMode.Create)); }
protected override void OnAttached() { var control = Control as Panel; if (control != null) { System.Windows.Controls.Image image = null; var effect = new BlurEffect { Radius = 5, KernelType = KernelType.Box, RenderingBias = RenderingBias.Performance }; window = System.Windows.Application.Current.MainWindow; var dpiX = 96; //(int)typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null); var dpiY = 96; //(int)typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null); var renderer = new RenderTargetBitmap((int)window.Width, (int)window.Height, dpiX, dpiY, PixelFormats.Pbgra32); visibleChanged = (s, e) => { if (control.IsVisible) { control.Children.Remove(image); var children = control.Children.Cast <UIElement>().ToArray(); control.Children.Clear(); image = new System.Windows.Controls.Image(); image.Effect = effect; image.Stretch = Stretch.Fill; image.Source = renderer; renderer.Render(window); control.Children.Add(image); foreach (var c in children) { control.Children.Add(c); } image.Width = window.Width; image.Height = window.Height; image.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity)); image.Arrange(new Rect(image.DesiredSize)); } }; control.IsVisibleChanged += visibleChanged; sizeChanged = (s, e) => { if (control.IsVisible && image != null) { image.Width = window.Width; image.Height = window.Height; image.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity)); image.Arrange(new Rect(image.DesiredSize)); } else { renderer = new RenderTargetBitmap((int)window.Width, (int)window.Height, dpiX, dpiY, PixelFormats.Pbgra32); } }; window.SizeChanged += sizeChanged; } }
private void ApplyCinemagraphButton_Click(object sender, RoutedEventArgs e) { if (CinemagraphInkCanvas.Strokes.Count == 0) { ShowWarning(ResMessage("Editor.Cinemagraph.WarningNoDrawing"), MessageIcon.Info); return; } ActionStack.Did(ListFrames); var dpi = ListFrames[0].ImageLocation.DpiOf(); var scaledSize = ListFrames[0].ImageLocation.ScaledSize(); #region Get the Strokes and Clip the Image var image = ListFrames[0].ImageLocation.SourceFrom(); var rectangle = new RectangleGeometry(new Rect(new System.Windows.Point(0, 0), new System.Windows.Size(image.PixelWidth, image.PixelHeight))); Geometry geometry = Geometry.Empty; foreach (Stroke stroke in CinemagraphInkCanvas.Strokes) { geometry = Geometry.Combine(geometry, stroke.GetGeometry(), GeometryCombineMode.Union, null); } geometry = Geometry.Combine(geometry, rectangle, GeometryCombineMode.Xor, null); var clippedImage = new System.Windows.Controls.Image { Height = image.PixelHeight, Width = image.PixelWidth, Source = image, Clip = geometry }; clippedImage.Measure(scaledSize); clippedImage.Arrange(new Rect(scaledSize)); var imageRender = clippedImage.GetRender(dpi, scaledSize); #endregion Cursor = Cursors.AppStarting; _overlayFramesDel = Overlay; _overlayFramesDel.BeginInvoke(imageRender, dpi, true, OverlayCallback, null); ClosePanel(); }
void CheckTweetData(string text, List<BitmapImage> images) { bool ready = true; foreach(var img in images) { if(img.IsDownloading) { ready = false; break; } } if(ready) { Func<ImageSource, Image> makeImg = source => { var el = new System.Windows.Controls.Image() { Source = source, SnapsToDevicePixels = true, UseLayoutRounding = true, MinWidth = LedBadgeLib.BadgeCaps.Width, MinHeight = LedBadgeLib.BadgeCaps.Height, Width = source.Width * ((BitmapSource)source).DpiX / 96, Stretch = Stretch.UniformToFill, }; RenderOptions.SetBitmapScalingMode(source, BitmapScalingMode.NearestNeighbor); el.Measure(new Size(LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height)); el.Arrange(new Rect(0, 0, LedBadgeLib.BadgeCaps.Width, LedBadgeLib.BadgeCaps.Height)); return el; }; if(images.Count > 0) { m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(makeImg(images[0]), dither: Dither))); } m_messageQueue.Enqueue(LedBadgeLib.WPF.MakeQueuedItem(LedBadgeLib.WPF.MakeSingleLineItem(text))); for(int i = 1; i < images.Count; ++i) { m_messageQueue.Enqueue(new LedBadgeLib.MessageQueueItem(new LedBadgeLib.WpfVisual(makeImg(images[i]), dither : Dither))); } } else { Dispatcher.InvokeAsync(() => CheckTweetData(text, images), DispatcherPriority.Background); } }