2015-10-20 111 views
-1

我很抱歉,如果我要去我引起一些混淆的問題,因爲它涉及到的東西,也許是不可能的,但我d喜歡使用winforms創建一個允許用戶加載圖像的表單,從組合框中選擇一些證書級別,然後將其相關的圖章放在所述圖像上...的WinForms - 將圖像在另一個圖像(郵票等),並將其保存

我遇到的問題是:加載工作得很好,從組合框中選擇(因爲我甚至知道如何從特定位置獲得特定圖像(讓我們稱之爲'郵票'),並將它們放入一個picturebox中),我甚至可以保存第一個加載圖片(我們稱之爲'pic1'作爲參考)......但我不知道如何編程將'郵票'放置在'pic1'上......地獄我可以幾乎沒有編碼所需的線,我已經做了,格式除外,並不會沒有幫助從'stackoverflow'...

真相被告知,我剛剛完成了一些糟糕的200小時在C#編碼(和他們沒有在網上了解到,它實際上是一個過程-_-...的一部分)

任何想法,幫助,建議甚至是「回到你開始之前在做什麼弄亂編程「,請嗎?

謝謝你提前。我會離開這裏,我已經有...

PS:我沒有太多的經驗,無論是面向對象,這就是爲什麼下面的代碼不會像什麼真正優化...:■

順便說一句,在消息框內的下面的字符串是葡萄牙語,因爲用戶是我的父親,並且對他來說,在葡萄牙,使用葡萄牙語工作的形式會更容易...

哦,如果你知道任何其他語言,我應該也許這樣做,請給作爲反饋...非常感謝:)

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     // format form 
     // TopMost gets the window at max resolution size; WindowsState already maximizes window at start; 
     this.TopMost = true; 
     this.WindowState = FormWindowState.Maximized; 

     // format panel 
     panPic.AutoSize = false; 
     panPic.AutoScroll = true; 

     // format picbox 
     pbPic.SizeMode = PictureBoxSizeMode.AutoSize; 
     pbPic.Location = new Point(0, 0); 
    } 

    private void btLoad_Click(object sender, EventArgs e) 
    { 
     // boolean tester 
     bool test = false; 
     // turn combobox and apply btn enabled; 
     cbxCertificate.Enabled = true; 
     btApply.Enabled = true; 

     do 
     { 
      // open dialog box to upload picture; 
      // instance OpenFileDialog class object 'dlg'; 
      OpenFileDialog dlgPic = new OpenFileDialog(); 
      // define object dialog title; 
      dlgPic.Title = "Por favor selecione imagem a carregar"; 
      // define object dialog filter; 
      dlgPic.Filter = "All Files|*.*"; 

      // if user decides object and presses 'OK'; 
      if (dlgPic.ShowDialog() == DialogResult.OK) 
      { 
       // check if continue (probability of uploading incorrect image) 
       DialogResult dlgConf = MessageBox.Show("Carregou a imagem: " 
        + dlgPic.SafeFileName.ToString() + "\nContinuar?", "Continuar", MessageBoxButtons.YesNo); 
       if (dlgConf == DialogResult.Yes) 
       { 
        // show image and its location 
        txbLocation.Text = dlgPic.FileName.ToString(); 
        pbPic.Image = Image.FromFile(dlgPic.FileName); 

        // check tester true to exit cycle 
        test = true; 
        break; 
       } 
       else 
       { 
        // keep tester at false to continue inside the cycle; 
        test = false; 
        continue; 
       } 
      } 
      else 
      { 
       break; 
      } 
     } while (test == false); 
    } 

    private void btApply_Click(object sender, EventArgs e) 
    { 
     if (cbxCertificate.SelectedItem != null) 
     { 
      switch (cbxCertificate.SelectedText) 
      { 
       case "A+": 
        // image location 
        // "Parent_Directory"\Certificados\cer_a_mais.png 
        break; 

       case "A": 
        // image location 
        // "Parent_Directory"\Certificados\cer_a.png 
        break; 

       case "B": 
        // image location 
        // "Parent_Directory"\Certificados\cer_b.png 
        break; 

       case "B-": 
        // image location 
        // "Parent_Directory"\Certificados\cer_b_menos.png 
        break; 

       case "C": 
        // image location 
        // "Parent_Directory"\Certificados\cer_c.png 
        break; 

       case "D": 
        // image location 
        // "Parent_Directory"\Certificados\cer_d.png 
        break; 

       case "E": 
        // image location 
        // "Parent_Directory"\Certificados\cer_e.png 
        break; 

       case "F": 
        // image location 
        // "Parent_Directory"\Certificados\cer_f.png 
        break; 

      }// switch 

      // enable save image btn 
      btSave.Enabled = true; 
     } 
     else 
     { 
      MessageBox.Show("Por favor selecione primeiro o tipo de certificado pretendido."); 
     } 
    } 

    private void btSave_Click(object sender, EventArgs e) 
    { 
     // confirm saving before actually opening the save dialog box 
     DialogResult dlgConf = MessageBox.Show("Vai guardar a imagem editada. Continuar?", "Confirmar", MessageBoxButtons.YesNo); 

     // observe validation 
     if (dlgConf == DialogResult.Yes) 
     { 
      // save image from picture box to selected folder 
      // instance save file dialog 
      SaveFileDialog save = new SaveFileDialog(); 

      // default file name 
      save.FileName = "EditedImage"; 

      // default file type 
      save.DefaultExt = ".jpg"; 

      // default filter 
      save.Filter = "Image (.jpg)|*.jpg"; 

      // restore current directory in case of closing before correct saving 
      save.RestoreDirectory = true; 

      // save file 
      if (save.ShowDialog() == DialogResult.OK) 
      { 
       string fileName = save.FileName; 

       // define the using statement in case the object goes out of scope 
       using (System.IO.FileStream fstream = new System.IO.FileStream(fileName, System.IO.FileMode.Create)) 
       { 
        // define image saving ext and save object image into stream file selected path 
        pbPic.Image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg); 
        // close stream 
        fstream.Close(); 
       } 
      }// if2 
     }// if1 
     else 
     { 
      MessageBox.Show("Continue a edição por favor..."); 
     }// else1 
    } 
}// class Form 

////後的情況得到解決////

public partial class Form1 : Form 
{ 
    #region constructor 
    public Form1() 
    { 
     InitializeComponent(); 

     // format form 
     // TopMost gets the window at max resolution size; WindowsState already maximizes window at start; 
     this.TopMost = true; 
     this.WindowState = FormWindowState.Maximized; 

     // format panel 
     panPic.AutoSize = false; 
     panPic.AutoScroll = true; 

     // format picbox 
     pbPic.SizeMode = PictureBoxSizeMode.AutoSize; 
     pbPic.Location = new Point(0, 0); 
    } 
    #endregion 

    #region load_pic 
    private void btLoad_Click(object sender, EventArgs e) 
    { 
     // boolean tester 
     bool test = false; 
     // turn combobox and apply btn enabled; 
     cbxCertificate.Enabled = true; 
     btApply.Enabled = true; 

     do 
     { 
      // open dialog box to upload picture; 
      // instance OpenFileDialog class object 'dlg'; 
      OpenFileDialog dlgPic = new OpenFileDialog(); 
      // define object dialog title; 
      dlgPic.Title = "Por favor selecione imagem a carregar"; 
      // define object dialog filter; 
      dlgPic.Filter = "All Files|*.*"; 

      // if user decides object and presses 'OK'; 
      if (dlgPic.ShowDialog() == DialogResult.OK) 
      { 
       // check if continue (probability of uploading incorrect image) 
       DialogResult dlgConf = MessageBox.Show("Carregou a imagem: " 
        + dlgPic.SafeFileName.ToString() + "\nContinuar?", "Continuar", MessageBoxButtons.YesNo); 
       if (dlgConf == DialogResult.Yes) 
       { 
        // show image and its location 
        txbLocation.Text = dlgPic.FileName.ToString(); 
        pbPic.Image = Image.FromFile(dlgPic.FileName); 

        // check tester true to exit cycle 
        test = true; 
        break; 
       } 
       else 
       { 
        // keep tester at false to continue inside the cycle; 
        test = false; 
        continue; 
       } 
      } 
      else 
      { 
       break; 
      } 
     } while (test == false); 
    } 
    #endregion 

    #region apply_stamp 
    private void btApply_Click(object sender, EventArgs e) 
    { 
     // parent directory 
     // original image to execute the method 'addStamp'; 
     // bitmap image to use in the method 'addStamp'; 
     // string to include the path of the stamp, 
     // to be changed according to each case below (see switch); 
     string parentDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName; 
     Image originalImage; 
     Bitmap bitmap; 
     string stampDir = ""; 

     if (cbxCertificate.SelectedItem != null) 
     { 
      // switch 
      switch (cbxCertificate.SelectedItem.ToString()) 

      // note: whenever i need to switch over what's selected on a combobox, 
      // use 'SelectedItem.ToString()'; 
      { 
       case "A+": 
        stampDir = parentDir + "\\Certificados\\cer_a_mais.png"; 
        break; 

       case "A": 
        stampDir = parentDir + "\\Certificados\\cer_a.png"; 
        break; 

       case "B": 
        stampDir = parentDir + "\\Certificados\\cer_b.png"; 
        break; 

       case "B-": 
        stampDir = parentDir + "\\Certificados\\cer_b_menos.png"; 
        break; 

       case "C": 
        stampDir = parentDir + "\\Certificados\\cer_c.png"; 
        break; 

       case "D": 
        stampDir = parentDir + "\\Certificados\\cer_d.png"; 
        break; 

       case "E": 
        stampDir = parentDir + "\\Certificados\\cer_e.png"; 
        break; 

       case "F": 
        stampDir = parentDir + "\\Certificados\\Certificados\\cer_f.png"; 
        break; 

      }// switch 

      // declare the originalImage being the image from the picture box (previously loaded); 
      // execute the addStamp(); 
      // replace the image on the picture box with the edited one (bitmap); 
      originalImage = pbPic.Image; 
      bitmap = addStamp(originalImage, stampDir); 
      pbPic.Image = bitmap; 

      // enable save image btn 
      btSave.Enabled = true; 
     } 
     else 
     { 
      MessageBox.Show("Por favor selecione primeiro o tipo de certificado pretendido."); 
     } 
    } 
    #endregion 

    #region save_pic 
    private void btSave_Click(object sender, EventArgs e) 
    { 
     // confirm saving before actually opening the save dialog box 
     DialogResult dlgConf = MessageBox.Show("Vai guardar a imagem editada. Continuar?", "Confirmar", MessageBoxButtons.YesNo); 

     // observe validation 
     if (dlgConf == DialogResult.Yes) 
     { 
      // save image from picture box to selected folder 
      // instance save file dialog 
      SaveFileDialog save = new SaveFileDialog(); 

      // default file name 
      save.FileName = "EditedImage"; 

      // default file type 
      save.DefaultExt = ".jpg"; 

      // default filter 
      save.Filter = "Image (.jpg)|*.jpg"; 

      // restore current directory in case of closing before correct saving 
      save.RestoreDirectory = true; 

      // save file 
      if (save.ShowDialog() == DialogResult.OK) 
      { 
       string fileName = save.FileName; 

       // define the using statement in case the object goes out of scope 
       using (System.IO.FileStream fstream = new System.IO.FileStream(fileName, System.IO.FileMode.Create)) 
       { 
        // define image saving ext and save object image into stream file selected path 
        pbPic.Image.Save(fstream, System.Drawing.Imaging.ImageFormat.Jpeg); 
        // close stream 
        fstream.Close(); 
       } 
      }// if2 
     }// if1 
     else 
     { 
      MessageBox.Show("Continue a edição por favor..."); 
     }// else1 
    } 
    #endregion 

    #region methods 
    public Bitmap addStamp(Image originalImage, String stampImagePath) 
    { 
     Image stampImage = Image.FromFile(stampImagePath); 

     Bitmap bitmap = new Bitmap(originalImage); 

     Graphics gr = Graphics.FromImage(bitmap); 

     gr.DrawImage(stampImage, new Point(0, 0)); 

     return bitmap; 

    } 
    #endregion 

}// class Form 

再次感謝你對那些誰幫助:)

+1

TL; DR ......您是否在尋找水印? SO(英文和西班牙文)有很多關於渲染位圖或保存圖像的問題 - 所以不太清楚你卡在哪裏。 –

+0

我們對此深感抱歉...問題是我真的不知道很多關於編程和無法理解閹我需要什麼適合我水印類別或不...也許它只是讓我太小白和沒有經驗的,或者說,我沒有嘗試過足夠...對不起,讓你失去了你的時間來回答...謝謝反正 –

回答

0

檢查了這一點,該代碼是非常有據可查的,不應該這樣很難理解它,你可以嘗試一步步調試:

http://www.codeproject.com/Articles/2927/Creating-a-Watermarked-Photograph-with-GDI-for-NET

正如意見建議我寫了一個小方法,可以幫助你:

public void addStamp(String originalImagePath, String stampImagePath,String outputPath) 
     { 
      Image originalImage=Image.FromFile(originalImagePath); 
      Image stampImage = Image.FromFile(stampImagePath);    

      Bitmap bitmap = new Bitmap(originalImage); 

      Graphics gr = Graphics.FromImage(bitmap); 

      gr.DrawImage(stampImage, new Point(0, 0)); 

      bitmap.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png);  
     } 
+0

非常感謝你對這樣的好例子回答......我不是在電腦前,現在,但只要可能,我會測試它,看它是否適合我需要什麼... –

+1

非常感謝你的幫助......它的工作完美和應用程序運行得非常好...我會發佈下面的完整代碼,以便任何需要它的人都可以使用它......謝謝你不放棄這個noob :) –

相關問題