2012-07-11 41 views
0

我在C# 被開發Windows應用程序,在C#轉換這個程序有三個按鈕(打開,保存,灰度) 並具有圖片框在應用插入圖像,灰度和擦拭

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing.Imaging; 

namespace MyFirstWindowsApplication 
{ 
    public partial class Form1 : Form 
    { 
     Bitmap newbitmap; 
     Bitmap newbitmap2; 
     Bitmap outp; 
     Image file; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      DialogResult dr = openFileDialog1.ShowDialog(); 

      if (dr == DialogResult.OK) 
      { 
       file = Image.FromFile(openFileDialog1.FileName); 
       newbitmap = new Bitmap(openFileDialog1.FileName); 
       newbitmap2 = new Bitmap(openFileDialog1.FileName); 
       outp = new Bitmap(openFileDialog1.FileName); 
       pictureBox1.Image = file; 
      } 
     } 


     private void button2_Click(object sender, EventArgs e) 
     { 
      DialogResult dr = saveFileDialog1.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       if (newbitmap != null) 
       { 
        if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "bmp") 
        { 
         newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp); 
        } 

        if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "jpg") 
        { 
         newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp); 
        } 

        if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "bmp") 
        { 
         newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); 
        } 

        if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 4).ToLower() == "jpeg") 
        { 
         newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); 
        } 

        if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "png") 
        { 
         newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Png); 
        } 

        if (saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.Length - 3).ToLower() == "gif") 
        { 
         newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Gif); 
        } 
       } 
       else 
       { 
        MessageBox.Show("you need to open file first"); 
       } 
      } 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 


      for (int x = 0; x < newbitmap.Width; x++) 
      { 
       for (int y = 0; y < newbitmap.Height; y++) 
       { 
        Color originalColor = newbitmap.GetPixel(x, y); 
        int grayscale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11)); 
        Color newColor = Color.FromArgb(grayscale, grayscale, grayscale); 
        newbitmap.SetPixel(x, y, newColor); 
       } 

      } 

      int tmax = 10; 
      int xmax=newbitmap.Width; 
      int ymax=newbitmap.Height; 
      for (int t = 0; t <= tmax; t += 1) 
      { 

       for (int x = 0; x < xmax; x++) 
       { 
        for (int y = 0; y < ymax; y++) 
        { 
         if ((x/xmax) > (t/tmax)) 
         { 
          Color originalco = newbitmap2.GetPixel(x, y); 
          outp.SetPixel(x, y, originalco); 
         } 
         else 
         { 
          Color originalco3 = newbitmap.GetPixel(x, y); ; 
          outp.SetPixel(x, y, originalco3); 
         } 
         pictureBox1.Image = outp; 
        } 


       } 
      } 



     } 
    } 
} 
顯示圖片

問題是犯規使擦拭過渡

回答

1

要進行灰度化圖像考慮使用這個更好的解決方案

public Image MakeGrayscale(Image original) 
{ 
    Image newBitmap = new Bitmap(original.Width, original.Height); 
    Graphics g = Graphics.FromImage(newBitmap); 
    ColorMatrix colorMatrix = new ColorMatrix(
     new float[][] 
     { 
      new float[] {0.299f, 0.299f, 0.299f, 0, 0}, 
      new float[] {0.587f, 0.587f, 0.587f, 0, 0}, 
      new float[] {.114f, .114f, .114f, 0, 0}, 
      new float[] {0, 0, 0, 1, 0}, 
      new float[] {0, 0, 0, 0, 1} 
     }); 

    ImageAttributes attributes = new ImageAttributes(); 
    attributes.SetColorMatrix(colorMatrix); 
    g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 
     0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); 

    g.Dispose(); 
    return newBitmap; 
} 

然後

pictureBox1.Image = MakeGrayscale(newbitmap); 

即使在保存程序中也有一些錯誤。試試這個:

private void button2_Click(object sender, EventArgs e) 
{ 
    if (newbitmap == null) 
    { 
     MessageBox.Show("you need to open file first"); 
     return; 
    } 

    if (DialogResult dr = saveFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
     string ext = Path.GetExtension(saveFileDialog1.FileName).ToLower(); 
     switch (ext) 
     { 
      case ".bmp": 
       newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Bmp); 
       break; 
      case ".jpg": 
      case ".jpeg": 
       newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Jpeg); 
       break; 
      case ".png": 
       newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Png); 
       break; 
      case ".gif": 
       newbitmap.Save(saveFileDialog1.FileName, ImageFormat.Gif); 
       break; 
      default: MessageBox.Show("Extension not supported"); 
     } 
    } 
} 

在我看來過渡爲此不起作用:你的主線程(GUI一個理解)快速變化(非常快)PictureBox的圖像在一個循環,但同時在這個循環中它沒有有時間更新圖形用戶界面,所以picturebox被有效地改變,只有退出循環..所以你沒有看到過渡。
您應該使用BackgroundWorker更改picturebox圖像(在每個循環之間暫停,以便讓人眼看到新圖像)。

+0

不,我沒有意味着灰度,因爲它與我的代碼工作我的意思是第三個循環包含擦拭過渡它應該工作,但我不知道是問題 – 2012-07-11 07:57:04

+0

沒有人所有的代碼是與我合作 我只想要第三個循環的正確語法 – 2012-07-11 08:02:06

+0

@ OmarAl-hamawi:閱讀我編輯的答案。無論如何,你的代碼是錯誤的,所以看看它。我的灰度代碼更高效。您的保存例程具有兩次bmp,並且不使用正確的路徑功能來獲取文件擴展名。 :) – Marco 2012-07-11 08:06:05