首页 > 开发 > 综合 > 正文

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

2024-07-21 02:26:30
字体:
来源:转载
供稿:网友

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);

 

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表