What is a function or class in C # that turns. bmp into. jpg, and the compression function of a JPG file
---------------------------------------------------------------
Write one yourself,
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;
}
}
---------------------------------------------------------------
The easiest way to do this is to:
Bitmap image = new Bitmap ("Your image Path.bmp");
Image. Save ("New jpg file path", imageformat.jpg);
Note: Don't forget to introduce namespaces.
In addition, the compression rate of JPG relative to the same size of the content of BMP is also different, in short, very large. In particular, simple graphics compression rate is very good.
---------------------------------------------------------------
ImageFormat class (under the namespace System.Drawing.Imaging), you can achieve BMP, GIF, Icon, JPEG, PNG, TIFF, WMF and other common image format conversion between.
Open the file you want to convert, and store it in a variable
Bitmap. Save (FileName, imageformat.jpg);