seeHow can I programmatically convert a BMP file to a JPG or GIF
http://msdn.microsoft.com/msdnmag/issues/03/07/WebQA/

解决方案 »

  1.   

    假设需要转换的bmp图像为aaa.bmpBitmap bmp=new Bitmap("aaa.bmp");
    bmp.Save("bbb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
      

  2.   

    在C#中将.bmp转为.jpg格式的函数或类是什么,以及jpg文件的压缩函数
    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=11392
      

  3.   

    我们的类添加三个私有的数据成员:private Bitmap m_bitmap;private int m_width0;private int m_height0;在构造函数中初始化这三个数据成员,代码如下: public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); 
    // // TODO: Add any constructor code after InitializeComponent call // m_bitmap = null; m_width0 = m_pictureBox.Size.Width; m_height0 = m_pictureBox.Size.Height; } 
      

  4.   

    最后,给“打开”和“转化为”两个按钮添加Click事件,生成两个消息相应函数,代码以及注释如下: private void m_btnOpen_Click(object sender, System.EventArgs e) { //创建一个打开对话框对象 OpenFileDialog ofd = new OpenFileDialog(); //设置对话框的各项属性 ofd.Filter = m_cmbOpen.Text + "|" + m_cmbOpen.Text; string filter = ofd.Filter; ofd.InitialDirectory = System.Environment.CurrentDirectory; ofd.Title = "打开图象文件"; ofd.ShowHelp = true; if(ofd.ShowDialog() == DialogResult.OK) { //如果是OK,则建立一个图象对象 string strFileName = ofd.FileName; m_bitmap = new Bitmap(strFileName); //调整m_pictureBox的大小以适合图象大小 if(m_bitmap.Width > m_bitmap.Height) { //保持宽度 m_pictureBox.Width = m_width0; m_pictureBox.Height = (int)((double)m_bitmap.Height*m_width0/m_bitmap.Width); } else { //保持高度 m_pictureBox.Height = m_height0; m_pictureBox.Width = (int)((double)m_bitmap.Width*m_height0/m_bitmap.Height); } //显示图片 m_pictureBox.Image = m_bitmap; //设置窗体的标题 this.Text = "Image Converter: " + strFileName; m_btnSaveAs.Enabled = true; } } 
    private void m_btnSaveAs_Click(object sender, System.EventArgs e) { //创建一个保存对话框对象 SaveFileDialog sfd = new SaveFileDialog(); //设置对话框的各项属性 sfd.Title = "转化为"; sfd.OverwritePrompt = true; sfd.CheckPathExists = true; sfd.Filter = m_cmbSaveAs.Text + "|" + m_cmbSaveAs.Text; sfd.ShowHelp = true; if(sfd.ShowDialog() == DialogResult.OK) { //如果是OK,则根据不同的选项保存为相应格式的文件 string strFileName = sfd.FileName; switch(m_cmbSaveAs.Text) { case "*.bmp": // 在这里用ImageFormat类 m_bitmap.Save(strFileName, ImageFormat.Bmp); break; case "*.jpg": // 在这里用ImageFormat类 m_bitmap.Save(strFileName, ImageFormat.Jpeg); break; case "*.gif": // 在这里用ImageFormat类 m_bitmap.Save(strFileName, ImageFormat.Gif); break; case "*.tif": // 在这里用ImageFormat类 m_bitmap.Save(strFileName, ImageFormat.Tiff); break; } this.Text = "Image Converter: " + strFileName; } }