C#位图仅在指定静态文件地址时加载 [英] C# Bitmap only loads when static file address is specified

查看:51
本文介绍了C#位图仅在指定静态文件地址时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个加载图像(png)的方法,并将其存储在稍后要使用的Bitmap变量中。代码如下:



I'm trying to create a method which loads a image (png) and stores it in a Bitmap variable to be used later. The code is as follows:

private static Bitmap LoadImage(string subfolderName, string imageName) {
           string fileSpec = string.Format(@".\Images\{0}\{1}.png", subfolderName, imageName);

           Bitmap bitmap = new Bitmap(fileSpec);


           return bitmap;
       }



正如您所看到的那样,我将文件路径存储在fileSpec中,然后将其用作创建新参数的参数位图。问题是当我使用它时,我得到以下错误:




As you can see its fairly simple, I store the file path in fileSpec and then use that as the parameter for creating the new bitmap. The issue is when I use this is that I get the following error:

"Parameter is not Valid"



错误指向此行:




The error points to this line:

Bitmap bitmap = new Bitmap(fileSpec);  





我发现当文件不存在时会出现这个错误,但是文件在那里,当我在使用静态文件地址,例如:





I have found that this is error is meant to happen when a file is non-existent, but the file is there and when I use a static file address such as this:

private static Bitmap LoadImage(string subfolderName, string imageName) {

            string st = @".\Images\Cards\CardBack_Red.png"; //did this to test if it was working in any capacity at all

            Bitmap bitmap = new Bitmap(st);

            return bitmap;
        }



我没有错误,图片加载正常。我错过了一些非常明显的东西吗帮助真的很感激,我现在​​非常沮丧和绝望。



谢谢。


I get no error and image loads fine. Am I missing something really obvious? Help would really be appreciated, I'm getting really frustrated and desperate at this point.

Thanks.

推荐答案

首先做两件事。

首先,稍微改变你的代码:

Start by doing two things.
First, change your code slightly:
string fileSpec = string.Format(@".\Images\{0}\{1}.png", subfolderName, imageName);

成为:

Becomes:

string fileSpec = Path.Combine(@".\Images", subfolderName, imageName + ".png");

这样就可以删除你的参数已经跟踪\字符的任何可能性,这些字符会破坏生成的路径。

然后使用调试器来查看确切的生成的字符串 - 如果应该与您显示的手动输入的字符串相同。



其次,考虑使用更好的路径规范:如果相对路径可能会非常混乱您的代码运行条件甚至略有变化。如果您要从应用程序中编写这些文件,请考虑将它们移动到可写文件夹:我应该在哪里存储我的数据? [ ^ ]应该有帮助。

如果你不写它们,这些都是只读的,那么考虑使用

This removes any chances that your parameters already have trailing "\" characters which are messing up the resulting path.
Then use the debugger to look at exactly what string that generates - if should be identical to the manually entered string you show.

Second, consider using a better path specification: relative paths can get very confused if the conditions your code is running under change even slightly. If you are ever writing these files from within you application, then consider moving them to a "writable" folder: Where should I store my data?[^] should help.
If you don't write them, and these are readonly, then consider using

string fileSpec = Path.Combine(Application.StartupPath, @"Images", subfolderName, imageName + ".png");


这篇关于C#位图仅在指定静态文件地址时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆