Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a xaml string like

C#
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>"; 

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

{

      //convert code here

}

now i want to convert that xaml into image something like above
Posted

1 solution

 
Share this answer
 
Comments
agha_ali22 27-Jul-12 2:09am    
i have something like it but empty image is created

public System.Drawing.Image ConvertXamltoImage(string XamlString, int Width, int Height)
{
UIElement visual = XamlReader.Load(System.Xml.XmlReader.Create(new StringReader(XamlString))) as UIElement;

Size size = new Size(Width, Height);

RenderTargetBitmap bmpCopied = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(visual);

dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(Width, Height)));
}

bmpCopied.Render(dv);

PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bmpCopied));
MemoryStream stream = new MemoryStream();

png.Save(stream);

System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
stream.Flush();
stream.Close();
return img;
}
Jesse Chisholm 16-Sep-12 21:26pm    
The MSDN says that the stream must remain open for the life of the image.
To get around this there are two solutions.

// force it to make a deep copy, which reads the stream.
1: img = img.Clone();

// force it to make a deep copy in a polite way. ;-D
2: WritableImage wi=new WritableImage(img); wi.Freeze(); img = wi;


-Jesse

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900