在C#中將.bmp轉(zhuǎn)為.jpg格式的函數(shù)或類是什么,以及jpg文件的壓縮函數(shù) ---------------------------------------------------------------
自己寫個(gè), public class Bmp2Jpeg { private long lQuality=50L; private ImageCodecInfo myImageCodecInfo; private EncoderParameters myEncoderParameters; public Bmp2Jpeg(long Quality) { lQuality=Quality; Ini(); } private void Ini() { System.Drawing.Imaging.Encoder myEncoder; EncoderParameter myEncoderParameter; myImageCodecInfo = GetEncoderInfo("image/jpeg"); myEncoder = System.Drawing.Imaging.Encoder.Quality; myEncoderParameters = new EncoderParameters(1); myEncoderParameter = new EncoderParameter(myEncoder,lQuality); myEncoderParameters.Param[0] = myEncoderParameter; } public Bmp2Jpeg() { Ini(); } private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for(j = 0; j < encoders.Length; ++j) { if(encoders[j].MimeType == mimeType) return encoders[j]; } return null; } public byte[] BmpToJpegBuff(Bitmap bmpSrc) { MemoryStream msjpg=new MemoryStream(); bmpSrc.Save(msjpg, myImageCodecInfo, myEncoderParameters); byte[] bjpeg=msjpg.ToArray(); return bjpeg;
} public byte[] BmpToJpegBuff(byte[] bmpSrc) { MemoryStream ms=new MemoryStream(bmpSrc); MemoryStream msjpg=new MemoryStream(); Bitmap myBitmap = new Bitmap(ms,false); //myBitmap.Save("c://temp//"+i.ToString()+".jpg", myImageCodecInfo, myEncoderParameters); myBitmap.Save(msjpg, myImageCodecInfo, myEncoderParameters); byte[] bjpeg=msjpg.ToArray(); return bjpeg; } public Stream BmpToJpegStream(byte[] bmpSrc) { MemoryStream ms=new MemoryStream(bmpSrc); MemoryStream msjpg=new MemoryStream(); Bitmap myBitmap = new Bitmap(ms,false); //myBitmap.Save("c://temp//"+i.ToString()+".jpg", myImageCodecInfo, myEncoderParameters); myBitmap.Save(msjpg, myImageCodecInfo, myEncoderParameters); return msjpg; } public bool BmpToJpegFile(byte[] bmpSrc,string filename) { MemoryStream ms=new MemoryStream(bmpSrc); Bitmap myBitmap=new Bitmap(ms,false); myBitmap.Save(filename,myImageCodecInfo,myEncoderParameters); return true; } public bool BmpToJpegFile1(Bitmap bmpSrc,string filename) { bmpSrc.Save(filename,myImageCodecInfo,myEncoderParameters); return true; } }
---------------------------------------------------------------
最簡(jiǎn)單的方式應(yīng)該是: Bitmap image = new Bitmap("Your image path.bmp"); image.Save("new jpg file path", ImageFormat.Jpg);
注意:別忘了引入名稱空間。
另外,jpg的壓縮率相對(duì)于相同內(nèi)容大小精度的Bmp來說也是不同的,總之很大就是了。尤其是簡(jiǎn)單的圖形壓縮率很好。 ---------------------------------------------------------------
ImageFormat 類(在命名空間 System.Drawing.Imaging 下),就可以實(shí)現(xiàn)BMP、GIF、Icon、JPEG、PNG、TIFF、WMF等常見圖象格式之間的轉(zhuǎn)化了。 打開需要轉(zhuǎn)換的文件,存放在一個(gè)變量中 bitmap.Save(fileName, ImageFormat.Jpg);
|