private static Icon FromDrawImage(DrawingImage source) { System.Windows.Controls.Image image = new System.Windows.Controls.Image { Source = source, Stretch = Stretch.Uniform }; image.Arrange(new Rect(0, 0, 32, 32)); image.UpdateLayout(); RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(32, 32, 96, 96, PixelFormats.Pbgra32); renderTargetBitmap.Render(image); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { encoder.Save(stream); Bitmap bmp = new Bitmap(stream); IntPtr Hicon = bmp.GetHicon(); Icon newIcon = Icon.FromHandle(Hicon); return(newIcon); } }
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); }
/// <summary> /// Loads the BowIcon from resources and colors it according to the current theme /// </summary> /// <returns></returns> public static RenderTargetBitmap GenerateWindowIcon() { var iconImage = new System.Windows.Controls.Image { Source = (DrawingImage)Application.Current.MainWindow.Resources["BowIcon"], Stretch = Stretch.Uniform, Margin = new Thickness(20) }; iconImage.Arrange(new Rect(0, 0, 100, 100)); var bitmap = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32); bitmap.Render(iconImage); return(bitmap); }
public void ExportAs(string path, BitmapEncoder encoder) { if (encoder == null) { return; } int width = GetPixelWidth(); int height = GetPixelHeight(); var bitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32); encoder.Frames.Add(BitmapFrame.Create(bitmap)); DrawingGroup drawingGroup = new DrawingGroup(); DrawingGroup mapBackground = new DrawingGroup(); DrawingGroup layerItems = new DrawingGroup(); drawingGroup.Children.Add(mapBackground); drawingGroup.Children.Add(layerItems); Point backgroundLocation = new Point(0, 0); Size backgroundSize = new Size(width, height); Rect backgroundRect = new Rect(backgroundLocation, backgroundSize); using (DrawingContext context = mapBackground.Open()) { context.DrawRectangle(Brushes.Transparent, new Pen(Brushes.Transparent, 0), backgroundRect); } foreach (ILayer layer in this.Layers) { layerItems.Children.Add(layer.Group); } DrawingImage drawingImage = new DrawingImage(drawingGroup); var image = new System.Windows.Controls.Image { Source = drawingImage }; image.Arrange(new Rect(0, 0, width, height)); bitmap.Render(image); using (var stream = new FileStream(path, FileMode.Create)) { encoder.Save(stream); } }
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)); }
public static Bitmap ToBitmap(this System.Windows.Media.Drawing drawing, double width, double height, int dpi, BitmapEncoder encoder) { System.Windows.Controls.Image drawingImage = new System.Windows.Controls.Image { Source = new DrawingImage(drawing) }; drawingImage.Arrange(new Rect(0, 0, width, height)); var bitmap = new RenderTargetBitmap((int)(width / 96 * dpi), (int)(height / 96 * dpi), dpi, dpi, PixelFormats.Pbgra32); bitmap.Render(drawingImage); MemoryStream stream = new MemoryStream(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); encoder.Save(stream); return(new Bitmap(stream)); }
public static Bitmap ImageToBitmap(System.Windows.Controls.Image image) { if (image.Source == null) { throw new AstridExceptions.OriginalImageDontExistException("добавьте редактируемую фотографию"); } var 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 Rect(new System.Windows.Size((int)image.ActualWidth, (int)image.ActualHeight))); rtBmp.Render(image); var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtBmp)); var stream = new MemoryStream(); encoder.Save(stream); return(new Bitmap(stream)); }
/// <summary> /// Loads the BowIcon from resources and colors it according to the current theme /// </summary> /// <returns></returns> public static RenderTargetBitmap GenerateWindowIcon() { var iconImage = new System.Windows.Controls.Image { Source = (DrawingImage) Application.Current.MainWindow.Resources["BowIcon"], Stretch = Stretch.Uniform, Margin = new Thickness(20) }; iconImage.Arrange(new Rect(0, 0, 100, 100)); var bitmap = new RenderTargetBitmap(100, 100, 96, 96, PixelFormats.Pbgra32); bitmap.Render(iconImage); return bitmap; }
private void MenuItem_Click_1(object sender, RoutedEventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; saveFileDialog1.Title = "Save an Image File"; saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") { FileStream stream = new FileStream(saveFileDialog1.FileName, FileMode.Create); TiffBitmapEncoder encoder = new TiffBitmapEncoder(); // var bitmap = BitmapFrame.Create( ); // TextBlock myTextBlock = new TextBlock(); // myTextBlock.Text = "Codec Author is: " + encoder.CodecInfo.Author.ToString(); // bitmap = image.Source; //var bitmap = BitmapFrame(); //2. Create an Image control which will accept the DrawingImage as its Source System.Windows.Controls.Image myImage = new System.Windows.Controls.Image(); myImage.Source = image.Source; myImage.Arrange(new Rect(0, 0, 500, 500)); //Required //3. Render the Image control's content to a RenderTargetBitmap RenderTargetBitmap rtb = new RenderTargetBitmap(500, 500, 96, 96, System.Windows.Media.PixelFormats.Default); rtb.Render(myImage); //4. Now the RenderTargetBitmap can be used with the JpegBitmapEncoder encoder.Frames.Add(BitmapFrame.Create(rtb)); // MessageBox.Show(myPalette.Colors.Count.ToString()); encoder.Save(stream); stream.Close(); } }