2011-10-10 26 views
6

我渲染一個WPF視覺(用戶控件)的位圖,但問題是,所呈現的圖像之前縮放/轉換的用戶控件的大小縮放大小。所以我們假設UserControl設計爲200x200像素。當我渲染到BMP時,我使用的是分別報告200和200的UserControl的ActualWidth和ActualHeightt。問題是UserControl是在畫布中並且自動調整大小(設置爲縮放/填充窗口大小)到更接近1200 x 1200(更改)如何獲取一個WPF視覺元素

我已經完成了一些閱讀和搜索,不知道如何確定有效尺寸,即控制器在屏幕上的尺寸。

我遇到了這個問題聽起來充滿希望,但變換返回不包含縮放數據。好吧,但它們都是1. Get element position after transform

任何關於在哪裏尋找渲染縮放的建議都會很棒!

[更新]作爲建議,我包括相關代碼:

public static Bitmap PngBitmap(this Visual visual) 
{ 
    // Get height and width 
    int width = (int)(double)visual.GetValue(
     FrameworkElement.ActualWidthProperty); 
    int height = (int)(double)visual.GetValue(
     FrameworkElement.ActualHeightProperty); 

    // Render 
    RenderTargetBitmap rtb = 
     new RenderTargetBitmap(
      width, 
      height, 
      96, 
      96, 
      PixelFormats.Default); 
    rtb.Render(visual); 

    // Encode 
    PngBitmapEncoder encoder = new PngBitmapEncoder(); 
    encoder.Frames.Add(BitmapFrame.Create(rtb)); 
    System.IO.MemoryStream stream = new System.IO.MemoryStream(); 
    encoder.Save(stream); 

    // Create Bitmap 
    Bitmap bmp = new Bitmap(stream); 
    stream.Close(); 

    return bmp; 
} 

public static BitmapSource BitmapSource(this Visual visual) 
{ 
    Bitmap bmp = visual.PngBitmap(); 
    IntPtr hBitmap = bmp.GetHbitmap(); 
    BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions(); 
    return Imaging.CreateBitmapSourceFromHBitmap(
         hBitmap, 
         IntPtr.Zero, 
         Int32Rect.Empty, 
         sizeOptions); 
} 

[更新#2]添加了XAML - Grid元素被刪除,因爲它是巨大的,從我的讀取包含鍵盤UserControl的Canvas並不是Grid元素的一部分。

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:PMD.HECAT.DashboardModule" 
    xmlns:PMD_HECAT_DashboardModule_VirtualKeyboard="clr-namespace:PMD.HECAT.DashboardModule.VirtualKeyboard" 
    xmlns:System_Windows_Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" 
    xmlns:PMD_HECAT_DashboardModule_Input="clr-namespace:PMD.HECAT.DashboardModule.Input" 
    xmlns:control="clr-namespace:PMD.HECAT.DashboardModule.Controls"  
    x:Class="PMD.HECAT.DashboardModule.CandidateElectrodeView"    
    x:Name="UserControl" 
    mc:Ignorable="d" 
    d:DesignWidth="1024" d:DesignHeight="768" Width="640" Height="360"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="../Themes/DashboardStyles.xaml" /> 
       <ResourceDictionary Source="../Themes/ImageButtons.xaml" /> 
       <ResourceDictionary Source="CandidateViewResources.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary>   
    </UserControl.Resources> 
    <UserControl.Triggers> 
     <EventTrigger RoutedEvent="PMD_HECAT_DashboardModule_VirtualKeyboard:VirtualKeyboardView.KeyboardClose" SourceName="virtualKeyboardView"> 
      <BeginStoryboard Storyboard="{StaticResource OnKeyboardClose1}"/> 
     </EventTrigger> 
    </UserControl.Triggers> 
    <Canvas Width="100" HorizontalAlignment="Left"> 

     <PMD_HECAT_DashboardModule_VirtualKeyboard:VirtualKeyboardView x:Name="virtualKeyboardView" Height="222" Width="550" RenderTransformOrigin="0.5,0.5" Opacity="0" Active="False"> 
      <PMD_HECAT_DashboardModule_VirtualKeyboard:VirtualKeyboardView.RenderTransform> 
       <TransformGroup> 
        <ScaleTransform/> 
        <SkewTransform/> 
        <RotateTransform/> 
        <TranslateTransform X="40" Y="400"/> 
       </TransformGroup> 
      </PMD_HECAT_DashboardModule_VirtualKeyboard:VirtualKeyboardView.RenderTransform> 
     </PMD_HECAT_DashboardModule_VirtualKeyboard:VirtualKeyboardView> 
     <Rectangle Stroke="White" Opacity="0.7" Fill="White" Height="370" Width="654.851" Canvas.Left="687" Canvas.Top="0" /> 
    </Canvas> 
</UserControl> 
+0

什麼樣的轉換應用於控制? LayoutTransform或/和RenderTransform? 「汽車大小」是什麼意思? –

+0

@Erno - 老實說我不完全確定。我是新來的,我比WPF新,而且還沒有掌握佈局工具。看起來UserControl是在一個Canvas裏面的,它被配置爲在寬度和高度上伸展 –

+0

然後向我們顯示代碼,這樣我們可以提供幫助。 –

回答

5

我知道很多的時間過去了,因爲有人問,但它不會傷害張貼一些更多的信息:)

我使用此代碼找到應用的渲染變換(S視覺效果的大小(規模) )。

GeneralTransform t = transformedVisual.TransformToVisual(parentVisual); 

Vector topLeft = (Vector) t.Transform(new Point(0,0)); 
Vector topRight = (Vector) t.Transform(new Point(originalWidthOfTransformedVisual, 0)); 

double renderedWidth = (topRight-topLeft).Length; 
double widthScale = renderedWidth/originalWidthOfTransformedVisual; 
+3

請注意,更一般地說,可以簡單地計算視覺相對於父代的界限:Rect bounds = t .TransformBounds(new Rect(new Size(transformedVisual.ActualWidth,transformedVisual.ActualHeight));'。當然,爲了這個工作,'transformedVisual'需要是一個'FrameworkElement',而不是一個簡單的'Visual'(後者本身沒有任何尺寸信息)。 –

0

你說你使用Visual的寬度和高度。 首先,有兩個問題。 視覺,http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx不具備的高度和寬度的任何屬性。

現在,假設你有一個FrameworkElement的,它確實有高度和寬度屬性,您使用的是高度和寬度屬性,或者的ActualHeight和ActualWidth屬性? 如果你不是,那麼這些是你正在尋找的屬性。

如果您使用的是他們,或者你使用的不是FrameworkElement的,那麼你應該張貼一些代碼。

+0

對不起,我感到困惑。它實際上是一個UserControl,我使用ActualWidth和ActualHeight(我已經更新了我的原始信息來澄清這一點) –

0

如果你可以用動畫來執行Transform然後動畫提供Completed事件。這應該是提取新的轉換大小的好地方。

0
public static Rect GetRelativePlacement(this UIElement element, UIElement relativeTo) 
    { 
     var absolutePos = element.PointToScreen(new Point(0, 0)); 
     var posRelativeTo = relativeTo.PointToScreen(new Point(0, 0)); 

     var topLeft = new Point(absolutePos.X - posRelativeTo.X, absolutePos.Y - posRelativeTo.Y); 
     var bottomRight = element.PointToScreen(new Point(element.RenderSize.Width, element.RenderSize.Height)); 

     var bounds = Rect.Empty; 
     bounds.Union(topLeft); 
     bounds.Union(bottomRight); 

     return bounds; 
    } 
1

嗨,我有一個類似的問題,如果之前強制此渲染

 uiElement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 
     uiElement.Arrange(new Rect(new Point(0, 0), uiElement.DesiredSize)); 
     Size _size = uiElement.DesiredSize; 

這個工程至少在我的情況下脫穎而出UIElement以使其尺寸視覺不顯示

相關問題