Showing results for 
Search instead for 
Did you mean: 

C# SaveJPEG example

Hi all,
I am looking for help in resolving an issue I have when saving a Bitmap to JPEG.
I am using a minimally adapted code extracted from the tippi.cs example delivered with the samples in version 6.

The problem here is that every third or fourth image turns out completely gray. I have also verified that the same behaviour is present in the unmodified version of the example. Any help or further c# example will be greatly apreciated.



PS some background: I am collecting about 15fps at 1280x1024, processing the frames and then creating an MJPEG stream of cropped frames.


unsafe public static void SaveJPEG(string filename) { IppiSize roi; // JSS sampling = JSS.JS_OTHER; JSS sampling = JSS.JS_411; // JSS sampling = JSS.JS_422; // JSS sampling = JSS.JS_444; JCOLOR in_color = JCOLOR.JC_BGR; JCOLOR out_color = JCOLOR.JC_YCBCR; int JPEGSize; byte[] pJPEG; JERRCODE jerr; JPEGEncoder encoder = new JPEGEncoder(); roi = new IppiSize(bmpdst.Width, bmpdst.Height); //01// Console.WriteLine("Width: {0} Height: {1}", bmpdst.Width, bmpdst.Height); JPEGSize = roi.width * roi.height * nChannels; //01// Console.WriteLine("JPEGSize: {0}", JPEGSize); // just for too small images if (JPEGSize < 1024) { JPEGSize = 4096; } pJPEG = new byte[JPEGSize]; switch (nChannels) { case 1: in_color = JCOLOR.JC_GRAY; out_color = JCOLOR.JC_GRAY; sampling = JSS.JS_444; break; case 3: in_color = JCOLOR.JC_BGR; out_color = JCOLOR.JC_YCBCR; break; case 4: in_color = JCOLOR.JC_CMYK; out_color = JCOLOR.JC_YCCK; break; default: break; } // This saves whithout issues.
// bmpdst.Save(@"R:OutputImage.bmp"); BitmapData dstdata = getBmpData(bmpdst); encoder.SetSource((byte*)dstdata.Scan0, dstdata.Stride, roi, nChannels, in_color); int qualityJPEG = 50; encoder.SetDestination(ref pJPEG, JPEGSize, qualityJPEG, sampling, out_color, JMODE.JPEG_BASELINE, 0); // encoder.SetDestination(ref pJPEG, JPEGSize, qualityJPEG, sampling, out_color, JMODE.JPEG_PROGRESSIVE, 0); if (in_color == JCOLOR.JC_CMYK) { BGRA_to_RGBA((byte*)dstdata.Scan0, width, height, dstdata.Stride); } int size = JPEGSize; jerr = encoder.WriteImageBaseline(ref size); // jerr = encoder.WriteImageProgressive(ref size); // Console.WriteLine("jerr> {0}", jerr); if (JERRCODE.JPEG_OK == jerr) { FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs); bw.Write(pJPEG, 0, size); bw.Close(); fs.Close(); bmpdst.UnlockBits(dstdata); } } [/c-sharp]

0 Kudos
1 Reply

The solution to the problem can be found at thread:

lee_step provided a modified version of encoder.cs and solved the problem.