当前位置:   article > 正文

记一次WPF Image不显示图片问题

wpf image不显示

近日做项目时需要用到简单的WPF打印功能,需要打印的页面上有一个Image控件,调试代码的时候可以正常显示图片,但是我离开了调试,直接运行程序,Image中的图片一直显示不出来,网上搜索了很多相关问题,也向chatGPT问了很久都没有办法定位到问题,最后阴差阳错的给我发现了端倪。

框架是.NET6

先上代码:

1.我建了一个WPF类库,里面有一个WPF窗体:MainWindow.xaml

前端代码:

  1. <Window x:Class="PrintWpf.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" SizeToContent="WidthAndHeight" Height="700" Width="794" Loaded="Window_Loaded">
  5. <Grid>
  6. <Canvas x:Name="printArea" Height="600" Width="794">
  7. <Image x:Name="IDCardPhoto" Width="204" Height="321" Margin="458,0,0,0"/>
  8. </Canvas>
  9. </Grid>
  10. </Window>

后端代码:

  1. using ...略
  2. namespace PrintWpf
  3. {
  4. /// <summary>
  5. /// MainWindow.xaml 的交互逻辑
  6. /// </summary>
  7. public partial class MainWindow : Window
  8. {
  9. private string imagePath = string.Empty;
  10. private string returnStr = string.Empty;
  11. public MainWindow(string path)
  12. {
  13. InitializeComponent();
  14. imagePath = path;
  15. }
  16. private void Window_Loaded(object sender, RoutedEventArgs e)
  17. {
  18. WinPosition();
  19. ConvertFilePathToBitmapImage();
  20. PrintDialog printDialog = new PrintDialog();
  21. if (printDialog.ShowDialog() == true)
  22. {
  23. printDialog.PrintVisual(printArea, "Print Window");
  24. returnStr = "complete";
  25. this.Close();
  26. }
  27. else
  28. {
  29. returnStr = "cancel";
  30. this.Close();
  31. }
  32. }
  33. /// <summary>
  34. /// 修改窗体初始显示位置
  35. /// </summary>
  36. private void WinPosition()
  37. {
  38. double ScreenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;//WPF
  39. this.Top = this.ActualWidth / 5;
  40. this.Left = ScreenWidth - this.ActualWidth * 1.3;
  41. }
  42. /// <summary>
  43. /// windows文件绝对路径转化为WPF中Image控件的source属性
  44. /// </summary>
  45. /// <param name="filePath"></param>
  46. /// <returns></returns>
  47. public void ConvertFilePathToBitmapImage()
  48. {
  49. BitmapImage bitmapImage = new BitmapImage();
  50. bitmapImage.BeginInit();
  51. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  52. bitmapImage.StreamSource = new MemoryStream(File.ReadAllBytes(imagePath));
  53. bitmapImage.EndInit();
  54. bitmapImage.Freeze();
  55. IDCardPhoto.Source = bitmapImage;
  56. }
  57. public string ShowDia()
  58. {
  59. this.ShowDialog();
  60. return returnStr;
  61. }
  62. }
  63. }

2.我建了一个winform项目,在里面调用上述的WPF窗体

  1. string source = "图片来源(绝对路径)";
  2. MainWindow mainWindow = new MainWindow(source);
  3. string str = mainWindow.ShowDia();
  4. if (str == "complete")
  5. {
  6. MessageBox.Show("启动打印");
  7. }
  8. else
  9. {
  10. MessageBox.Show("不启动打印");
  11. }

以上代码在我调试的时候,能正常显示出图片内容并打印无误,但是当我直接运行Debug里的程序时,图片就无法显示了,后来发现是我写的有问题,PrintDialog的调用放在了Window_Loaded里面导致图片还没完成加载就占用了窗体资源,导致无法显示图片。

于是我对上述代码做了一些修改,把PrintDialog的调用移出来

前端代码:(添加了一个打印按钮)

  1. <Window x:Class="PrintWpf.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" SizeToContent="WidthAndHeight" Height="700" Width="794" Loaded="Window_Loaded">
  5. <Grid>
  6. <Button Content="打印" Canvas.Left="9" HorizontalAlignment="Left" VerticalAlignment="Top" Height="39" Width="92" Click="Button_Click"/>
  7. <Canvas x:Name="printArea" Height="600" Width="794">
  8. <Image x:Name="IDCardPhoto" Width="204" Height="321" Margin="458,0,0,0"/>
  9. </Canvas>
  10. </Grid>
  11. </Window>

后端代码:(把PrintDialog的调用放到按钮事件中)

  1. using ...略
  2. namespace PrintWpf
  3. {
  4. /// <summary>
  5. /// MainWindow.xaml 的交互逻辑
  6. /// </summary>
  7. public partial class MainWindow : Window
  8. {
  9. private string imagePath = string.Empty;
  10. private string returnStr = string.Empty;
  11. public MainWindow(string path)
  12. {
  13. InitializeComponent();
  14. imagePath = path;
  15. }
  16. private void Window_Loaded(object sender, RoutedEventArgs e)
  17. {
  18. WinPosition();
  19. ConvertFilePathToBitmapImage();
  20. }
  21. /// <summary>
  22. /// 修改窗体初始显示位置
  23. /// </summary>
  24. private void WinPosition()
  25. {
  26. double ScreenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;//WPF
  27. this.Top = this.ActualWidth / 5;
  28. this.Left = ScreenWidth - this.ActualWidth * 1.3;
  29. }
  30. /// <summary>
  31. /// windows文件绝对路径转化为WPF中Image控件的source属性
  32. /// </summary>
  33. /// <param name="filePath"></param>
  34. /// <returns></returns>
  35. public void ConvertFilePathToBitmapImage()
  36. {
  37. BitmapImage bitmapImage = new BitmapImage();
  38. bitmapImage.BeginInit();
  39. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  40. bitmapImage.StreamSource = new MemoryStream(File.ReadAllBytes(imagePath));
  41. bitmapImage.EndInit();
  42. bitmapImage.Freeze();
  43. IDCardPhoto.Source = bitmapImage;
  44. }
  45. public string ShowDia()
  46. {
  47. this.ShowDialog();
  48. return returnStr;
  49. }
  50. private void Button_Click(object sender, RoutedEventArgs e)
  51. {
  52. PrintDialog printDialog = new PrintDialog();
  53. if (printDialog.ShowDialog() == true)
  54. {
  55. printDialog.PrintVisual(printArea, "Print Window");
  56. returnStr = "complete";
  57. this.Close();
  58. }
  59. else
  60. {
  61. returnStr = "cancel";
  62. this.Close();
  63. }
  64. }
  65. }
  66. }

这下图片加载跟打印分成了两部分,互不冲突,能正常显示图片和实现打印了。

这两天还搜到一些其他关于WPF Image控件不显示图片的问题回答,

有的说路径不对的,

有的说要把图片引入到项目中,并右键属性设置为资源的,这里就不展开说了,因为我的图片不是固定资源,是实时读取别的地方的文件。

ok,希望能帮到你。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号