tags:

views:

119

answers:

4

I have a bmp file and am trying to convert it to jpeg format. The jpeg created using the following code loses a lot of clarity. I have tried tweaking many settings to no avail.

Does anyone have a function which will convert a bmp file to a jpeg?

var
  Bmp: TBitmap;
  Jpg: TJPEGImage;
begin
  Bmp := TBitmap.Create;
  Jpg := TJPEGImage.Create;
  try
    Bmp.LoadFromFile(BmpFileName);
    Jpg.Assign(Bmp);
    jpg.PixelFormat    :=jf24bit;  // or jf8bit
    Jpg.CompressionQuality := 100;
    Jpg.ProgressiveDisplay := False;
    Jpg.ProgressiveEncoding := False;

    Jpg.SaveToFile(JpgFileName);
  finally
    Jpg.Free;
    Bmp.Free;
  end;
end;

Update I figure it might be good to add references to the actual graphics.

Here is the bmp file:

alt text

Here is the converted jpg file:

alt text

Update II A lot of people have responded that jpeg is not the graphic type to use in this case. Understood. Not to beat a dead horse, but I have been able to use other programs (i.e. Photoshop) and convert this to a nice looking jpeg. And the tool I am using to create the chart (fusioncharts) is able to export it to a nice looking jpeg too (see below). What is the difference?

alt text

+3  A: 
Andreas Rejbrand
Changing the CompressionQuality does not affect it (I have updated the value in my original post).
M Schenkel
Ok Thanks. The "big picture" answer appears to be that jpeg is not the applicable graphic type. BUT, read my Update II above. It seems it can in fact be converted to a respectable jpeg. Why is this?
M Schenkel
A: 

You can read this article about resizing images using antialiasing techniques. It's explined step by step and the code is included.

This is a sample that the problem and posible resoluions explined:

The article is written in Spanish but you can transalate it.

Regards

Neftalí
-1. I'm afraid I don't see how changing the dimensions of a bitmap is relevant to the issue of converting a bitmap to a JPEG.
Rob Kennedy
Good answer, but for some other question.
Chris Thornton
Excuse-me. Really the response it's not correct for this question. I have a mistake when reading the question (possibly read it fast). It really makes no reference to the change in size. ;-(
Neftalí
+1  A: 

As Andreas said, jack up the CompressionQuality to 100. If you'd like to try another example, Dr. Bob (frequent SO contributor) has a nice writeup here:
http://articles.techrepublic.com.com/5100-10878_11-5031886.html#

He has code that uses a stream approach. I would expect the same result, but you never know. Here's the code:
http://techrepublic.com.com/html/tr/sidebars/5031886-1.html

Chris Thornton
Tried it - same result. Can you be guaranteed to achieve the same results with a jpeg as with a bmp?
M Schenkel
@MSchenkel - no, jpeg is a lossy algorithm, and will not give identical results as a BMP. Use GIF or PNG for best results. Jpeg will be satisfactory for photos, but you'll see "mosquito" artifacts with screenshots, text, line drawings, etc..
Chris Thornton
A: 

As others have said, JPEG is simply the wrong format for that kind of image - and with the high-frequency chroma edges, you have possibly one of the worst-case images for JPEG!

I recommend you get a copy of Paint.Net, and try saving/previewing images in different formats (PNG, JPEG, GIF, BMP, etc) and get a feel for the trade-offs between image size, quality and performance yourself.

I've tried saving your test image with Paint.net as JPEG, and get exactly the same quality as Delphi gives you, so there's nowt wrong with what you (or Delphi) are doing...

UPDATE: According to Wikipedia...

There is an optional lossless mode defined in the JPEG standard; however, that mode is not widely supported in products.

I'd guess that FusionCharts and Photoshop have a way of specifying this mode. It's not normally used, because JPEG is usually considered synonymous with LOSSY compression.

In summary, while JPEG files can support lossless compression, JPEG is not an ideal format to because it's not widely supported. If you want lossless, PNG remains a better choice. The PNG-compressed version of your image is 30% smaller than the losslessly compressed JPEG one (71K vs 101K)

Roddy
Thank you for your detailed response. So the "Big" picture answer is to not use jpeg. But what I can't understand is how some programs are able to save it in jpeg format and still have it look nice. I have added a new jpeg file above. How is this jpeg able to render nicely?
M Schenkel