将图像和文本保存到单个文件中 [英] save image and text into single file

查看:76
本文介绍了将图像和文本保存到单个文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个图像文件(jpeg)和一个文本文件.
有没有一种方法可以将这两件事合并"为新的文件类型并能够将其打开?

我知道如何加密文本文件,但对图像文件一无所知.

Basically i have an image file(jpeg) and a text file .
Is there a way to "merge"this 2 things into a new file type and able to open it up?

I know how to encrypt a text file but had no idea on image file.

推荐答案

是的,可以做到,但是最好使用一些现有代码.
问题在于更改:如果更改了文本或图像,则还必须重新编写另一个.
如果只处理包含一个图像和一组文本的文件,那么这不是主要问题:创建一个Serialisable类,并让框架处理业务.
如果变得更加复杂(N个文件或可变数量的图像/文本),那么我将考虑使用数据库.
有一个SqlCE,在单个用户环境中使用起来非常简单,并且不需要在应用程序中安装任何数据库引擎.
Yes, you can do it, but it might be best to use some existing code.
The problem is changes: If you change either the text or the image, you have to re-write the other one as well.
If you are dealing only with a file which contains one image, and one set of text, then it''s not a major problem: Make a Serialisable class and let the framework handle the business.
If it gets much more complex (N files, or variable numbers of images / texts) then I would consider a database instead.
There is SqlCE which is pretty simple to use in a single user environment, and doesn''t require any database engine installed with your app.




您可以将位图转换为字符串,将其存储在文件中,然后可以将其读回为字符串并将其转换为图像.
您需要确保始终知道文件的哪一部分是文本,哪一部分是图像.对此非常适合的示例是xml.要将位图存储到字符串并返回,可以使用:

Hi,

you can convert the bitmap to a string, store it in a file and then you can read it back as string and convert it to an image.
You need to make sure you always know which part of your file is text and which part is an image. Well suited for this could be for example xml. For storing an bitmap to a string and back you can use:

public string ImageToBase64String( Image image, ImageFormat format )
{
   MemoryStream memory = new MemoryStream();
   image.Save( memory, format );
   string base64 = Convert.ToBase64String( memory.ToArray() );
   memory.Close();
   return base64;
}
public Image ImageFromBase64String( string base64 )
{
   MemoryStream memory = new MemoryStream( Convert.FromBase64String( base64 ) );
   Image result = Image.FromStream( memory );
   memory.Close();
   return result;
}


这篇关于将图像和文本保存到单个文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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