当网格大小更改时调整绘图图像的大小

本文关键字:绘图 图像 调整 网格 | 更新日期: 2023-09-27 18:33:35

我有这个定义,这个DrawingImage有这个RectangleGeometry属性:Rect="0,0,108,208"

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" ></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid Grid.Row="0" Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="20"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel x:Name="ImageDetractor" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <Image Stretch="None" HorizontalAlignment="Center" Name="Detractor" Width="Auto" Height="Auto" VerticalAlignment="Top">
                <Image.Source>
                    <DrawingImage PresentationOptions:Freeze="True" x:Name="DrawDetractor">
                        <DrawingImage.Drawing>
                            <DrawingGroup xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
                                <DrawingGroup x:Name="DrawingLayer">
                                    <DrawingGroup.ClipGeometry>
                                        <RectangleGeometry Rect="0,0,108,208" />
                                    </DrawingGroup.ClipGeometry>
                                    <GeometryDrawing Brush="#FFFFFFFE">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry FillRule="Nonzero" Figures="M0,0L108,0 108,208 0,208 0,0z" />
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                    <GeometryDrawing Brush="#FFF29C44">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry FillRule="Nonzero" Figures="M40.72,20.51C42.58,14.38 48.61,9.88 55,9.73 62.36,9.21 69.56,14.68 70.99,21.91 72.26,28.09 70.08,34.48 66.67,39.61 65.47,41.1 65.82,43.06 65.83,44.82 70.87,44.74 75.92,44.62 80.96,44.8 84.91,44.92 88.95,47.28 90.31,51.11 92.71,58.3 94.47,65.7 96.8,72.92 98.19,77.58 99.53,83.12 96.52,87.45 93.1,92.04 86.96,93.03 81.59,92.77 81.55,126.19 81.57,159.61 81.58,193.03 82,198.23 77.16,203.07 71.97,202.76 67.76,202.58 62.84,203.71 59.37,200.63 55.29,197.78 55.91,192.3 55.66,187.95 55.59,168.46 55.85,148.95 55.52,129.46 55.08,148.3 55.5,167.16 55.32,186.01 55.08,189.8 56.44,193.91 54.54,197.43 52.89,201 48.91,203.16 45.02,202.77 41.33,202.54 37.05,203.61 33.92,201.12 31.34,199.29 29.5,196.32 29.67,193.08 29.69,159.65 29.59,126.22 29.72,92.79 25.34,93.78 20.3,94.35 16.42,91.57 12.61,89.04 11.38,83.98 12.69,79.74 15.29,70.59 17.88,61.43 20.55,52.3 21.62,48.07 25.66,45.11 29.94,44.84 35.14,44.53 40.36,44.9 45.56,44.67 45.53,43.7 45.47,41.75 45.44,40.77 41.03,35.11 39.01,27.55 40.72,20.51z" />
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                    <GeometryDrawing Brush="#FFFFFFFE">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry FillRule="Nonzero" Figures="M43.66,21.44C45.29,15.4 51.94,11.56 58.01,12.84 63.43,13.69 67.68,18.56 68.17,23.97 68.63,29.75 66.25,35.41 62.74,39.9 62.72,42.39 62.71,44.89 62.69,47.38 69.11,48.27 75.61,47.32 82.05,47.87 84.83,48.22 87.14,50.44 87.77,53.15 89.9,60.28 91.92,67.45 94.04,74.59 95.09,78.4 96.15,83.03 93.48,86.44 90.57,89.61 85.97,89.94 81.94,89.85 68.16,89.69 54.36,90.08 40.58,89.65 42.57,87.48 45.76,87.37 48.35,86.3 56.84,83.23 65.69,81.03 73.84,77.09 75.95,76.78 78.99,77.4 78.43,74.18 76.96,74.13 75.5,74.09 74.04,74.04 73.36,70.9 71.98,67.51 68.71,66.34 63.23,64.53 57.32,65.85 51.82,66.94 45.66,68.57 39.38,69.77 33.28,71.6 33.59,72.15 34.22,73.23 34.54,73.78 45.06,72.54 55.14,67.43 65.91,68.53 69.05,68.84 70.88,71.91 70.87,74.85 69.97,76.04 68.4,76.39 67.09,76.96 54.08,81.36 41.04,85.68 28.03,90.05 24.34,91.21 19.64,91.19 16.94,88.03 14.69,85.56 15.19,81.93 16.06,79 18.64,70.38 21,61.71 23.54,53.08 24.23,50.14 26.93,47.98 29.92,47.82 36.14,47.4 42.4,48.21 48.6,47.39 48.58,44.91 48.58,42.43 48.57,39.95 44.59,34.78 42,27.98 43.66,21.44z" />
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                    <GeometryDrawing Brush="#FFFFFFFE">
                                        <GeometryDrawing.Geometry>
                                            <PathGeometry FillRule="Nonzero" Figures="M32.71,91.68C33.82,91.22 34.9,90.68 36.07,90.4 38.33,91.24 40.39,92.87 42.93,92.69 54.77,92.73 66.62,92.7 78.47,92.69 78.55,126.12 78.5,159.55 78.5,192.98 78.78,196.6 75.62,199.86 72.06,199.99 68.57,200.17 64.54,200.7 61.52,198.56 57.94,195.84 59.07,190.85 58.99,186.97 59.02,168.3 58.79,149.63 59.03,130.96 59.4,128.69 57.46,126.14 55.05,126.73 52.95,126.88 52.21,129.24 52.39,131.02 52.4,151.7 52.44,172.38 52.38,193.06 52.5,196.23 50.11,199.28 46.98,199.83 43.99,200.32 40.9,200.31 37.92,199.76 34.91,199.13 32.74,196.11 32.85,193.07 32.69,159.28 32.96,125.47 32.71,91.68z" />
                                        </GeometryDrawing.Geometry>
                                    </GeometryDrawing>
                                </DrawingGroup>
                            </DrawingGroup>
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </Image.Source>
            </Image>
        </StackPanel>
    </Grid>
</Grid>

当我最大化/调整窗口大小时,我的图像保持上述大小,我需要调整图像大小我该怎么做?

当网格大小更改时调整绘图图像的大小

我有一个很好的例子。在我的示例中,我使用 ViewModel 绘制到网格,然后在调整窗口大小时更新。

在 xaml 中尝试此操作

    <Window x:Class="GraphApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModel="clr-namespace:GraphApp"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="400*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0">
            <Button Width="100" Height="30" Content="update" Command="{Binding Path=UpdateCommand}"></Button>
        </StackPanel>
        <Grid x:Name="GraphGrid" Grid.Row="1"> 
            <ViewModel:BasicGraph x:Name="BasicGraphE" Width="{Binding ElementName=GraphGrid, Path=ActualWidth}"
                                  Height="{Binding ElementName=GraphGrid, Path=ActualHeight}"
                                  ></ViewModel:BasicGraph>
        </Grid>
    </Grid>
</Window>

现在是视图模型添加此代码

    namespace GraphApp
{    
    public class BasicGraph:FrameworkElement
    {
    Canvas Graph;
    VisualCollection vc;
    DrawingContext dc;
    double x_center;
    double y_center;
    public BasicGraph()
    {
        vc  = new VisualCollection(this);
        this.Loaded += new RoutedEventHandler(Draw_Loaded);
        this.LayoutUpdated += new EventHandler(Draw_Updated);
    }
    public void UpdateLayout()
    {
        this.UpdateLayout();
    }
    void DrawAxes()
    {
        Point leftMid = new Point(0,this.ActualHeight/2);
        Point rightMid = new Point(this.ActualWidth, this.ActualHeight / 2);
        Point topMid = new Point(this.ActualWidth / 2, 0);
        Point bottomMid = new Point(this.ActualWidth / 2, this.ActualHeight); 
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext dc = dv.RenderOpen())
        {
            dc.DrawLine(new Pen(Brushes.Black, 1.0), leftMid, rightMid);
            dc.DrawLine(new Pen(Brushes.Black, 1.0), topMid, bottomMid);
        }
        vc.Add(dv);
    }
    void DrawLinePoint(Point p1, Point p2)
    {
        DrawAxes();
    }
    void Draw_Loaded(object sender, RoutedEventArgs args)
    {
        DrawAxes();
        // DrawLinePoint(new Point(1.5, 1.5), new Point(50.0, 50.0));
    }
    void Draw_Updated(object sender, EventArgs args)
    {
        vc.Clear();
        DrawAxes();
    }

    void GenerateAxes(Canvas GraphWindow)
    {
        double width = GraphWindow.Width;
        double height = GraphWindow.Height;            
    }
    protected override Visual GetVisualChild(int index)
    {
        return vc[index];
    }
    protected override int VisualChildrenCount
    {
        get
        {
            return vc.Count;
        }
    }

}
}

框架元素基本上告诉编译器这是将在屏幕上绘制的东西。

Draw_Updated <-每当调整窗口大小时都会被调用。这必须清除绘图并重绘。

这些是我的使用语句,以防您想知道

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Forms;
using System.Windows;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Linq.Expressions;