2015-10-20 112 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 :) –

相关问题