2017-03-01 144 views
0

我在网上发现了一些代码来合并图像。我设置并解决了它。我终于开始工作了,但我合并到空白图像上的图像并未居中。如何在将图像合并到其他图像时将图像居中?

这里是它的外观: enter image description here

这里是我的代码:

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) { } 
      } 
     } 
    } 
} 
+0

a)以此为中心只是非常简单的数学..但.. b)确保两个图像都有相同的dpi设置! – TaW

回答

0

为了集中在另一个图像的顶部绘制时,你需要计算的位置的图像图像(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)时,可能会出现一个像素四舍五入的不准确性,因此使用Math.Round来纠正该错误会是有益的。

+0

我真的很感激快速的回应!它工作完美。谢谢。 –

相关问题