2012-04-18 55 views
5

我想製作一個將png圖像合併到一個PNG中的程序。 所有圖像的高度爲78px,寬度爲120px,所以我創建了一個位圖1200x78(我需要合併10張圖像) 它合併,但結果png中的每個圖像縮放大約x2。爲什麼?.NET在繪製圖形時縮放圖像

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Security; 
using System.Drawing.Imaging; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     OpenFileDialog openFileDialog1; 
     Bitmap bitmap; 

     public Form1() 
     { 
      InitializeComponent(); 
      openFileDialog1 = new OpenFileDialog(); 
      openFileDialog1.Multiselect = true; 
      openFileDialog1.Filter = 
     "Images (*.PNG;*.JPG;*.GIF)|*.PNG;*.JPG;*.GIF|" + 
     "All files (*.*)|*.*"; 
      openFileDialog1.Title = "Select images to merge"; 

      bitmap = new Bitmap(1200, 78); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      DialogResult dr = this.openFileDialog1.ShowDialog(); 

      if (dr == System.Windows.Forms.DialogResult.OK) 
      { 
       // Read the files 
       int shift = 0; 
       foreach (String file in openFileDialog1.FileNames) 
       { 
        // Create a PictureBox. 
        try 
        { 
         PictureBox pb = new PictureBox(); 
         Image loadedImage = Image.FromFile(file); 
         pb.Height = loadedImage.Height; 
         pb.Width = loadedImage.Width; 
         pb.Image = loadedImage; 
         flowLayoutPanel2.Controls.Add(pb); 

         paintToBitmap(loadedImage, shift); 

         shift += loadedImage.Width; 
        } 
        catch (SecurityException ex) 
        { 
         // The user lacks appropriate permissions to read files, discover paths, etc. 
         MessageBox.Show("Security error\n\n" + 
          "Error message: " + ex.Message + "\n\n" + 
          "Details (send to Support):\n\n" + ex.StackTrace 
         ); 
        } 
        catch (Exception ex) 
        { 
         // Could not load the image - probably related to Windows file system permissions. 
         MessageBox.Show("!!!"); 
        } 
       } 


       saveImage(); 
      } 
     } 

     private void paintToBitmap(Image image, int shift) 
     {   
      Graphics graphics = Graphics.FromImage(bitmap); 
      graphics.DrawImage(image, new Point(shift, 0)); 
     } 

     private void saveImage() 
     { 
      bitmap.Save("d:\\result.png", System.Drawing.Imaging.ImageFormat.Png); 
     } 

     private void flowLayoutPanel2_Paint(object sender, PaintEventArgs e) 
     { 

     } 
    } 
} 
+0

我跑你的代碼,它似乎很好地工作。你可以附上你的一個輸入圖像嗎?我猜想這是您的圖像在dpi或類似方面的問題。 – Till 2012-04-18 08:00:04

+0

http://files.moonmana.com/forums/m1.png http://files.moonmana.com/forums/m2.png – 2012-04-18 08:10:18

回答

3

嘗試更新您的代碼以這種

graphics.DrawImage(image, new Rectangle(shift, 0,120,78)); 
+0

非常感謝,矩形幫助,但第三個參數是寬度,所以它應該是120,沒有轉變。 graphics.DrawImage(image,new Rectangle(shift,0,120,78)); – 2012-04-18 08:00:19

+0

我編輯,所以請標記爲答覆 – Likurg 2012-04-18 08:21:24

1

它似乎試圖將圖片調整到你的尺寸「粉紅狗:P」的形象。當您使用背景填充圖像時,會導致不同的方式。看到這一點:

http://i.stack.imgur.com/3eVEu.png http://i.stack.imgur.com/DaygF.png http://i.stack.imgur.com/xwEve.png

所以,這個問題似乎是在畫面中的隱形背景。

+2

如何在使用填充一種顏色的矩形時知道您的示例如何工作? :) 您正在繪製一個長方形,它的繪製大小從120到240,比您在120處繪製下一個矩形時更復雜,因此它與第一個調整大小的矩形的一部分重疊,等等;) – 2012-04-18 08:00:58

+0

檢查我的編輯評論:) – 2012-04-18 08:03:56

+0

看到我的編輯答案 – MarcoM 2012-04-18 08:49:07