PNG 和 jpg 图像未出现在 C# 应用程序中
我在使用 C# 在应用程序中显示某些图像时遇到问题。我使用 Image 类来指定位置,并使用 BitmapImage 来指定源。 UriSource 是相对的,我只需指定名称。它对某些图像有效,但对于其他图像,图像根本不出现。我的图像实例大小为 35x35,另一个大小为 100x100(像素)。
任何人都知道为什么会发生这种情况以及如何解决它?
谢谢。 这是我使用的代码:
Image removeImage = new Image();
removeImage.HorizontalAlignment = HorizontalAlignment.Left;
removeImage.VerticalAlignment = VerticalAlignment.Top;
removeImage.Margin = new Thickness(490, 10, 0, 0);
removeImage.Width = 35;
removeImage.Height = 35;
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("delete.png", UriKind.RelativeOrAbsolute);
source.EndInit();
removeImage.Source = source;
removeImage.Stretch = Stretch.None;
removeImage.Visibility = Visibility.Visible;
removeImage.MouseDown += new MouseButtonEventHandler(removeImage_MouseDown);
I have a problem with displaying certain images in my application using C#. I am using the Image class to specify the location and the BitmapImage to specify the source. The UriSource is relative and I just specify the name. It worked for some images, but for others, the image simply does not appear. My image instance is 35x35 big and another is 100x100 big (pixels).
Anyone knows why this might be occurring and how to fix it?
Thanks.
Here's the code I used:
Image removeImage = new Image();
removeImage.HorizontalAlignment = HorizontalAlignment.Left;
removeImage.VerticalAlignment = VerticalAlignment.Top;
removeImage.Margin = new Thickness(490, 10, 0, 0);
removeImage.Width = 35;
removeImage.Height = 35;
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("delete.png", UriKind.RelativeOrAbsolute);
source.EndInit();
removeImage.Source = source;
removeImage.Stretch = Stretch.None;
removeImage.Visibility = Visibility.Visible;
removeImage.MouseDown += new MouseButtonEventHandler(removeImage_MouseDown);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定图像文件的位置。如果图像位于当前项目文件夹中,则必须从属性窗口设置图像文件的
复制到输出目录=始终复制
属性。Not sure about the location of image files. If images are in your current project folder then you have to set
Copy To Output Directory=Copy Always
property of image file from Properties Windows.据我所知,诊断此类问题的最佳方法(假设对代码进行快速同行评审毫无结果)是使用 ProcessMonitor:http://technet.microsoft.com/en-us/sysinternals/bb896645
您可以使用此工具监视计算机上的所有文件活动(确保使用包含/排除过滤器来限制噪音)。
图像未显示的原因很可能是因为您的应用程序在错误的位置查找它们(它们没有被复制,或者相对路径已关闭)。
ProcessMonitor 将记录 Windows 访问您的 .jpg 的每次尝试(无论失败还是成功)。如果您在日志中搜索文件名,您应该会找到它,可能还包含一条错误消息,以及 Windows 用于打开该文件的完整路径。
我看到的最常见的结果是
在所有这些情况下,ProcessMonitor 都会向您显示发生了什么。
The best way that I know of to diagnose a problem like that (assuming a quick peer review of the code gets you nowhere), is to use ProcessMonitor: http://technet.microsoft.com/en-us/sysinternals/bb896645
You can use this tool to monitor all of the file activity on your machine (make sure to use the include/exclude filters to limit the noise).
It's very likely that the reason that the images are not showing up is because your application is looking for them in the wrong place (either they didn't get copied, or the relative path is off).
ProcessMonitor will log every attempt that Windows makes to access your .jpg (whether it fails or succeeds). If you search for your file name in the log, you should find it, probably along with an error message, and the full path that Windows was using to open the file.
The most common results I see are
In all those cases, ProcessMonitor will show you what happened.