PNG 和 jpg 图像未出现在 C# 应用程序中

发布于 2024-12-20 20:26:08 字数 971 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-12-27 20:26:08

不确定图像文件的位置。如果图像位于当前项目文件夹中,则必须从属性窗口设置图像文件的复制到输出目录=始终复制属性。

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.

梦里人 2024-12-27 20:26:08

据我所知,诊断此类问题的最佳方法(假设对代码进行快速同行评审毫无结果)是使用 ProcessMonitor:http://technet.microsoft.com/en-us/sysinternals/bb896645

您可以使用此工具监视计算机上的所有文件活动(确保使用包含/排除过滤器来限制噪音)。

图像未显示的原因很可能是因为您的应用程序在错误的位置查找它们(它们没有被复制,或者相对路径已关闭)。

ProcessMonitor 将记录 Windows 访问您的 .jpg 的每次尝试(无论失败还是成功)。如果您在日志中搜索文件名,您应该会找到它,可能还包含一条错误消息,以及 Windows 用于打开该文件的完整路径。

我看到的最常见的结果是

  1. 实际使用的路径与您需要的路径不同。
  2. 路径是正确的,但您的文件不存在(构建/复制/安装问题)
  3. 路径是正确的,但您的 Web 应用程序没有读取该文件的权限。

在所有这些情况下,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

  1. Path that was actually being used was different from the path you needed.
  2. The path was correct, but your files weren't there (build/copy/install problem)
  3. The path was correct, but your web app did not have permissions to read the file.

In all those cases, ProcessMonitor will show you what happened.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文