`

转换图像格式

阅读更多
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; namespace imageConvert { public class Form1 : System.Windows.Forms.Form { private Bitmap m_bitmap; //重要!打开的图象文件放在这个实例中 private int m_width0; //图象的宽度 private int m_height0; //图象的高度 private System.Windows.Forms.PictureBox pictureBox1; private void btnOpenfile_Click(object sender, System.EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = cmbOpenFiletype.Text + "|" + cmbOpenFiletype.Text; string filter = ofd.Filter; ofd.InitialDirectory = System.Environment.CurrentDirectory; ofd.Title = "打开图象文件"; ofd.ShowHelp = true; if(ofd.ShowDialog() == DialogResult.OK) { string strFileName = ofd.FileName; m_bitmap = new Bitmap(strFileName); if(m_bitmap.Width > m_bitmap.Height) { //Keep the width pictureBox1.Width = m_width0; pictureBox1.Height = (int)((double)m_bitmap.Height*m_width0/m_bitmap.Width); } else { //Keep the height pictureBox1.Height = m_height0; pictureBox1.Width = (int)((double)m_bitmap.Width*m_height0/m_bitmap.Height); } pictureBox1.Image = m_bitmap; btnSave.Enabled = true; } } private void btnSave_Click(object sender, System.EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "图象另存为"; sfd.OverwritePrompt = true; sfd.CheckPathExists = true; sfd.Filter = cmbSaveFiletype.Text + "|" + cmbSaveFiletype.Text; sfd.ShowHelp = true; if(sfd.ShowDialog() == DialogResult.OK) { string strFileName = sfd.FileName; switch(cmbSaveFiletype.Text) { case "*.bmp": m_bitmap.Save(strFileName, ImageFormat.Bmp); break; case "*.jpg": m_bitmap.Save(strFileName, ImageFormat.Jpeg); break; case "*.gif": m_bitmap.Save(strFileName, ImageFormat.Gif); break; case "*.ico": m_bitmap.Save(strFileName, ImageFormat.Icon); break; } MessageBox.Show("图象文件格式转换成功!", "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics