JpegEncoder issues

Jul 20, 2009 at 1:14 PM

Hi guys,

I would like to save the canvas content into .jpeg picture. When I'm trying to use JpegEncoder NullReference exception is thrown. The issue occurs due to the mistake in FDCT.cs file:

row 37: if (quality < 50) Quality = 5000 / quality;

I've fixed that issue by adding additional verification. Thus I was able to encode image as .jpeg successfully. However, this image had the worth quality - in Encode method of JpegEncoder the quality is set to 0 by default (why?). In addition jpeg image is generated as grayscale (not colorful). Most likely that happens due to the incorrect conversion between Ycbcr and RGB color systems. So that, it would be perfect if you investigate and fix the issues with JpegEncoder

Best regards

Jul 20, 2009 at 4:32 PM
Edited Jul 20, 2009 at 4:47 PM

Hi BorysG,

(1) I changed to following code in the FDCT that you mentioned:

// jpeg_quality_scaling
if (quality <= 0) Quality = 1;
if (quality > 100) Quality = 100;
if (quality < 50) Quality = 5000 / quality;
else Quality = 200 - quality * 2;


// jpeg_quality_scaling
if (quality <= 0) Quality = 1;
else if (quality > 100) Quality = 100;
else if (quality < 50) Quality = 5000 / quality;
else Quality = 200 - quality * 2;

(2) I chaned the default quality in the encoder from 0 to 100 (but there is a new property, where you can define the quality you want).
(3) I added a property with a color, that will be used if the pixel is transparent (because jpeg does not support a transparent layer).
(4) Grayscale color is also fixed, it was a bug by me.

Hope this helps,


Thanks for posting this bug!

Jul 21, 2009 at 8:41 AM

Hi Sebastian,

It works well now. Good job:)

Best wishes,



Jul 21, 2009 at 8:56 AM


I would be happy, if you can post a short a review to the current release, if you want (and give 5 Stars ;)).

Jul 23, 2009 at 4:16 PM
Edited Jul 23, 2009 at 4:17 PM

thanks for your review :)

you have written, that there are still some issues:
- png size is not optimal
- png quality is not optimal
- png can only be loaded

I am really sure that the last point should work. I made a short demo some days before and it worked. Afaik I understand the png format there cannot be quality issues. Can you explain me where you have the issues? I like to fix them.