views:

697

answers:

4

I've got 100's (maybe 1000's) of products with 10-30 images of each product coming to an online store I've put together. I need to optimize the images' file sizes as much as possible without loosing image quality.

I haven't used jpegtran, jpegoptim, or any other jpeg optimizer directly but I have noticed that punypng shrinks file sizes down about 4-6% on the larger jpeg images LOSSLESSLY.

Meta data is already stripped from the images during upload (via jumpoader) so that is not an option/problem anymore.

Is there any way to get one of the jpeg optimizers to run from C# code?

Note: I'm using shared Godaddy hosting with IIS7 and .Net 3.5

A: 

You should be able to use System.Drawing.Imaging functionality directly for the purpose -- for example, here is example (simple;-) C# code which uses functionality from that system namespace to change a JPEG image's quality.

Alex Martelli
Thanks for the response.It has to be lossless compression; I don't want to change the image quality. After saving the images with System.Drawing.Imaging I ran them through punypng.com (it compresses/optimizes jpegs too) which then stripped another 2-6% off their file size....losslessly.
David Murdoch
A: 

I would batch process the images before uploading them to your web server, rather then try to process them while serving them. This will lead to less load on the web server and let you use any match image processing tools you wish.

Ian Ringrose
I need four sizes for each image (thumb, small, medium, and large). Right now I just have to upload a image and all four sizes are created for me by the server. Batch processing the images manually then uploading won't work (er, it will be too much work). I will need a server-side solution. Thanks for your input.
David Murdoch
Or a tool that does the image processing then uploads them. The processing should be done at upload time anyway not when the image is served.
Ian Ringrose
This is a web-app; all work will need to be done after uploading.The processing IS currently and will be done at upload-time. I serve only static image files...no server processing needed. :o)
David Murdoch
A: 

Why not call punypng.com with Process.Start()? There is no reason why you .net code can't run external programs, provided the processing is done at the time of uploading (rather then when serving the images)

E.g.

  • upload into a "upload" folder,
  • have a windows services that watches for new files in the “upload” folder
  • when you get a new file, start punypng.com to process it and put the output into the correct image folder.
Ian Ringrose
David Murdoch
A: 

If you don't like to mess with temporary files, I'd advise to use C++/CLI.

Create a C++/CLI dll project in visual studio. Create one static managed class, and define the functions as you want to use them from C#:

public ref class JpegTools
{
public:
     static array<byte>^ Optimize(array<byte>^ input)
};

These functions you define can be directly called from C#, and you can implement them with all that C++ offers.

array^ corresponds to a C# byte array. You'll need to use pin_ptr<> to pin the byte array in memory, so you can pass on the data to the unmanaged Jpeg helper function of your choice. C++/CLI has ample support for marshalling managed types to native types. You can also allocate new array with gc_new, to return CLI compatible types. If you have to marshall strings from C# to C++ as part of this excercise, use Mfc/Atl's CString type.

You can statically link all the jpeg code into the dll. A C++ dll can be mixed pure native and C++/CLI code. In our C++/CLI projects, typically only the interface source files know about CLI types, all the rest work with with C++ types.

There's some overhead work to get going this way, but the upside is that your code is compile-time typechecked, and all the dealings with unmanged code and memory are dealt with on the C++ side. It actually works so well that I used C++/CLI to unit test native C++ code almost directly with NUnit.

Good luck!

jdv
You lost me. I can't even figure out how to compile jpegoptim or or jpegtran by themselves.Whats the reason software like this isn't provide as a downloadable dll?
David Murdoch
Could be that the toolkits try to be platform independent. Dll's are windows-only. I agree having a dll would be convenient, but if there was one, you would probably not need our help, right? If you have no C/C++ experience, its maybe wiser to run the .exe from a progrem as suggested below.
jdv