简体   繁体   English

如何存储图片不使用资源

[英]How to store images do not use the resources

I have a C # application and I need to keep in her pictures, but I can not use resources (resx).我有一个 C # 应用程序,我需要保留她的图片,但我不能使用资源 (resx)。 Is there then an alternative method?那么有没有替代的方法呢?

update: ANSWER: My images is static, for this I will use Embedded Resource更新:答案:我的图像是 static,为此我将使用嵌入式资源

How create embedded resource如何创建嵌入式资源

As mentioned above, it really depends on whether or not they are static or dynamic.如上所述,这实际上取决于它们是 static 还是动态的。 If static, you could use an Embedded Resource (instead of encoding them as constant byte arrays, as others have mentioned).如果 static,您可以使用嵌入式资源(而不是像其他人提到的那样将它们编码为常量字节 arrays)。 If dynamic, you could store them in isolated storage.如果是动态的,您可以将它们存储在隔离存储中。

Is this a dynamic set of pictures or static?这是一组动态图片还是static? If static, you can compile the resource file as a separate assembly and then consume it.如果 static,您可以将资源文件编译为单独的程序集,然后使用它。 If dynamic, you need some type of data store to store the files.如果是动态的,您需要某种类型的数据存储来存储文件。 If file system does not work, a database is an option.如果文件系统不起作用,则可以选择数据库。

One idea comes to mind, but I probably wouldn't use it if at all possible: put the images into your code as arrays of bytes.想到一个想法,但如果可能的话,我可能不会使用它:将图像作为 arrays 字节放入您的代码中。

You could create byte[] fields for the images and initialize them in code.您可以为图像创建byte[]字段并在代码中初始化它们。 You could dynamically generate the code using either CodeDOM or Reflection.Emit.您可以使用 CodeDOM 或 Reflection.Emit 动态生成代码。

If images are not too large you can encode them into huge constant arrays in code.如果图像不是太大,您可以在代码中将它们编码为巨大的常量 arrays。 This is not very elegant solution but what else if no file, no resource, no database?这不是很优雅的解决方案,但如果没有文件、没有资源、没有数据库,还能怎么办?

Given the options you gave us, that doesn't leave us with much to work with.鉴于您给我们的选项,这并没有给我们留下太多的工作余地。 Depending on the image size, you can use this website to encoding your images to Base64 and then store them in a string .根据图像大小,您可以使用此网站将图像编码为 Base64,然后将它们存储在string中。 Then you can use System.Convert.FromBase64String to convert them from base64 back to a byte array.然后您可以使用System.Convert.FromBase64String将它们从 base64 转换回字节数组。

Another option would be to store your images on a web server and create a webservice on the server that you can consume to retreive the images.另一种选择是将您的图像存储在 web 服务器上,并在服务器上创建一个 Web 服务,您可以使用它来检索图像。

These are not good methods though.不过这些都不是好方法。 The first idea is consuming a lot more memory then it would need to and the second method is just kind of ridiculous if you think about it.第一个想法是消耗更多的 memory 然后它需要,如果你仔细想想,第二种方法有点荒谬。 It is much much much better to use a database.使用数据库要好得多。 You can use a database such as MySQL that will work with both Windows and Mono.您可以使用MySQL等数据库,该数据库将与 Windows 和 Mono 一起使用。 You can also use SQLite database so that the database is portable.您还可以使用SQLite 数据库,以便该数据库是可移植的。 There really isn't a good reason to store this in a database.确实没有充分的理由将其存储在数据库中。

I would go on my line: you can save on disk by using any Mono ZIP library that provides secure encryption.我会在我的线路上使用 go:您可以使用任何提供安全加密的Mono ZIP库来保存在磁盘上。

Zip : cause you potentially will save a space Zip :因为您可能会节省空间

Encryption : if you need save some private data (family images, documents...)加密:如果您需要保存一些私人数据(家庭图像、文档...)

Can group them based on your own app logic, or save them individually, with also some metafile attached, if you need also provide some information on image.可以根据您自己的应用程序逻辑对它们进行分组,或者单独保存它们,并附加一些元文件,如果您还需要提供一些图像信息。

Microsoft uses this kind of "technology" for example for DOCX files. Microsoft使用这种“技术”,例如DOCX文件。 Try to change an extension of DOCX file to ZIP and unzip it into the folder.尝试将 DOCX 文件的扩展名更改为 ZIP 并将其解压缩到文件夹中。 You will see the content.你会看到内容。

Regards.问候。

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

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