Click here to Skip to main content
14,363,089 members

Silverlight / WPF

 
GeneralRe: WPF empty tabbed dashboard example code Pin
holdorf6-Jul-16 11:11
memberholdorf6-Jul-16 11:11 
QuestionRaisePropertyChanged is not being called Pin
Kevin Marois27-Jun-16 11:41
professionalKevin Marois27-Jun-16 11:41 
AnswerRe: RaisePropertyChanged is not being called Pin
Mycroft Holmes27-Jun-16 15:27
memberMycroft Holmes27-Jun-16 15:27 
AnswerRe: RaisePropertyChanged is not being called Pin
Bernhard Hiller27-Jun-16 22:50
memberBernhard Hiller27-Jun-16 22:50 
AnswerRe: RaisePropertyChanged is not being called Pin
Jacob Himes28-Jun-16 23:28
professionalJacob Himes28-Jun-16 23:28 
QuestionWPF Diagram Of Logical & Visual Trees Pin
Kevin Marois23-Jun-16 14:56
professionalKevin Marois23-Jun-16 14:56 
AnswerRe: WPF Diagram Of Logical & Visual Trees Pin
Gerry Schmitz24-Jun-16 4:53
mveGerry Schmitz24-Jun-16 4:53 
QuestionHow To Render WinForms Control In WPF Pin
Kevin Marois21-Jun-16 9:07
professionalKevin Marois21-Jun-16 9:07 
We have a MindFusion.RealTimeCharting control that has been extensively modified by us, so talking to them probably won't help.

On a tab control I have User Control, and in that is the WinForms chart control. We want to create a report from the UI. A "Report" is an image that's created and opened in Windows Photo Viewer.

For the purposes of reporting I've written a class called PrintLib that takes the element you want to print, creates an image of it, and opens the Windows Photo Viewer with the image displayed.

The problem is that the actual contents of the chart, a Sine Wave, doesn't show up. To correct this we added a function to the chart control called 'GetPrintBitmap' which returns a DrawingVisual. We then have to call this in code to produce an image file which is the used to replace the image of the XAML element at runtime.

What we would like is to have this happen when the element is rendered. Basically, we need to force WPF to draw the Sine Wave from the WinForms control when the XAML element image is created.

What I have now does work, except that I have to call a function in the WinForms control to get the bitmap back. Here's what I do have:

The process starts here. Element is the outer control containing the User Control, which contains the WinForms control:
public void Print(ItemsControl element) 
{
    ChartViewModel vm = ChartCollection[0];
    RealTimeChart chart = vm.GetChart();
    var dv = chart.GetPrintBitmap();

    PrintLib.GenerateReport(element, dv);
}

Here is the significant portion of the GenerateReport method:
public static void GenerateReport(FrameworkElement element, DrawingVisual dv)
{
    App.Current.Dispatcher.Invoke(() =>
    {
        var width = (int)element.ActualWidth;
        var mainImageName = string.Empty;

        // If no WinForms image (the DrawingVisual) was passed in, then use the element 
        // that was tagged in the XML, otherwise use the passed in image as the main control 
        if (dv == null)
        {
            // Create the image of the element
            mainImageName = CreateImageFromView(element, width);
        }
        else
        {
            mainImageName = DrawImage(dv, width, (int)element.ActualHeight);
        }

        .
        .
        .

    });
}
private static string CreateImageFromView(FrameworkElement element, int width)
{
    // Get the size of the Visual and its descendants.
    var rect = VisualTreeHelper.GetDescendantBounds(element);

    // Make a DrawingVisual to make a screen representation of the control.
    var dv = new DrawingVisual();

    // Fill a rectangle the same size as the control with a brush containing images of the control.
    using (var ctx = dv.RenderOpen())
    {
        var brush = new VisualBrush(element);
        ctx.DrawRectangle(brush, null, new Rect(rect.Size));
    }

    var height = (int)element.ActualHeight;
    if (height == 0)
    {
        height = (int)element.Height;
    }
    if (height == 0)
    {
        var message = "UI Element height is 0 and is not valid";
        Debug.Print(message);
        throw new ArgumentException(message);
    }

    return DrawImage(dv, width, height);
}

private static string DrawImage(DrawingVisual dv, int width, int height)
{
    var rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
    rtb.Render(dv);

    // Make a PNG encoder.
    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(rtb));

    // Save the file.
    var fileName = GetUniqueFileName("png");
    using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        encoder.Save(fs);
    }

    return fileName;
}

So essentiially we don't want to have create the DrawingVisual from the WinForms control in code. Is it possible to get WPF to render the WinForms control's contents when it renders the rest of it?

Thanks
If it's not broken, fix it until it is

AnswerRe: How To Render WinForms Control In WPF Pin
Gerry Schmitz23-Jun-16 8:46
mveGerry Schmitz23-Jun-16 8:46 
GeneralRe: How To Render WinForms Control In WPF Pin
Kevin Marois23-Jun-16 9:28
professionalKevin Marois23-Jun-16 9:28 
GeneralRe: How To Render WinForms Control In WPF Pin
Gerry Schmitz23-Jun-16 13:42
mveGerry Schmitz23-Jun-16 13:42 
QuestionHow to Set WPF InkCanvas Drawing area Pin
Om Prakash17-Jun-16 23:24
memberOm Prakash17-Jun-16 23:24 
AnswerRe: How to Set WPF InkCanvas Drawing area Pin
Gerry Schmitz18-Jun-16 7:21
mveGerry Schmitz18-Jun-16 7:21 
QuestionWPF/MVVM Tabbed UI - Handle Menu Commands Pin
Kevin Marois14-Jun-16 12:54
professionalKevin Marois14-Jun-16 12:54 
AnswerRe: WPF/MVVM Tabbed UI - Handle Menu Commands Pin
Mycroft Holmes14-Jun-16 15:44
memberMycroft Holmes14-Jun-16 15:44 
GeneralRe: WPF/MVVM Tabbed UI - Handle Menu Commands Pin
Kevin Marois14-Jun-16 16:15
professionalKevin Marois14-Jun-16 16:15 
GeneralRe: WPF/MVVM Tabbed UI - Handle Menu Commands Pin
Mycroft Holmes14-Jun-16 17:27
memberMycroft Holmes14-Jun-16 17:27 
AnswerRe: WPF/MVVM Tabbed UI - Handle Menu Commands Pin
U. G. Leander16-Jun-16 2:34
professionalU. G. Leander16-Jun-16 2:34 
QuestionHow to Change WPF Textbox SpellCheck Language to UK English Pin
Dave M121:-)4-Jun-16 23:33
memberDave M121:-)4-Jun-16 23:33 
AnswerRe: How to Change WPF Textbox SpellCheck Language to UK English Pin
Gerry Schmitz5-Jun-16 7:56
mveGerry Schmitz5-Jun-16 7:56 
GeneralRe: How to Change WPF Textbox SpellCheck Language to UK English Pin
Dave M121:-)6-Jun-16 1:06
memberDave M121:-)6-Jun-16 1:06 
QuestionResources not showing up in WPF control put in Winform Pin
pgmr_648044-Jun-16 6:36
memberpgmr_648044-Jun-16 6:36 
AnswerRe: Resources not showing up in WPF control put in Winform Pin
Gerry Schmitz5-Jun-16 9:09
mveGerry Schmitz5-Jun-16 9:09 
GeneralRe: Resources not showing up in WPF control put in Winform Pin
pgmr_648045-Jun-16 10:15
memberpgmr_648045-Jun-16 10:15 
QuestionTwo-Dimensional Datagrid Help Pin
madusanka8931-May-16 4:13
membermadusanka8931-May-16 4:13 

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.