function in C # to convert. bmp to. jpg format

Source: Internet
Author: User
Tags bool ini tostring
Function

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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.