public static int SaveJPG(byte[] pixels, int w, int h, string Filename)
{
// Define the image palette
BitmapPalette myPalette = BitmapPalettes.Halftone256;
// Creates a new empty image with the pre-defined palette
BitmapSource image = BitmapSource.Create(
w,
h,
96,
96,
PixelFormats.Rgb24,
myPalette,
pixels,
w * 3);
FileStream stream = new FileStream(Filename, FileMode.Create);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
//encoder.Interlace = PngInterlaceOption.On;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
stream.Close();
return 0;
}