2

我想在WriteableBitmap. 似乎不可能,但我发现在 C# 中将 WriteableBitmap 转换为 Bitmap和this

这是调用(也包括转换方法):

     // Draw dino names...
            Graphics g = Graphics.FromImage(BitmapFromWriteableBitmap(writeableBitmap));
            g.DrawString(Dinosaurs[i].PersonalName, new Font("Tahoma", 40), System.Drawing.Brushes.White, new PointF((float)Dinosaurs[i].Head.X, (float)Dinosaurs[i].Head.Y));

        } // for i

    }// DrawDinos2d

    private System.Drawing.Bitmap BitmapFromWriteableBitmap(WriteableBitmap writeBmp)
    {
        System.Drawing.Bitmap bmp;
        using (MemoryStream outStream = new MemoryStream())
        {
            BitmapEncoder enc = new BmpBitmapEncoder();
            enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
            enc.Save(outStream);
            bmp = new System.Drawing.Bitmap(outStream);
        }
        return bmp;
    }

我已经检查了调试器并且g有一个很好的值(不是空的),但是屏幕上没有任何东西。我还应该提到,传递给的字符串DrawString很好,点的 X、Y 也很好(大约 1000,800 的正整数)。我认为它要么在屏幕上绘制,要么 X,Y 坐标可能没有正确转换。

有任何想法吗?谢谢。

这是 XAML(如果有帮助的话):

<Window x:Class="DinosaurIsland.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Dinosaur Island" Height="600" Width="600" WindowState="Normal"  Icon="/DinosaurIsland;component/Icon1.ico" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" WindowStartupLocation="CenterOwner">
<Window.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type BitmapImage}">
            <Image Source="{Binding}" />
        </DataTemplate>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<DockPanel>
    <Menu x:Name="MainMenu" DockPanel.Dock="Top">
        <MenuItem Header="_File">
            <MenuItem Header="_Open Dinosaur Island 'snapshot' file..." x:Name="OpenSnapshotFile" Click="OpenSnapshotFile_click" />
            <MenuItem Header="_Save"/>
            <MenuItem Header="_Exit" x:Name="ExitApp" Click="ExitAppClick" />
        </MenuItem>
        <MenuItem Header="_Height Map">
            <MenuItem Header="Load Height Map..." Name="LoadHeightMap" Click="LoadHeightMapClick" />
            <Separator />
            <MenuItem Header="Display Height Map" x:Name="DisplayHeightMap" Click="DisplayHeightMapClick" />
        </MenuItem>
        <MenuItem Header="Terrain">
            <MenuItem Header="Load Terrain Map..." x:Name="LoadTerrainMap" Click="LoadTerrainMap_Click" />
            <MenuItem Header="Draw Terrain..." x:Name="DrawTerrain" Click="DisplayTerrainPaintBoxClick" />
            <MenuItem Header="Save Terrain Map..." x:Name="SaveTerrainMap" Click="SaveTerrainMap_Click"/>
            <MenuItem Header="Get Terrain Data From BMP..." x:Name="TerrainFromBMP" Click="TerrainFromBMP_Click" />
            <Separator />
            <MenuItem Header="Adjust Terrain Transparency..." x:Name="AdjustTerrainTransparency"  Click="AdjustTerrainTransparency_Click"/>
            <MenuItem Header="Display Terrain Map" x:Name="DisplayTerrainMap"  Click="DisplayTerrainMap_Click"/>
        </MenuItem>

        <MenuItem Header="_Vegetation">
            <MenuItem Header="Plant Vegetation..." x:Name="PlantVegetation" Click="PlantVegetation_Click" />
            <Separator />
            <MenuItem Header="Load Vegetation Map..." x:Name="LoadVegetation" Click="LoadVegetation_Click" />
            <MenuItem Header="Save Vegetation Map..." x:Name="SaveVegetation" Click="SaveVegetation_Click" />
            <Separator />
            <MenuItem Header="Display Vegetation"  Click="DisplayVegetation_Click" />

        </MenuItem>

        <MenuItem Header="Dinosaurs">
            <MenuItem Header="Edit / Place Dinosaurs..." x:Name="EditDinosaurs" Click="EditDinosaurs_Click" />
            <Separator />
            <MenuItem Header="Load Dinosaur Map" Name="LoadDinosaurnMap" Click="LoadDinosaurs_Click"/>
            <MenuItem Header="Save Dinosaur Map" Name="SaveDinosaurMap" Click="SaveDinosaurs_Click"/>
            <Separator />
            <MenuItem Header="Terrain/Slope Effect..." Name="TerrainSlope"  Click="TerrainSlope_Click"/>
            <MenuItem Header="Probability of Smelling Dinosaur..." Name="SmellProbability"  Click="SmellProbability_Click"/>
        </MenuItem>

        <MenuItem Header="Time">
            <MenuItem Header="Start..." x:Name="AdvanceTime" Click="StartTime_Click" />
            <MenuItem Header="Stop..." x:Name="StopTime" Click="StopTime_Click" />
            <Separator />
            <MenuItem Header="Adjust Time Step..." x:Name="AdjustTimeStep"  Click="AdjustTimeStep_Click"/>
        </MenuItem>

        <MenuItem Header="Debug">
            <MenuItem Header="A* trace" Name="AStarTrace" Click="AStarTrace_Click" />
            <MenuItem Header="Show Dinosaur Goals" Name="DinoGoals" Click="DinoGoals_Click" />
            <MenuItem Header="Show Dinosaur Path" Name="DinoPath" IsChecked="False"  Click="DinoPath_Click" />
            <MenuItem Header="Show Predator Scent Path" Name="StinkPath" IsChecked="False"  Click="StinkPath_Click" />
            <MenuItem Header="Show Dinosaur Vision Angles" Name="VisionAngles" IsChecked="True"  Click="VisionAngles_Click" />
            <MenuItem Header="Show Dinosaur Axis" Name="DinoAxis" IsChecked="True"  Click="DinoAxis_Click" />
        </MenuItem>

        <MenuItem Header="Help">
            <MenuItem Header="About Dinosaur Island" Name="AboutDinosaurIsland" Click="AboutDinoIslandClick" />
        </MenuItem>
    </Menu>

    <StatusBar DockPanel.Dock="Bottom">
        <TextBlock Name="StatusBarField1">Location = X,Y</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField2">Elevation = X</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField3">Terrain = None</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField4">Plants = None</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField5">Dinosaurs = None</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField6">Zoom</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField7">Time 0:00</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField8">Wind direction: 000</TextBlock>
        <Separator/>
        <TextBlock Name="StatusBarField9">Speed: 000</TextBlock>
    </StatusBar>

    <Label DockPanel.Dock="Bottom" Content="Scale = 2000 meters" Height="23" HorizontalAlignment="Center"  Name="HorizScaleDisplayText"  Width="127" />
    <Label DockPanel.Dock="Bottom" Content="└───────────────────────────────┴───────────────────────────────────┘" Height="20" HorizontalAlignment="Center"  Name="HorizScaleDisplayLine"  Width="423" />
    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

        <Slider Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Left" Minimum="1" x:Name="slider"/>
    <ScrollViewer Name="scrollViewer" Grid.Column="1" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Margin="0,0,0,6">

        <Grid Name="grid" Width="400" Height="400" RenderTransformOrigin="0.5,0.5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="37*" />
                    <RowDefinition Height="363*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="297*" />
                    <ColumnDefinition Width="103*" />
                </Grid.ColumnDefinitions>
                <Grid.LayoutTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scaleTransform"/>
                </TransformGroup>
            </Grid.LayoutTransform>
            <Viewbox x:Name="viewBox" Margin="-35,-12,-22,22" Grid.ColumnSpan="2" Grid.RowSpan="2">
                    <ContentPresenter x:Name="contentPresenter" Width="350" Height="350" >
                        <ContentPresenter.Content>
                            <Image x:Name="image" Width="350" Height="350">
                                <Image.Source >
                                    <BitmapImage x:Name="HeightMapImage" UriSource="DinoIslandLogo.bmp" />
                                </Image.Source>
                            </Image>
                        </ContentPresenter.Content>
                    </ContentPresenter>
                </Viewbox>
            </Grid>
        </ScrollViewer>
    </Grid>
</DockPanel>

这就是我显示位图的方式:

this.image.Source = writeableBitmap;
4

1 回答 1

0

这解决了问题:

        public void DrawDinoNames()
    {
        System.Drawing.Bitmap bmp;
        bmp = BitmapFromWriteableBitmap(writeableBitmap);
        Graphics g = Graphics.FromImage(bmp);
        for (int i = 0; i < Dinosaurs.Count; i++)
        {
            g.DrawString(Dinosaurs[i].PersonalName, new Font("Tahoma", 14), System.Drawing.Brushes.White, new PointF((float)Dinosaurs[i].Head.X, (float)Dinosaurs[i].Head.Y));

        }
        this.image.Source = BitmapToImageSource(bmp, System.Drawing.Imaging.ImageFormat.Bmp);
    }
于 2013-10-17T22:14:52.317 回答