在C#中将.bmp转为.jpg格式的函数

80酷酷网    80kuku.com

  函数

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; 
                       } 
           } 
 
--------------------------------------------------------------- 
 
最简单的方式应该是: 
Bitmap  image  =  new  Bitmap("Your  image  path.bmp"); 
image.Save("new  jpg  file  path",  ImageFormat.Jpg); 
 
注意:别忘了引入名称空间。 
 
另外,jpg的压缩率相对于相同内容大小精度的Bmp来说也是不同的,总之很大就是了。尤其是简单的图形压缩率很好。 
--------------------------------------------------------------- 
 
ImageFormat  类(在命名空间  System.Drawing.Imaging  下),就可以实现BMP、GIF、Icon、JPEG、PNG、TIFF、WMF等常见图象格式之间的转化了。 
打开需要转换的文件,存放在一个变量中 
bitmap.Save(fileName,  ImageFormat.Jpg);

 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: