It seems you have switched the width and height in your CompressSave function. It works if you use:
int _width = img.cols;
int _height = img.rows;
Also note that OpenCV uses BGR as format when dealing with images, so probably you will want to use TJPF_BGR instead of TJPF_RGB.