Yesterday encountered a very strange thing, in a recent project has such a demand, the upload of the picture to compress, to avoid the image because of too large to affect the browsing speed.
The code is also very simple 32 sentence can be achieved, but found that the compressed image, although there is a small, but not very obvious.
The code is as follows:
01 public void CreateThumbnailImage()
02 {
03 Image img = Image.FromFile("e:/1.jpg");
04 Image.GetThumbnailImageAbort cb = new Image.GetThumbnailImageAbort(CallBack);
05 Image thumbnailImage = img.GetThumbnailImage(120, 120, cb, System.IntPtr.Zero);
06 thumbnailImage.Save("e:/2.jpg");
07 }
08 public bool CallBack()
09 {
10 return false;
11 }
Originally is a picture of about 800*700 (80KB), compressed to 120*110 about the picture, size should be within 10kb, but I found that the compressed image has 45kb. There are individual pictures compressed instead of larger than the original, and the Web page can not be opened to display.
Later I looked up the relevant documents and found that. NET is saved in the PNG default format when saving pictures, I think the problem should be out here, or try it.
01//Get thumbnail
public void createthumbnailimage ()
05 {
String imgfile = "E:/1.jpg";
Image img = image.fromfile (imgfile);
Image.getthumbnailimageabort cb = new Image.getthumbnailimageabort (CallBack);
Modified Image thumbnailimage = img. Getthumbnailimage (in the System.IntPtr.Zero);
A string extendname = Imgfile.substring (Imgfile.lastindexof (".") + 1);
ImageFormat Imgformat = Getimageformat (extendname);
Ten Thumbnailimage.save ("E:/2.jpg", Imgformat);
11}
public bool CallBack ()
{
return false;
}
16//Get Picture format
-private static IMAGEF Ormat Getimageformat (String extendname)
{
Extendname = Extendname.tolower ();
if (Extendname = = "gif")
{
return imageformat.gif
}
or else if (extendname = = "JPG" | | exten Dname = = "jpeg")
{
Imageformat.jpeg return
}
28 else if (extendname = "bmp")
return imageformat.bmp
to}
/Else if ext Endname = = "wmf")
imageformat.emf {
-return to
$
+ else if (Extendname = "exif ")
Notoginseng {
imageformat.exif;
4/else if (extendname = "icon")
-{
return imageformat.icon;
(+}
):
4 else if (Extendname = "tiff")
Imageformat.tiff [
]- > {
return imageformat.png;
'
} '
It is true that the compressed image size is only 4.6kb.
Compression considerations:
1, compress the picture to specify the saved format, although the picture name is right after the save, but the picture is still very large, and on the Web page can not be displayed.
2, for the picture format GIF and transparent picture, if the compression to retain the transparent, the compressed format will be used PNG, and the image extension is PNG, if compressed into a GIF format, it can not retain the transparent part.