如何在wpf中加载png文件,并使其大小与在Paint中相同

本文关键字:Paint wpf 加载 png 文件 | 更新日期: 2023-09-27 18:16:42

我在使用VS 2008 3.5框架的WPF中加载pgn文件时遇到问题。文件加载和显示正常,但大小不正常。我研究了其他问题,并尝试了各种建议,但都无济于事。

我从一个36 x 50的pgn文件中的图像开始。然后我用Windows Paint把它缩小到29 x 40。我将其加载到system . windows . media . image . bitmap对象中,然后将其用作System.Windows.Controls.Image对象的源。当图像显示时,它看起来比"绘制"中的相同图像大。下面是我所做的尝试来解决这个问题:

  1. 验证读取的文件具有正确的宽度和高度。查看调试器中的位图对象,它显示宽度= 29,高度= 40。

  2. 设置拉伸= none。

  3. 尝试使用System.Drawing.Image对象来查看pgn文件的水平和垂直分辨率。

  4. 尝试使用pgnout,这是一个pgn文件压缩程序对我的一些文件图像。它还应该将图像分辨率设置为96。它对显示图像的大小没有影响。

  5. 尝试将显示的大小与不同大小的图像进行比较。看起来显示的高度是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中使图像显示与在Paint和其他图像编辑器中显示的大小相同?我做错什么了吗?如有任何建议,我将不胜感激。

如何在wpf中加载png文件,并使其大小与在Paint中相同

这里几乎是相同的问题:WPF:如何以原始尺寸显示图像?

Stretch="None"将使图像比例保持在100%。

你也可以改变ImageBox的大小为相同的图像。

一些样品:

保持宽高比

imageBox.Stretch="None"

更改大小:

imageBox.Width = myImage.Width ;
imageBox.Height = myImage.Height;

限制最大尺寸:

if (imageBox.ActualHeight > 500)
    imageBox.Height = 500;
if (imageBox.ActualWidth > 800)
     imageBox.Width = 800;