Click here to Skip to main content
15,877,857 members
Articles / Multimedia / GDI+

A Simple JPEG Encoder in C#

Rate me:
Please Sign up or sign in to vote.
4.86/5 (42 votes)
1 Jun 2010CPOL8 min read 200K   14.8K   92   34
Baseline JPEG encoding implemented using C#.


JPEG is one of the most widely used standards for encoding photographs, pictures, or other visual content. But, the internal workings are mostly overlooked. We are used to using image.Save("filename",Imaging.ImageFormat.Jpeg);, but what's going inside; remains a big mystery for the programmer.

Most of the available implementations of JPEG Encoder/Decoder are in C/C++. But, for a project, my friend 'anirban' required a JPEG encoder in C#. So, we started writing our own encoder. It turned out to be very complex and difficult. The one we wrote from scratch was really-really slow.. some functions had fourth order complexity (mostly the DCT part). Somehow, we managed to get some 'C' code from a forum. I converted parts of the 'C' code to C#; it was tedious work, but it worked pretty fast due to the fast DCT (AA&N).

The encoder is not enhanced too much for speed, but it works properly.

This article will not explain in detail how this all works, but will give an overview of the workings of JPEG and the wonderful work the JPEG standard people have done. If you want to properly understand the JPEG standard, then visit the official JPEG site or the IJG site.



The program displaying an image with its three (RGB) channels separated.

Clicking on the individual channel picture boxes shows the individual channel images on the main PictureBox, and clicking on the main PictureBox shows the original loaded image.

Write-original saves the original image using the JPEG encoder, whereas clicking on the 'Write Current' button converts the picture being displayed on the main PictureBox to an image pixel array and then saves it. The latter option can be used for saving individual channel images.


The Y channel of the same image.


I am not an expert in JPEG. But, from what I have learnt, the basic steps are...

1. The afine transformation in colour space: [R G B] -> [Y Cb Cr]

(It is defined in the CCIR Recommendation 601.)

The YCC colour space used follows that used by TIFF and JPEG (Rec 601-1):

Y  =  0.2989 R + 0.5866 G + 0.1145 B
Cb = -0.1687 R - 0.3312 G + 0.5000 B
Cr =  0.5000 R - 0.4183 G - 0.0816 B

RGB values are normally on the scale of 0 to 1, or since they are stored as unsigned single bytes, 0 to 255. The resulting luminance value is also on the scale of 0 to 255; the chrominance values need 127.5 added to them so they can be saved in an unsigned byte.

Y or luminance is the intensity of an RGB colour perceived by the eye. The formula for Y is like a weighted-filter with different weights for each spectral component: the eye is most sensitive to the Green component, then it follows the Red component, and last is the Blue component.

The values Cb and Cr are called the chrominance values, and represent two coordinates in a system which measures the nuance and saturation of the colour ([approximately], these values indicate how much blue and how much red is in that colour).

2. Sampling

The JPEG standard takes into account the fact that the eye seems to be more sensitive at the luminance of a colour than at the nuance of that colour. (The white-black view cells have more influence than the day view cells.)

So, on most JPGs, luminance is taken in every pixel while the chrominance is taken as a medium value for a 2x2 block of pixels. Note that it is not necessary that the chrominance be taken as a medium value for a 2x2 block; it could be taken in every pixel, but good compression results are achieved this way, with almost no loss in visual perception of the new sampled image.

3. Level shift

All 8-bit unsigned values (Y, Cb, Cr) in the image are "level shifted": they are converted to an 8-bit signed representation, by subtracting 128 from their value.

4. Block splitting

Each channel must be split into 8X8 blocks of pixels. If the data for a channel does not represent an integer number of blocks, then the encoder must fill the remaining area of the incomplete blocks with some form of dummy data. Filling the edge pixels with a fixed color (typically black) creates ringing artifacts along the visible part of the border; repeating the edge pixels is a common technique that reduces the visible border, but it can still create artifacts. We are currently filling the dummy pixels with zeros (black).

5. The 8x8 Discrete Cosine Transform (DCT)

DCT is then applied to the 8x8 blocks.

The mathematical definition of Forward DCT (FDCT) is:

        c(u,v)     7   7                 2*x+1                2*y+1
F(u,v) = --------- * sum sum f(x,y) * cos (------- *u*PI)* cos (------ *v*PI)
          4       x=0 y=0                 16                   16

 u,v = 0,1,...,7

      { 1/2 when u=v=0
 c(u,v) = { 1/sqrt(2) when  u=0, v!=0
          { 1/sqrt(2) when u!=0, v=0
      {  1 otherwise

The FDCT formula described above is very computationally expensive, so we use a different faster form of FDCT.

6. Quantization

Quantization is the step in which the primary compression takes place; in this step, the full 8x8 vector is divided by the values in the quantization table. As a result, the higher frequencies in the image are removed. The reason for this is that our eye is more sensitive to low frequency details and less sensitive to high frequency details.

Luminance Quantization Table {STANDARD}
16,  11,  10,  16,  24,  40,  51,  61,
12,  12,  14,  19,  26,  58,  60,  55,
14,  13,  16,  24,  40,  57,  69,  56,
14,  17,  22,  29,  51,  87,  80,  62,
18,  22,  37,  56,  68, 109, 103,  77,
24,  35,  55,  64,  81, 104, 113,  92,
49,  64,  78,  87, 103, 121, 120, 101,
72,  92,  95,  98, 112, 100, 103,  99 

Similar quantization tables exist for chrominance as well.

An encoder can use a different quantization table, but the same should be specified in the JPEG file so that proper decoding can take place.

The quantization process will result in a lot of zeros in the resultant vector; as a result, the RLC in the next step will cause major decrease in file size.

The quantization tables can be defined manually for testing (comma separated list of 64 values).


Any Quantization Table can be used just by typing in the values.

Effect of quantization

Scaling is done using the following method..

static Byte[] Scale_And_ZigZag_Quantization_Table(Byte[] intable, float quant_scale)
    Byte[] outTable = new Byte[64];
    long temp;
    for (Byte i = 0; i < 64; i++)
        temp = ((long)(intable[i] * quant_scale + 50L) / 100L);
        if (temp <= 0L)
            temp = 1L;
        if (temp > 255L)
            temp = 255L;
        outTable[Tables.ZigZag[i]] = (Byte)temp;
    return outTable;

Using QT-1 (Quantization Table 1)


Luminance QuantizationTable 1 [JPEG STANDARD]


Using QT=1 , Factor = 50 [28.9 Kb]


Using QT=1 , Factor = 900 [9.46 Kb]

Using QT-2 (Quantization Table 2)


Luminance QuantizationTable 2 [Trial: For lower quality]


Using QT=2 , Factor = 50 [19.3 KB]


Using QT=2 , Factor = 900 [8.26 KB]

So, it is clear that the actual compression in JPEG occurs at the quantization step. This is the only lossy part. The Entropy encoding done later is lossless.

7. Entropy coding

a. Zigzag reordering

The 8x8 block is then traversed in zig-zag manner like this:

|   0, 1, 5, 6,14,15,27,28, |
|   2, 4, 7,13,16,26,29,42, |
|   3, 8,12,17,25,30,41,43, |
|   9,11,18,24,31,40,44,53, |
|  10,19,23,32,39,45,52,54, |
|  20,22,33,38,46,51,55,60, |
|  21,34,37,47,50,56,59,61, |
|  35,36,48,49,57,58,62,63  |
(The numbers in the 8x8 block indicate the order in which we traverse the bi-dimensional 8x8 matrix)

As you see, first is the upper-left corner (0,0), then the value at (0,1), then (1,0), then (2,0), (1,1), (0,2), (0,3), (1,2), (2,1), (3,0) etc.

After we are done with traversing in zig-zag the 8x8 matrix, we have now a vector with 64 coefficients (0..63). The reason for this zig-zag traversing is that we traverse the 8x8 DCT coefficients in the order of increasing spatial frequencies.

b. Run length coding

Run-length encoding (RLE) is a very simple form of compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. This is most useful on data that contains many such runs; here zeros.

Example data: 20,17,0,0,0,0,11,0,-10,-5,0,0,1,0,0,0, 0 , 0 ,0 , only 0,..,0

RLC for JPEG compression: (0,20) ; (0,17) ; (4,11) ; (1,-10) ; (0,-5) ; (2,1) ; EOB

Format: (Zeros preceding the number, Number)

In the Huffman coding in the next step, things are coded in 4 bits, so there is a restriction on the number of continuous zeros, to prevent the value 15 (0xF) getting over passed.

So, after 15 zeros, we use (15,0) which indicates there are 16 consecutive zeros.

c. Huffman coding

This is a complex procedure. I will explain the procedure later.

Using the code

1. An instance of the encoder can be created by:

BaseJPEGEncoder encoder = new BaseJPEGEncoder();

2. Then any of the two functions should be called.

1. EncodeImageBufferToJpg()

public void EncodeImageBufferToJpg(Byte[, ,] ImageBuffer, 
            Point originalDimension, Point actualDimension, 
            BinaryWriter OutputStream, float Quantizer_Quality, 
            byte[] luminance_table, byte[] chromiance_table, 
            Utils.IProgress progress, 
            Utils.ICurrentOperation currentOperation)

2. EncodeImageToJpg()

public void EncodeImageToJpg(Image ImageToBeEncoded, 
       BinaryWriter OutputStream, 
       float Quantizer_Quality, byte[] luminance_table, 
       byte[] chromiance_table, Utils.IProgress progress, 
       Utils.ICurrentOperation currentOperation)

The image can be in the form of a buffer (pixel array) which can be obtained from a bitmap by calling Fill_Image_Buffer().

byte[,,] Fill_Image_Buffer(Bitmap bmp, IProgress progress,ICurrentOperation operation);

The array is defined as Byte [Width, Height, 3]. The third index is the color 'Red = 0', 'Blue = 1', and 'Green = 2'.

Full example:

Utils.ProgressUpdater progressObj = new Utils.ProgressUpdater();
Utils.CurrentOperationUpdater currentOperationObj = new Utils.CurrentOperationUpdater();

Bitmap bmp = new Bitmap("C:\\source.bmp");
byte [,,] image_array = Utils.Fill_Image_Buffer(bmp, progressObj, currentOperationObj);

Point originalDimension = new Point(bmp.Width, bmp.Height);
Point actualDimension = Utils.GetActualDimension(originalDimension);            

FileStream fs = new FileStream("C:\\dest.jpg", FileMode.Create, 
    FileAccess.Write, FileShare.None);
BinaryWriter bw = new BinaryWriter(fs);

JpegEncoder.BaseJPEGEncoder encoder = new BaseJPEGEncoder();

encoder.EncodeImageBufferToJpg(image_array, originalDimension, actualDimension,
    bw, float.Parse("50"), // Lower quality value better Image
    Tables.std_luminance_qt, Tables.std_chrominance_qt,
    progressObj, currentOperationObj);

Other details

I used Interfaces for progress update, so initialize the objects as shown, before calling the encode function.

Utils.ProgressUpdater progressObj = new Utils.ProgressUpdater();
Utils.CurrentOperationUpdater currentOperationObj = new Utils.CurrentOperationUpdater();

If somebody has a better way for progress reporting, I would like to know.

Points of interest

Fill_Image_Buffer() and Write_Bmp_From_Data() in Utils.cs uses interop "gdi32.dll" to fill the image buffers fast enough. Earlier, I used GetPixel() and SetPixel(), and it was very slow. So, I had to change the functions.

I spent two days to figure out how the GetDIBits() and SetDIBits() function, by writing a C++ .NET program and then converting it to C#. InteropGDI.cs contains a lot of unused functions, but they all work as I had written a program which uses them all.


This is the first public version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Questionwhen I run this code the visual studio featured this error when I put image on it and another error: Severity Code Description Project File Line Suppression State Warning The referenced component 'System.Windows.Forms' could not be found. EncoderUI, Pin
Member 135044662-Dec-17 5:26
Member 135044662-Dec-17 5:26 
QuestionEncodeBackgroundWorker_DoWork avoide thread-safe debug error Pin
Member 906654826-Jun-15 7:28
Member 906654826-Jun-15 7:28 
QuestionLooks good but Pin
dommy1A4-Mar-14 18:51
dommy1A4-Mar-14 18:51 
Questionthank you bhai you saved me.......... Pin
ramranu220-Feb-14 18:51
ramranu220-Feb-14 18:51 
QuestionCan you please explain how to get the quantization table from the JPEG image Pin
Parthiban Beem22-Oct-13 10:10
Parthiban Beem22-Oct-13 10:10 
GeneralMy vote of 5 Pin
Ravimal Bandara13-Aug-13 20:29
Ravimal Bandara13-Aug-13 20:29 
GeneralRe: My vote of 5 Pin
Arpan Jati21-Aug-13 5:25
Arpan Jati21-Aug-13 5:25 
GeneralMy vote of 5 Pin
Paulo Zemek30-Mar-13 6:59
mvaPaulo Zemek30-Mar-13 6:59 
GeneralRe: My vote of 5 Pin
Arpan Jati21-Aug-13 5:26
Arpan Jati21-Aug-13 5:26 
QuestionPossibly the best jpeg article I ever read Pin
Pascal Ganaye21-Mar-13 1:49
Pascal Ganaye21-Mar-13 1:49 
AnswerRe: Possibly the best jpeg article I ever read Pin
Arpan Jati21-Aug-13 5:26
Arpan Jati21-Aug-13 5:26 
Questionwhere is decoding part? Pin
vasanth198911-Feb-13 14:56
vasanth198911-Feb-13 14:56 
Generallooks good, but it is difficult to understand the logic underneath Pin
wayne wu25-Jan-13 17:44
wayne wu25-Jan-13 17:44 
QuestionNice But problems.. :( Pin
MikeFr778-Oct-12 5:57
MikeFr778-Oct-12 5:57 
QuestionOutputFileName from String to Bitmap Pin
sidali.kadi20-Mar-12 0:13
sidali.kadi20-Mar-12 0:13 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 21:25
professionalManoj Kumar Choubey26-Feb-12 21:25 
QuestionSeems not working on Windows7 Pin
Member 431389727-Dec-11 0:48
Member 431389727-Dec-11 0:48 
AnswerRe: Seems not working on Windows7 Pin
hughknaus15-Jun-12 12:04
hughknaus15-Jun-12 12:04 
BugOutput Jpg File can't open in Photoshop Pin
gebin18-Oct-11 17:23
gebin18-Oct-11 17:23 
GeneralMy vote of 3 Pin
Shargon_8512-Aug-11 2:56
Shargon_8512-Aug-11 2:56 
GeneralRe: My vote of 3 Pin
Arpan Jati15-Sep-11 4:42
Arpan Jati15-Sep-11 4:42 
GeneralRe: My vote of 3 Pin
Pascal Ganaye21-Mar-13 1:46
Pascal Ganaye21-Mar-13 1:46 
GeneralMy vote of 4 Pin
Fabrice Vergnenegre5-Mar-11 10:53
Fabrice Vergnenegre5-Mar-11 10:53 
GeneralIssues with RGB extraction Pin
Fabrice Vergnenegre5-Mar-11 10:52
Fabrice Vergnenegre5-Mar-11 10:52 
Generaldecode Pin
AmirAlilou17-Dec-10 6:47
AmirAlilou17-Dec-10 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.