无间道III (July 2005 C# Microsoft MVP) 的博客

《涅槃经》第十九卷:八大地狱之最,称为无间地狱,为无间断遭受大苦之意,故有此名。《地藏菩萨本愿经卷上》:如是等辈,当堕无间地狱,千万亿劫,以此连绵,求出无期......

博客园 首页 联系 订阅 管理

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;

namespace WindowsApplication1
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button m_btnOpen;
  private System.Windows.Forms.Button m_btnSaveAs;
  private System.Windows.Forms.ComboBox m_cmbOpen;
  private System.Windows.Forms.ComboBox m_cmbSaveAs;
  private System.Windows.Forms.PictureBox m_pictureBox;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;
  private Bitmap m_bitmap;
  private int m_width;
  private int m_height;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
   m_bitmap=null;
   m_width()=m_pictureBox.Size.Width;
   m_height()=m_pictureBox.Size.Height;
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.m_btnOpen = new System.Windows.Forms.Button();
   this.m_btnSaveAs = new System.Windows.Forms.Button();
   this.m_cmbOpen = new System.Windows.Forms.ComboBox();
   this.m_cmbSaveAs = new System.Windows.Forms.ComboBox();
   this.m_pictureBox = new System.Windows.Forms.PictureBox();
   this.SuspendLayout();
   //
   // m_btnOpen
   //
   this.m_btnOpen.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.m_btnOpen.Location = new System.Drawing.Point(8, 8);
   this.m_btnOpen.Name = "m_btnOpen";
   this.m_btnOpen.Size = new System.Drawing.Size(48, 23);
   this.m_btnOpen.TabIndex = 0;
   this.m_btnOpen.Text = "打开";
   this.m_btnOpen.Click += new System.EventHandler(this.m_btnOpen_Click);
   //
   // m_btnSaveAs
   //
   this.m_btnSaveAs.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.m_btnSaveAs.Location = new System.Drawing.Point(216, 8);
   this.m_btnSaveAs.Name = "m_btnSaveAs";
   this.m_btnSaveAs.Size = new System.Drawing.Size(56, 23);
   this.m_btnSaveAs.TabIndex = 1;
   this.m_btnSaveAs.Text = "转化为";
   this.m_btnSaveAs.Click += new System.EventHandler(this.m_btnSaveAs_Click);
   //
   // m_cmbOpen
   //
   this.m_cmbOpen.Items.AddRange(new object[] {
                 "*.bmp",
                 "*.jpg",
                 "*.gif",
                 "*.tif"});
   this.m_cmbOpen.Location = new System.Drawing.Point(64, 8);
   this.m_cmbOpen.Name = "m_cmbOpen";
   this.m_cmbOpen.Size = new System.Drawing.Size(121, 20);
   this.m_cmbOpen.TabIndex = 2;
   this.m_cmbOpen.Text = "*.bmp";
   //
   // m_cmbSaveAs
   //
   this.m_cmbSaveAs.Items.AddRange(new object[] {
                "*.bmp",
                "*.jpg",
                "*.gif",
                "*.tif"});
   this.m_cmbSaveAs.Location = new System.Drawing.Point(277, 10);
   this.m_cmbSaveAs.Name = "m_cmbSaveAs";
   this.m_cmbSaveAs.Size = new System.Drawing.Size(121, 20);
   this.m_cmbSaveAs.TabIndex = 3;
   this.m_cmbSaveAs.Text = "*.bmp";
   //
   // m_pictureBox
   //
   this.m_pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.m_pictureBox.Location = new System.Drawing.Point(0, 40);
   this.m_pictureBox.Name = "m_pictureBox";
   this.m_pictureBox.Size = new System.Drawing.Size(400, 208);
   this.m_pictureBox.TabIndex = 4;
   this.m_pictureBox.TabStop = false;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(408, 301);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.m_pictureBox,
                    this.m_cmbSaveAs,
                    this.m_cmbOpen,
                    this.m_btnSaveAs,
                    this.m_btnOpen});
   this.Name = "Form1";
   this.Text = "Image Converter";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  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_btnmap=new Bitmap(strFileName);
    //调整m_pictureBox的大小以适合图象大小
    if(m_bitmap.Width>m_bitmap.Height)
    {
     //保持宽度
     m_pictureBox.Width=m_width();
     m_pictureBox.Height=(int)((double)m_bitmap.Height*m_width()/m_bitmap.Width);
    }
    else
    {
     //保持高度
     m_pictureBox.Height=m_height();
     m_pictureBox.Width=(int)((double)m_bitmap.Width*m_width()/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.CheckFileExists=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;
   }
  }
 }
}

posted on 2004-05-16 21:13  无间道  阅读(1730)  评论(1编辑  收藏  举报