Click here to Skip to main content
15,888,121 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Render WPF Control to Image in Console App Pin
Abhinav S24-Jun-12 20:09
Abhinav S24-Jun-12 20:09 
GeneralRe: Render WPF Control to Image in Console App Pin
#realJSOP25-Jun-12 0:30
mve#realJSOP25-Jun-12 0:30 
GeneralRe: Render WPF Control to Image in Console App Pin
#realJSOP25-Jun-12 3:13
mve#realJSOP25-Jun-12 3:13 
GeneralRe: Render WPF Control to Image in Console App Pin
Abhinav S25-Jun-12 3:22
Abhinav S25-Jun-12 3:22 
GeneralRe: Render WPF Control to Image in Console App Pin
#realJSOP25-Jun-12 6:23
mve#realJSOP25-Jun-12 6:23 
GeneralRe: Render WPF Control to Image in Console App Pin
Pete O'Hanlon25-Jun-12 3:28
mvePete O'Hanlon25-Jun-12 3:28 
GeneralRe: Render WPF Control to Image in Console App Pin
#realJSOP25-Jun-12 6:23
mve#realJSOP25-Jun-12 6:23 
GeneralRe: Render WPF Control to Image in Console App Pin
#realJSOP25-Jun-12 3:49
mve#realJSOP25-Jun-12 3:49 
I'm starting with xaml created in a scene editor. This xaml is comprised of just the textblock definition. I thought the problem might be because the textblok isn't in a container of any kind, so I created a canvas, and put the textblock in it before trying to render it.

Here's my code:

cs"
private readonly XNamespace _xamlNS  = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation";
private readonly XNamespace _sysNS   = @"clr-namespace:System;assembly=mscorlib";
private readonly XNamespace _xNS     = @"http://schemas.microsoft.com/winfx/2006/xaml";
private readonly XNamespace _localNS = @"clr-namespace:System;assembly=WpfConsoleApp";

//--------------------------------------------------------------------------------
public void ImagesFromText2(string folder)
{
    foreach (DynamicContentItem item in ContentItems)
    {
        if (item.ContentType == "text")
        {
            XElement container = new XElement("Canvas",
                            new XAttribute("xmlns", _xamlNS),
                            new XAttribute("xmlns:x", _xNS),
                            new XAttribute("xmlns:local", _localNS),
                            new XAttribute("Padding", "0,0,0,0"),
                            new XAttribute("Width", item.CanvasElement.Width.ToString()),
                            new XAttribute("Height", item.CanvasElement.Height.ToString()));

            XElement child = item.CanvasElement.AsXElement;
            string xaml = child.ToString();

            child.Name = @"{http://clearchannel.com/spotchart/WpfConsoleApp}SpotChartTextBlock";
            //xElement.RemoveAttribute("Background");
            //xElement.RemoveAttribute("Panel.ZIndex");
            //xElement.RemoveAttribute("Canvas.Right");
            //xElement.RemoveAttribute("Canvas.Bottom");
            //xElement.RemoveAttribute("Canvas.Left");
            //xElement.RemoveAttribute("Canvas.Top");
            child.SetAttributeValue("Margin", "0,0,0,0");
            child.SetAttributeValue("xmlns", _xamlNS);
            child.SetAttributeValue("xmlns:x", _xNS);
            child.SetAttributeValue("xmlns:local", _localNS);

            string text = item.LocalData;
            if (item.CanvasElement.IsDateTime)
            {
                child.SetAttributeValue("Text", item.LocalDataFormatted);
            }
            child.Name = @"{http://clearchannel.com/spotchart/WpfConsoleApp}SpotChartTextBlock.RenderTransform";
            container.Add(child);

            xaml = container.ToString();
            Canvas canvas = (Canvas)(XamlReader.Parse(xaml));

            SpotChartTextBlock textBlock    = (SpotChartTextBlock)(canvas.Children[0]);
            textBlock.VerticalTextAlignment = item.VerticalAlign;
            textBlock.SizeToFit             = item.SizeToFit;
            textBlock.MinimumFontSize       = item.MinimumFontSize;
            textBlock.Process();
            int width = (int)textBlock.Width;
            int height = (int)textBlock.Height;

            RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(canvas);
                ctx.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height)));
            }
            rtb.Render(dv);
            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(rtb));
            MemoryStream stream = new MemoryStream();
            png.Save(stream);
            System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
            image.Save(System.IO.Path.Combine(folder, string.Format("{0}.png", item.ElementName)));
        }
    }
}

".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997


AnswerRe: Render WPF Control to Image in Console App Pin
JOAT-MON25-Jun-12 9:17
JOAT-MON25-Jun-12 9:17 
GeneralRe: Render WPF Control to Image in Console App Pin
#realJSOP26-Jun-12 4:04
mve#realJSOP26-Jun-12 4:04 
GeneralRe: Render WPF Control to Image in Console App Pin
JOAT-MON26-Jun-12 9:16
JOAT-MON26-Jun-12 9:16 
QuestionVirtualizationMode="Recycling" Issue And Hackish Fix?? Pin
FocusedWolf22-Jun-12 18:24
FocusedWolf22-Jun-12 18:24 
AnswerRe: VirtualizationMode="Recycling" Issue And Hackish Fix?? Pin
FocusedWolf23-Jun-12 8:43
FocusedWolf23-Jun-12 8:43 
Questiondefault content for custom control Pin
Silent Winter20-Jun-12 1:58
Silent Winter20-Jun-12 1:58 
AnswerRe: default content for custom control Pin
Wayne Gaylard20-Jun-12 2:28
professionalWayne Gaylard20-Jun-12 2:28 
GeneralRe: default content for custom control Pin
Silent Winter20-Jun-12 3:40
Silent Winter20-Jun-12 3:40 
GeneralRe: default content for custom control Pin
Wayne Gaylard20-Jun-12 4:14
professionalWayne Gaylard20-Jun-12 4:14 
Questionclient side serial communication Pin
javadadabi16-Jun-12 22:31
javadadabi16-Jun-12 22:31 
AnswerRe: client side serial communication Pin
Abhinav S17-Jun-12 20:21
Abhinav S17-Jun-12 20:21 
GeneralRe: client side serial communication Pin
javadadabi18-Jun-12 0:51
javadadabi18-Jun-12 0:51 
AnswerRe: client side serial communication Pin
Abhinav S18-Jun-12 17:29
Abhinav S18-Jun-12 17:29 
GeneralRe: client side serial communication Pin
javadadabi18-Jun-12 19:35
javadadabi18-Jun-12 19:35 
AnswerRe: client side serial communication Pin
Gerry Schmitz19-Jun-12 0:27
mveGerry Schmitz19-Jun-12 0:27 
GeneralRe: client side serial communication Pin
javadadabi19-Jun-12 1:22
javadadabi19-Jun-12 1:22 
GeneralRe: client side serial communication Pin
Gerry Schmitz19-Jun-12 7:45
mveGerry Schmitz19-Jun-12 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.