sharp-sky

想云的天空

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

C# 和 VB6程序,谁的速度快

C#
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2006-9-8
 * Time: 9:36
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using GISwei;
using System.IO;
namespace 获取图象象素值
{
 /// <summary>
 /// Description of MainForm.
 /// </summary>
 public partial class MainForm
 {
  [STAThread]
  public static void Main(string[] args)
  {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   Application.Run(new MainForm());
  }
  
  public MainForm()
  {
   //
   // The InitializeComponent() call is required for Windows Forms designer support.
   //
   InitializeComponent();
   
   //
   // TODO: Add constructor code after the InitializeComponent() call.
   //
  }
  string file=null;
  Bitmap bm=null;
  void Button1Click(object sender, System.EventArgs e)
  {
   OpenFileDialog pfd=new OpenFileDialog();
   pfd.ShowDialog();
   if(File.Exists(pfd.FileName))
   {
    file=pfd.FileName;
    bm=new Bitmap(file);
    this.pictureBox1.Image=bm;
   }
  }
  
  void Button2Click(object sender, System.EventArgs e)
  {
   DateTime dt1=new DateTime();
   DateTime dt2=new DateTime();
   dt1=DateTime.Now;
   for(int k=0;k<100;k++)
   {
    int w=bm.Width;
    int h=bm.Height;
    int Y;
    Color rgb;
    for(int i=0;i<w;i++)
    {
     for(int j=0;j<h;j++)
     {
      rgb=bm.GetPixel(i,j);
      Y= (9798 * rgb.R + 19235 *rgb.G + 3735 * rgb.B) /32768;
      rgb=Color.FromArgb(Y,Y,Y);
      bm.SetPixel(i,j,rgb);
     }
    }
   }
   dt2=DateTime.Now;
   this.Text=(dt2-dt1).ToString();
  }
 }
}
VB6.0

调用Windows API实现相同的功能,耗时72秒多,而C#只要59秒多。

posted on 2006-10-05 14:53  weigb  阅读(455)  评论(0)    收藏  举报