首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用新保存的图像加密图像时出错

使用新保存的图像加密图像时出错
EN

Stack Overflow用户
提问于 2013-04-10 04:34:23
回答 1查看 294关注 0票数 1

首先,我使用image.save创建图像,下面是一个代码片段:

代码语言:javascript
复制
    private void Save(Bitmap image)
    {
        string fullPath = string.Empty;
        string encryptPath = string.Empty;
        bool isSaved = false;

        try
        {
            // my code                

            image.Save(fullPath, jgpEncoder, myEncoderParameters); // save and image created

            isSaved = true;

            Log.WriteLine("PictureCapturer", "<< Save", LogLevel.Information, 0);
        }
        catch (Exception ex)
        {
            Log.WriteLine("PictureCapturer", "Error when Save : " + ex, LogLevel.Error, 0);
        }
        finally
        {
            image.Dispose();
        }

        if (Properties.EncryptPicture && isSaved)
        {
            Crypto.EncryptFile(fullPath, encryptPath); // start encrypt file
        }
    }

使用Rijndael的文件加密器的代码片段:

代码语言:javascript
复制
    public void EncryptFile(string inputFile, string outputFile)
    {
        try
        {
            string password = @"xxxxxxx";
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, key),
                CryptoStreamMode.Write);

            FileStream fsIn = new FileStream(inputFile, FileMode.Open); // ERROR HERE

            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);

            fsIn.Close();
            cs.Close();
            fsCrypt.Close();
        }
        catch(Exception ex)
        {
            string e = ex.Message;
        }
    } 

但是加密图像时有一个错误:

Message =“进程无法访问文件'D:\Foto\xxxxx.Jpeg‘,因为它正被另一个进程使用”

我想在运行时创建映像和加密图像。

谢谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2013-04-10 05:33:47

信息:

“进程无法访问文件'D:\Foto\xxxxx.Jpeg‘,因为它正在被另一个进程使用。”

这意味着一个程序打开了这个文件,从来没有Closed它。有两件事:

1)确保没有其他程序打开该文件(例如,在Explorer或图像查看器中打开该文件) 2)确保程序当前或以前的迭代没有打开该文件,并且从未关闭它。例如,如果抛出异常,您的程序就不会Close文件。我建议使用finally块来清理资源,比如调用CloseDispose,因为finally块总是在所有代码结束后执行,不管抛出或捕获异常。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15917006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档