转换图像格式

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); } } } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值