將.bmp轉(zhuǎn)為.jpg格式的函數(shù)或類是什么,以及jpg文件的壓縮函數(shù)


在C#中將.bmp轉(zhuǎn)為.jpg格式的函數(shù)或類是什么,以及jpg文件的壓縮函數(shù)
?
?

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

---------------------------------------------------------------

最簡單的方式應(yīng)該是:
Bitmap image = new Bitmap("Your image path.bmp");
image.Save("new jpg file path", ImageFormat.Jpg);

注意:別忘了引入名稱空間。

另外,jpg的壓縮率相對于相同內(nèi)容大小精度的Bmp來說也是不同的,總之很大就是了。尤其是簡單的圖形壓縮率很好。
---------------------------------------------------------------

ImageFormat 類(在命名空間 System.Drawing.Imaging 下),就可以實現(xiàn)BMP、GIF、Icon、JPEG、PNG、TIFF、WMF等常見圖象格式之間的轉(zhuǎn)化了。
打開需要轉(zhuǎn)換的文件,存放在一個變量中
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);