Winforms -将一个图像放置在另一个图像上(类似邮票)并保存它
本文关键字:图像 保存 另一个 一个 Winforms | 更新日期: 2023-09-27 18:18:30
我很抱歉,如果我要引起一些混乱的问题,至于它指的东西可能是不可能的,但我想,使用winforms,创建一个表单,允许用户加载图像,从组合框中选择一些证书级别,然后将其相关的印章在说的图像…
我遇到的问题是:加载工作得很好,从组合框中选择(因为我甚至知道如何从特定位置获得特定的图像(让我们称之为"邮票"),并将它们放入图片框中),我甚至可以保存第一个加载的图像(让我们称之为"pic1"以供参考)…但我不知道如何编程,无论它将是"邮票"在"pic1"…除了格式化,我几乎不能编写我已经做过的所需的行,如果没有'stackoverflow'的帮助,我就不会这样做…
说实话,我最近刚刚用c#完成了大约200小时的蹩脚编码(而且它们不是在线学习的,实际上是课程的一部分-_-…)
有什么想法,帮助,建议,甚至是"回到你在开始乱编程之前做的事情",拜托?
提前谢谢你。我将把我已经有的东西留在这里……
注。我对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
再次感谢那些帮助过你的人:)
看看这个,代码有很好的文档,应该不会很难理解,你可以尝试一步一步调试:
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);
}