Lossless JPEG Rewrites in C# (在Image中添加一些信息)

Lossless JPEG Rewrites in C#

While playing around with some of the GDI+ classes to manipulate pictures from my digital camera, I found that if you want to add or modify an image description of an EXIF image file (EXIF = JPEG plus additional information) you can read and write the image description with the PropertyItem data structure. These PropertyItems are very useful to avoid the bit manipulating in the file structure of an exif file.



But the problem is: when writing the image with the changed or new description the picture part becomes recompressed. You can notice this from the file size; you add information to the file and the file size decreases. When you repeat changing the description, the image become more and more poor, because jpg is a lossy compression. So how do you load and save an jpg or exif file without recompressing the bitmap?

The trick is to rotate the picture by 90 degrees. In this case the framework supplies a lossless rewriting of a jpeg file:
 

When saving to JPEG images, you can also control the compression ratio of the algorithm. You must use a different overload of the Save method:

public void Save(
string filename,
ImageCodecInfo encoder,
EncoderParameters encoderParams
);

The first step is to get the ImageCodecInfo structure for the JPEG image. The GDI+ interface provides no direct method to get this object. You must resort to a little trick—enumerate all the image encoders and check their MIME type properties against the JPEG MIME type string (image/jpeg). The ImageCodecInfo structure contains information inherent in the encoding and decoding of the image.

The EncoderParameters argument represents an array of encoding parameters. Each element of the array is an EncoderParameter type. Possible parameters are listed as members of the static class Encoder. For example, the parameter Compression lets you choose a compression engine for TIFF images. The Quality parameter lets you choose the desired quality of the JPEG compression. This code compresses a JPEG with a 40:1 ratio:

// Set the quality to 40 (must be a long)
Encoder qualityEncoder = Encoder.Quality;
EncoderParameter ratio = new EncoderParameter(qualityEncoder, 40L);
// Add the quality parameter to the list
codecParams = new EncoderParameters(1);
codecParams.Param[0] = ratio;
// Save to JPG
bmp.Save(fileName, jpegCodecInfo, codecParams);

Additionally, this MSDN page details more information, specifically that JPEG images must have a width and height in multiples of 16 in order to have completely "lossless" rotation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/usingGDIPlus/usingimageencodersanddecoders/transformingajpegimagewithoutlossofinformation.asp

Hope this is useful to others!

转载于:https://www.cnblogs.com/javak/archive/2008/06/17/1223941.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值