我很抱歉,如果我对这个问题感到困惑,那么它指的是可能无法实现的东西,但是我想用winforms创建一个允许用户加载图像的表单,从组合框中选择一些证书级别,然后将其关联的图章放在所述图像上...
我遇到的问题是:加载工作正常,从组合框中选择(因为我甚至知道如何获取特定图像(让他们称之为“邮票'”)特定的位置,并将它们放入一个图片框),我甚至可以保存第一个加载的图像(让我们称之为' pic1'供参考)...但我不知道如何编制任何可以放置“邮票”的邮件。通过' pic1' ...我几乎无法对我已经做过的行所需的代码进行编码,除了格式化之外,如果没有“stackoverflow&#”的帮助,它就无法完成它39; ...
说实话,我刚刚在c#中完成了一些蹩脚的200小时编码(他们没有在网上学习,实际上是课程的一部分-_- ......)
任何想法,帮助,建议,甚至是在开始搞乱编程之前回到你正在做的事情,请?
提前谢谢你。我将离开这里已经拥有的东西......
p.s。:我对OOP也没有多少经验,这就是为什么下面的代码看起来没有真正优化的原因......:s
顺便说一下,消息框里面的下面的字符串是葡萄牙语,因为用户是我的父亲,在葡萄牙,这对他来说更容易让这个表格用葡萄牙语...哦,如果你知道我应该做的任何其他语言,请将其作为反馈...非常感谢:)
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
再次感谢那些帮助过的人:)
答案 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);
}