如何将位于我的资源库中的图像放入数组中?

时间:2022-03-03 06:10:49

I want to load 52 images (deck of cards) in gif format from my recourse folder into an Image[] in c#. Any ideas?

我想从我的recourse文件夹中将gif格式的52张图像(卡片组)加载到c#中的Image []中。有任何想法吗?

Thanks, Jon

4 个解决方案

#1


4  

You can read a Bitmap from a file like this;

你可以从这样的文件中读取一个Bitmap;

  public static Bitmap GetBitmap( string filename )
  {
     Bitmap retBitmap = null;
     string path = String.Concat( BitmapDir, filename );
     if ( File.Exists( path ) )
     {
        try
        {
           retBitmap = new Bitmap( path, true );
        }
        catch { }
     }
     return retBitmap;
  }

You can get a list of files in a resource directory like this;

您可以像这样获取资源目录中的文件列表;

string[] files = Directory.GetFiles( BitmapDir, "*.gif" );

Just iterate through files calling GetBitmap( file ) and populate your array. BitmapDir is obviously the directory where your GIF files live.

只需遍历调用GetBitmap(文件)的文件并填充数组即可。 BitmapDir显然是您的GIF文件所在的目录。

#2


1  

Assuming that you have the images in a folder on your local file system and that you are running under .NET 3.5:

假设您将图像放在本地文件系统的文件夹中,并且您在.NET 3.5下运行:

Image[] cards = Directory.GetFiles(cardsFolder).Select(f => Image.FromFile(f)).ToArray();

One-liners are always nice :-)

单行总是很好:-)

#3


1  

If your resources are not being copied to a folder because they are embedded you shoud use Reflection, a start point would be this (the images are stored in the Resource file as a method):

如果您的资源没有被复制到文件夹,因为它们是嵌入式的,您应该使用Reflection,一个起点就是这个(图像作为方法存储在Resource文件中):

   List<System.Drawing.Image> images = new List<System.Drawing.Image>();
        foreach (System.Reflection.MethodInfo t 
            in typeof(Resources.Resource).GetMethods())
        {
            if (t.ReturnType.ToString() == "System.Drawing.Bitmap")
            {
                images.Add(new System.Drawing.Bitmap((System.Drawing.Image)t.Invoke(null, null)));

            }
        }

#4


1  

Perhaps it is better to verify if the file is an image, because if not, exception is thrown:

也许最好验证文件是否是图像,因为如果没有,则抛出异常:

 protected void MethodToBeCalled()
    {


        System.Drawing.Image[] cards = Directory.GetFiles(cardsFolder).Where(
              f =>
              {

                  if (IsImage((string)f))
                  {
                      return true ;
                  }
                  else { return false; }
              }
            ).Select(f => System.Drawing.Image.FromFile(f)).ToArray();

    }
        private bool IsImage(string filename)
    {
        string[] knownPicExtensions = {".jpg",".gif",".png",".bmp",".jpeg",".jpe" };

        foreach (string extension in knownPicExtensions)
        {
            if (filename.ToLower().EndsWith(extension))
                return true;
        }

        return false;
    }

#1


4  

You can read a Bitmap from a file like this;

你可以从这样的文件中读取一个Bitmap;

  public static Bitmap GetBitmap( string filename )
  {
     Bitmap retBitmap = null;
     string path = String.Concat( BitmapDir, filename );
     if ( File.Exists( path ) )
     {
        try
        {
           retBitmap = new Bitmap( path, true );
        }
        catch { }
     }
     return retBitmap;
  }

You can get a list of files in a resource directory like this;

您可以像这样获取资源目录中的文件列表;

string[] files = Directory.GetFiles( BitmapDir, "*.gif" );

Just iterate through files calling GetBitmap( file ) and populate your array. BitmapDir is obviously the directory where your GIF files live.

只需遍历调用GetBitmap(文件)的文件并填充数组即可。 BitmapDir显然是您的GIF文件所在的目录。

#2


1  

Assuming that you have the images in a folder on your local file system and that you are running under .NET 3.5:

假设您将图像放在本地文件系统的文件夹中,并且您在.NET 3.5下运行:

Image[] cards = Directory.GetFiles(cardsFolder).Select(f => Image.FromFile(f)).ToArray();

One-liners are always nice :-)

单行总是很好:-)

#3


1  

If your resources are not being copied to a folder because they are embedded you shoud use Reflection, a start point would be this (the images are stored in the Resource file as a method):

如果您的资源没有被复制到文件夹,因为它们是嵌入式的,您应该使用Reflection,一个起点就是这个(图像作为方法存储在Resource文件中):

   List<System.Drawing.Image> images = new List<System.Drawing.Image>();
        foreach (System.Reflection.MethodInfo t 
            in typeof(Resources.Resource).GetMethods())
        {
            if (t.ReturnType.ToString() == "System.Drawing.Bitmap")
            {
                images.Add(new System.Drawing.Bitmap((System.Drawing.Image)t.Invoke(null, null)));

            }
        }

#4


1  

Perhaps it is better to verify if the file is an image, because if not, exception is thrown:

也许最好验证文件是否是图像,因为如果没有,则抛出异常:

 protected void MethodToBeCalled()
    {


        System.Drawing.Image[] cards = Directory.GetFiles(cardsFolder).Where(
              f =>
              {

                  if (IsImage((string)f))
                  {
                      return true ;
                  }
                  else { return false; }
              }
            ).Select(f => System.Drawing.Image.FromFile(f)).ToArray();

    }
        private bool IsImage(string filename)
    {
        string[] knownPicExtensions = {".jpg",".gif",".png",".bmp",".jpeg",".jpe" };

        foreach (string extension in knownPicExtensions)
        {
            if (filename.ToLower().EndsWith(extension))
                return true;
        }

        return false;
    }