我在使用 VS 2008 3.5 框架在 WPF 中加载 pgn 文件时遇到问题。文件加载并显示正常,但大小已关闭。我研究了其他问题,并尝试了各种建议,但无济于事。
我从一个 36 x 50 的 pgn 文件中的图像开始。然后我使用 Windows Paint 将其缩小到 29 x 40。我将它加载到 System.Windows.Media.Imaging.Bitmap 对象中,然后将其用作System.Windows.Controls.Image 对象的源。显示图像时,它看起来比画图中的同一图像大。这是我为解决问题所做的工作:
验证读入的文件具有正确的宽度和高度。查看调试器中的位图对象,它显示宽度 = 29 和高度 = 40。验证系统 dpi 已从此链接的代码中设置为 96 。
设置拉伸 = 无。已经没有效果。
尝试使用 System.Drawing.Image 对象查看 pgn 文件的水平和垂直分辨率。它们都设置为 96。
尝试使用pgnout,这是我的一些文件图像上的 pgn 文件压缩程序。它还应该将图像分辨率设置为 96。它对显示图像的大小没有影响。
尝试将显示的尺寸与不同尺寸的图像进行比较。显示的高度似乎是 31 像素。宽度约为 34 像素。曾尝试使用其他编辑器,如 Paint.Net 和 Gimp,但它们产生的结果与 Paint 相同。
我的想法不多了。我看到的唯一解决方法是进入 Paint 并将我的所有图像重新调整为较小的尺寸,以便它们在 WPF 中显示时是正确的尺寸。
这是我加载 pgn 文件和创建图像的代码:
private void LoadCardbackBitmapFromFile()
{
// This method loads the cardback image from the pgn file.
//
string CardBackFileName = AppSettings.CardsbackFolder + @"\" + AppSettings.CardsBackFileName + AppSettings.CardsBackIndex.ToString() + ".png";
// Check if the file exists.
if (!File.Exists(CardBackFileName))
{
StringBuilder SBLog = new StringBuilder("AttackPoker2::Cards::Cards - unrecoverable error - could not find the card back file " + CardBackFileName.ToString() + ".\n");
SSLSocketInterface.LogMsgCPP(SBLog, LogLevel.LogInfo);
string S = "Unrecoverable error - could not find the card back file " + CardBackFileName.ToString() + ".\nInstallation may be corrupt. Try re-installing or re-locating the cards folder.";
throw new ApplicationException(S);
}
CardbackBitmap = new BitmapImage();
CardbackBitmap.BeginInit();
CardbackBitmap.UriSource = new Uri(CardBackFileName, UriKind.Absolute);
CardbackBitmap.CacheOption = BitmapCacheOption.OnLoad;
CardbackBitmap.EndInit();
CardbackImage = new Image();
CardbackImage.Source = CardbackBitmap;
CardbackImage.Stretch = Stretch.None;
CardbackImage.VerticalAlignment = VerticalAlignment.Top;
CardbackImage.HorizontalAlignment = HorizontalAlignment.Left;
}
private void CreateCardbackImages()
{
// This method creates clones of the cardback image so that they can be displayed simultaneously. WPF does not provide the capability to reuse ui objects.
//
for (int SeatLoop = 0; SeatLoop < NumSeats; SeatLoop++)
{
for (int CardLoop = 0; CardLoop < NumPlayerCards; CardLoop++)
{
Image CBI = new Image();
CBI.Source = CardbackImage.Source;
CBI.Stretch = CardbackImage.Stretch;
// Set the card so it is not viewable.
CBI.Visibility = Visibility.Collapsed;
CardbackImages[SeatLoop, CardLoop] = CBI;
CardsGrid.Children.Insert(SeatLoop, CBI);
}
}
}
//
private Grid LayoutRoot; // The main ui grid.
private Grid CardsGrid; // Used to place the the card images.
private BitmapImage CardbackBitmap; // Used to hold the image read in from the pgn file.
private static Image CardbackImage; // Used to hold the original cardback image.
private Image[,] CardbackImages; // Holds the list of cardback images used to deal the cards to each player. 1st dim is the seat; 2nd dim is the card number.
这是 XAML:
<Window x:Class="A2.GameWindow" Width="600" Height="497"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Closing="GameWindow_Closing">
<Viewbox HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Fill">
<Grid Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="0.85*"/>
<RowDefinition Height="0.15*"/>
</Grid.RowDefinitions>
<Image Source="Table Green.png" Stretch="Fill"/>
<TabControl Grid.Row="1" FontSize="9" FontFamily="Fonts/#Segoe UI Semibold" FontWeight="Bold">
<TabItem Header="Chat">
<StackPanel>
<TextBox Name="TxtTyping" Width="193.75" FontSize="8" MaxLines="1" Margin="-285,0,0,0"></TextBox>
<TextBox Name="TxtMessages" Width="193.75" Height="64.125" FontSize="8" Margin="-285,0,0,0" Background="#FFC8F0EC"></TextBox>
</StackPanel>
</TabItem>
<TabItem Header="Notes">
<StackPanel>
<ComboBox Name="NotesCombo" Width="193.75" Margin="-285,0,0,0"></ComboBox>
<TextBox Name="TxtNotesMessages" Width="193.75" Height="64.125" Margin="-285,0,0,0"></TextBox>
</StackPanel>
</TabItem>
<TabItem Header="Stats">
<TextBox Name="TxtStats" Width="193.75" Height="64.125" Margin="-285,0,0,0" Background="#FFC8F0EC"></TextBox>
</TabItem>
<TabItem Header="Info">
<TextBox Name="TxtInfo" Width="193.75" Height="64.125" Margin="-285,0,0,0" Background="#FFC8F0EC"></TextBox>
</TabItem>
</TabControl>
</Grid>
</Viewbox>
</Window>
有谁知道问题可能是什么?如何使图像在 WPF 中显示与在画图和其他图像编辑器中相同的大小?我做错什么了吗?任何建议,将不胜感激。