首先我使用 image.save 创建图像,这是一个片段代码:
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 的文件加密器的代码段:
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',因为它正被另一个进程使用。"
仅供参考,我想在运行时创建图像并加密图像。
感谢您的帮助