简体   繁体   English

从 C# windows 窗体应用程序中的资源文件夹中检索图像

[英]Retrieving Images from Resource folder in C# windows form application

I am developing project in C# windows application.我正在 C# windows 应用程序中开发项目。 I am new to this technology.我是这项技术的新手。 I declared Image variable in one class and list in another class.我在一个类中声明了 Image 变量并在另一个类中列出。 I want to retrieve image from Resource folder and store it in list ten times.我想从 Resource 文件夹中检索图像并将其存储在列表中十次。 I wrote code like this but it is returning null.我写了这样的代码,但它返回空值。

class clsAddImage
    {
       public Image m_imgSampleImage;
    }
class clsList
    {
        public List<clsAddImage> lstImage = new List<clsAddImage>();   
    }
class clsAddImageToList
    {
        public void AddImgMethod()
        {
            clsList objlist = new clsList();
            int i;
            for (i = 0; i < 10; i++)
            {
                clsAddImage objaddimg = new clsAddImage();
                objlist.lstImage.Add(objaddimg);
            }

            foreach (clsAddImage addimg in objlist.lstImage)
            {
                string path = "C:\\Users\\c09684\\Documents\\Visual Studio         2010\\Projects\\WindowsFormsAddImage\\WindowsFormsAddImage\\Resources\\Chrysanthemum.jpg";
                addimg.m_imgSampleImage = Image.FromFile(path);

            }

            }
    }
 public Form1()
    {
        InitializeComponent();
        clsAddImageToList a = new clsAddImageToList();
          a.AddImgMethod();
    }

I assume that you refer to a Windows8 app?我假设您指的是 Windows8 应用程序? In that case you can not simply program a directory to retrieve information.在这种情况下,您不能简单地编写一个目录来检索信息。 The user has to choose a directory manually, which you can store for future use.用户必须手动选择一个目录,您可以存储该目录以备将来使用。 However, you can have access to KnownFolders (for most you have to check Capabilities in the Package.appxmanifest, eg Pictures Library), see http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.knownfolders for the options.但是,您可以访问已知文件夹(对于大多数情况,您必须检查 Package.appxmanifest 中的功能,例如图片库),请参阅http://msdn.microsoft.com/en-us/library/windows/apps/windows。 storage.knownfolders的选项。

With the following task you will be able to retrieve files from a directory, I hope this helps you solving your problem:通过以下任务,您将能够从目录中检索文件,我希望这可以帮助您解决问题:

public async Task GetFilesFromDisk()
{
    StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
    StringBuilder outputText = new StringBuilder();             

    IReadOnlyList<StorageFile> fileList = await picturesFolder.GetFilesAsync(); 
    var images = new List<BitmapImage>();
    if (fileList != null)
    {
        foreach (StorageFile file in fileList)
        {
            string cExt = file.FileType;

            if (cExt.ToUpper() == ".JPG") 
            {
                Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                using (Windows.Storage.Streams.IRandomAccessStream filestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    await bitmapImage.SetSourceAsync(fileStream);
                }
            }
        }   // ForEach
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM