C# 当我将图像合并到另一个图像上时,如何将其居中?
我在网上找到了一些合并图像的代码。我设置了它,并解决了它。我最终得到它的工作,但我合并到空白图像的图像不是中心。 下面是它的外观: 这是我的密码:C# 当我将图像合并到另一个图像上时,如何将其居中?,c#,C#,我在网上找到了一些合并图像的代码。我设置了它,并解决了它。我最终得到它的工作,但我合并到空白图像的图像不是中心。 下面是它的外观: 这是我的密码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { }
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnSelectImage_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
openFileDialog.Filter = "Images (*.jpg, *.jpeg, *.png)|*.*";
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFileLoc.Text = openFileDialog.FileName;
System.Drawing.Image img = System.Drawing.Image.FromFile(txtFileLoc.Text);
txtImgWidth.Text = img.Width.ToString();
txtImgHeight.Text = img.Height.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
txtOutputPath.Text = fbd.SelectedPath;
//string[] files = System.IO.Directory.GetFiles(fbd.SelectedPath);
//System.Windows.Forms.MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
}
}
}
private void btnResize_Click(object sender, EventArgs e)
{
DateTime time = DateTime.Now;
string format = "MMMddyyy";
string imgdate = time.ToString(format);
int Height = Convert.ToInt32(txtNewHeight.Text);
int Width = Convert.ToInt32(txtNewWidth.Text);
Image resultImage = new Bitmap(Height, Width, PixelFormat.Format24bppRgb);
using (Graphics grp = Graphics.FromImage(resultImage))
{
grp.FillRectangle(
Brushes.White, 0, 0, Width, Height);
resultImage.Save(txtOutputPath.Text + @"\" + imgdate + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Image img = Image.FromFile(txtFileLoc.Text);
}
Image playbutton;
try
{
playbutton = Image.FromFile(txtFileLoc.Text);
}
catch (Exception ex)
{
return;
}
Image frame;
try
{
frame = resultImage;
}
catch (Exception ex)
{
return;
}
using (frame)
{
using (var bitmap = new Bitmap(Width, Height))
{
using (var canvas = Graphics.FromImage(bitmap))
{
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.DrawImage(frame,
new Rectangle(0,
0,
Width,
Height),
new Rectangle(0,
0,
frame.Width,
frame.Height),
GraphicsUnit.Pixel);
canvas.DrawImage(playbutton,
(bitmap.Width / 2) - (playbutton.Width / 2),
(bitmap.Height / 2) - (playbutton.Height / 2));
canvas.Save();
}
try
{
bitmap.Save(txtOutputPath.Text + @"\" + imgdate + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception ex) { }
}
}
}
}
在另一个图像上绘制时,为了使图像居中,您需要计算图像的位置(X,Y),可以按以下方式进行计算:
int x = (bitmap.Width - image.Width) / 2;
int x = (bitmap.Height - image.Height) / 2;
然后你就可以用它来画你的图像了
canvas.DrawImage(image, x, y, image.Width, image.Height);
请注意,这种方法允许您将图像居中,而不管它是小于原始位图还是大于原始位图。当计算(X,Y)时,可能会出现一个像素的舍入误差,因此使用数学是有益的。舍入可以纠正这一点。a)这样的居中只是非常简单的数学..但是。。b) 确保两个图像具有完全相同的dpi设置!!!我真的很感谢你的快速反应!它工作得很好。非常感谢。