1

I wish to return the new data of the image in Base64 string format.

These codes below generate image file format not supported

FileInfo imageFileName = new FileInfo(imageDirectoryFullName + "/image" +
    imageCounter.ToString() + "." + extension);
try
{
    RC2 crypt = RC2.Create();
    ICryptoTransform transform = crypt.CreateEncryptor();

    var output = new CryptoStream(File.Create(imageFileName.FullName),
        transform,
        CryptoStreamMode.Write);

    imageInfo.Bitmap.Save(output, imageFormat);
}

catch (System.Runtime.InteropServices.ExternalException)
{
    return null;
}

or

FileInfo imageFileName = new FileInfo(imageDirectoryFullName + "/image" +
    imageCounter.ToString() + "." + extension);
try
{
    RC2 crypt = RC2.Create();
    ICryptoTransform transform = crypt.CreateEncryptor();

    var output = new CryptoStream(File.Create(imageFileName.FullName),
        new ToBase64Transform(),
        CryptoStreamMode.Write);

    imageInfo.Bitmap.Save(output, imageFormat);
}

catch (System.Runtime.InteropServices.ExternalException)
{
    return null;
}

How can I do this?

  • What is `imageInfo`? There's no information about it in your question and it seems like it'd be the thing that causes it. – Nyerguds Feb 15 '21 at 14:22

2 Answers2

1

I found the function PerformCryptography from this SO post here which takes a ICryptoTransform and can return the result as a byte array.

From there the code looks like this:

private void Start(object sender, EventArgs e)
{
    try
    {
        // Step 01: First load the Image and convert to a byte array
        var imgByteArray = File.ReadAllBytes(@"C:\someImage.jpg");

        // Step 02: Then encrypt & convert to a byte array
        RC2 crypt = RC2.Create();
        ICryptoTransform transform = crypt.CreateEncryptor();

        var cryptByteArray = PerformCryptography(transform, imgByteArray);

        // Step 03: Now convert the byte array to a Base64 String
        string base64String = Convert.ToBase64String(cryptByteArray);
    }

    catch (System.Runtime.InteropServices.ExternalException)
    {
        //return null;
    }
}

Here are the supporting functions:

private byte[] PerformCryptography(ICryptoTransform cryptoTransform, byte[] data)
{
    using (var memoryStream = new MemoryStream())
    {
        using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
        {
            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();
            return memoryStream.ToArray();
        }
    }
}
Ocean Airdrop
  • 2,793
  • 1
  • 26
  • 31
  • Why do you bother reading the image and then re-saving it though? You could just, uhh, read the file and use those bytes. Literally just `File.ReadAllBytes(filePath)`. Especially since `image.RawFormat` just re-saves it in the exact same format it was already in when it was read. – Nyerguds Feb 15 '21 at 14:25
  • Hi @Nyerguds. That's a good spot. I will update the answer as that's more efficient approach – Ocean Airdrop Feb 15 '21 at 17:25
0

Instead of saving to a file stream you should use a memory stream. You can then convert the stream to a byte array and feed into Convert.ToBase64. Take the resulting string and do whatever you want with it.

JonasH
  • 28,608
  • 2
  • 10
  • 23