Q:

C# / Java Compress JPG image

I need to replicate this code in C# but I have yet to find a good method.    
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

Bitmap d;
this.d.compress(Bitmap.CompressFormat.JPEG, 70, localByteArrayOutputStream);
Byte[] image = localByteArrayOutputStream.toByteArray();

I am sending my own image to a server and it needs to be in the same format as this Java code makes.
How would this be possible in C#? Is there such thing as android.graphics.bitmap.CompressFormat or something equivalent that the server would accept?

A:

It is pretty easy to achieve this in the .Net Framework with the aid of a couple of classes in the System.Drawing and System.Drawing.Imaging namespace. You don't even need to deal with low level stuff such as byte arrays...here's an example that takes a input file path and outputs the results to a different file...
        string inputFile = @"in.jpg";
        string outputFile = @"out.jpg";

        Bitmap inputPhoto = new Bitmap(inputFile);
        ImageCodecInfo decoder = null;

        ImageCodecInfo[] decoders = ImageCodecInfo.GetImageDecoders();
        foreach (ImageCodecInfo c in decoders)
        {
            if (c.FormatID == ImageFormat.Jpeg.Guid)
            {
                decoder = c;
                break;
            }
        }

        System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;

        EncoderParameters encoderParams = new EncoderParameters(1);
        EncoderParameter qualityParam = new EncoderParameter(encoder, 70L);

        encoderParams.Param[0] = qualityParam;
        inputPhoto.Save(outputFile, decoder, encoderParams);

This does exactly what you want, however, if you don't have access to the file system you can always chuck the byte array in a MemoryStream object and use a different overload of the Bitmap class's constructor that takes in a Stream object

