将.bmp转为.jpg格式的函数或类是什么,以及jpg文件的压缩函数


在C#中将.bmp转为.jpg格式的函数或类是什么,以及jpg文件的压缩函数
 
 

rheleven(::☆痛并快乐着★::) 于 2005-5-25 13:41:37

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



Sunmast(速马, C++/CLI) 于 2005-5-25 13:44:16

用Image.Save方法
要控制JPEG压缩率得用到ImageCodecInfo:

//Load a bitmap from file
Bitmap bm=(Bitmap)Image.FromFile("mypic.jpg");

//Get the list of available encoders
ImageCodecInfo[] codecs=ImageCodecInfo.GetImageEncoders();

//find the encoder with the image/jpeg mime-type
ImageCodecInfo ici=null;
foreach(ImageCodecInfo codec in codecs)
{
if(codec.MimeType=="image/jpeg")
ici=codec;
}

//Create a collection of encoder parameters (we only need one in the collection)
EncoderParameters ep=new EncoderParameters();

//We'll save images with 25%, 50%, 75% and 100% quality as compared with the original
for(int x=25;x<101;x+=25)
{
//Create an encoder parameter for quality with an appropriate level setting
ep.Param[0]=new EncoderParameter(Encoder.Quality,(long)x);

//Save the image with a filename that indicates the compression quality used
bm.Save("C:\\quality"+x.ToString()+".jpg",ici,ep);