locked
Convert xaml to image RRS feed

  • Question

  • i have a xaml string like

    string xaml = @"<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph FontSize="10" FontFamily="Arial" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run FontSize="13" Foreground="#FFFF0000" FontWeight="Bold" Text="Haider is a very good guy" /></Paragraph><Paragraph FontSize="10" FontFamily="Arial" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run FontSize="13" Foreground="#FF008000" FontWeight="Bold" Text="Haider is a very " /><Run FontSize="13" Foreground="#FFFF0000" FontWeight="Bold" Text="good " /><Run FontSize="13" Foreground="#FF008000" FontWeight="Bold" Text="guy" /></Paragraph><Paragraph FontSize="10" FontFamily="Arial" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run FontSize="13" Foreground="#FF0000FF" FontWeight="Bold" Text="Haider is a very " /><Run FontSize="13" Foreground="#FFFF0000" FontWeight="Bold" Text="good " /><Run FontSize="13" Foreground="#FF0000FF" FontWeight="Bold" Text="guy" /></Paragraph><Paragraph FontSize="10" FontFamily="Arial" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run FontSize="13" Foreground="#FFFFFF00" FontWeight="Bold" Text="Haider is a very " /><Run FontSize="13" Foreground="#FFFF0000" FontWeight="Bold" Text="good " /><Run FontSize="13" Foreground="#FFFFFF00" FontWeight="Bold" Text="guy" /></Paragraph></Section>";  now i want to convert that xaml into image

    public Bitmap XamltoImage(string xaml ,int Width, int Height)

    {

          //convert code here

    }

    Thursday, July 26, 2012 8:29 AM

All replies

  • I haven't done anything similar before but pleased to help you with this in case if it helps.

    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nair S

    Thursday, July 26, 2012 8:40 AM
  •  public System.Drawing.Bitmap ConvertXamltoImage(string XamlString, int Width, int Height)
            {

                RichTextBox AdContentRichTextBox = new RichTextBox() { Width = Width, Height = Height };
                AdContentRichTextBox.BorderThickness = new Thickness(0);
                XmlReader _XmlReader = XmlReader.Create(new StringReader(XamlString));

                AdContentRichTextBox.Document = SetRTF(XamlString);          

                var size = new Size(Width, Height);
                AdContentRichTextBox.Measure(size);
                AdContentRichTextBox.Arrange(new Rect(size));
                RenderTargetBitmap bmp = new RenderTargetBitmap(Width, Height, 300, 300, PixelFormats.Pbgra32);

                bmp.Render(AdContentRichTextBox);


                DrawingVisual _drawingVisual = new DrawingVisual();

                using (DrawingContext _drwaingContext = _drawingVisual.RenderOpen())
                {
                    VisualBrush _visualBrush = new VisualBrush(AdContentRichTextBox);
                }

                PngBitmapEncoder _png = new PngBitmapEncoder();

                _png.Frames.Add(BitmapFrame.Create(bmp));
                System.Drawing.Bitmap _tempBitmap = null;
                using (Stream _fileStream = new MemoryStream())
                {
                    _png.Save(_fileStream);
                    _tempBitmap = new System.Drawing.Bitmap(_fileStream);

                    _fileStream.Flush();
                }
                return _tempBitmap;
            }

     private FlowDocument SetRTF(string xamlString)
            {

                return (FlowDocument)XamlReader.Parse("<FlowDocument xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"> " + xamlString + " </FlowDocument>");
    }

    Images is created but blurry any idea how to fix it

                            
    • Edited by Haider.ali Friday, August 3, 2012 11:07 AM
    Thursday, July 26, 2012 9:53 AM